note 49841 deleted from ref.session by betz
| From: | betz@php.net | Date: | Fri, 08 Jul 2005 09:18:00 +0000 |
| Subject: | note 49841 deleted from ref.session by betz | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-91658@lists.php.net to get a copy of this message | ||
Note Submitter: john at digitizelife dot com
----
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.