note 49841 added to ref.session

From: Date: Thu, 10 Feb 2005 06:30:50 +0000
Subject: note 49841 added to ref.session
Groups: php.notes 
Request: Send a blank email to php-notes+get-84770@lists.php.net to get a copy of this message
Here is a simple way to secure a single page (index.php) using sessions. Create a file called index.php and paste the following code; ---------------------------------------------------- <? session_start(); /* ---------- user settings ---------- */ $un = "Bob"; $pw = "fubar"; /* ---------- actions ---------- */ if ($action == "logout") { session_unset(); } if ($action == "login") { if ($put_un == $un) { if ($put_pw == $pw) { $_SESSION['auth'] = true; } else { $error = "incorrect password."; } } else { $error = "incorrect username."; } } /* ---------- authenticate ---------- */ if ($_SESSION['auth'] == true) { /* secure code */ } else { /* non-secure code */ $view = "login"; } ?> <html> <title>Secure Area</title> <body> <? if ($view == "login") { ?> <form action="index.php" method="post"> user<br> <input type="text" name="put_un"><br> password<br> <input type="password" name="put_pw"> <input name="action" type="hidden" id="action" value="login"><br> <input type="submit" name="Submit" value="login"><? echo "$error<br>"; ?> </form> <? } if ($_SESSION['auth'] == true) { ?> <! -- SECURE CONTENT --> <a href="index.php?action=logout">logout</a> <? } ?> </body> </html> ----------------------------------------------------- Simply set your user name and password in the "user settings" area and put your secure content in the "SECURE CONTENT" area. You can login using a URL like this; http://www.yoursite.com/index.php?action=login&put_un=Bob&put_pw=fubar You can logout using the following link; http://www.yoursite.com/index.php?action=logout Be sure that any addtional PHP code you wish to use should be placed in the "secure code" or the "non-secure code" sections. ---- Manual Page -- http://www.php.net/manual/en/ref.session.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+49841 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+49841&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49841&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+49841&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+49841&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+49841&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+49841&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#84770) next »