note 34169 deleted from function.setcookie by victor
| From: | victor@php.net | Date: | Sat, 27 Mar 2004 20:20:51 +0000 |
| Subject: | note 34169 deleted from function.setcookie by victor | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-67249@lists.php.net to get a copy of this message | ||
Note Submitter: lcattani at urbanet dot ch
----
For those who wants to do a simple login form with cookies, this without having very good experience
in php, here is a working complete code with html:
I think that if you see the page running, the code is self-explaining.
You can use this with a mysql database to request the user & pass, for example...
Anyways, this is a good beginning for your pages
Security? I'm not sure, but i think it's a good compromise without having to deal with
https...
(copy and paste this in a text file named "cookie.php")
<?php
function checkAuth($userToCheck, $passToCheck) {
$user = "foo";
$pass = "bar";
$encodedPass = md5($pass);
if ($userToCheck == $user && $passToCheck == $encodedPass) {
$check = TRUE;
} else {
$check = FALSE;
}
return $check;
}
if (isset($relogin)) {
setcookie ('cuser', "",time()-3600, '/','yourhost.com');
setcookie ('cpass', "",time()-3600, '/','yourhost.com');
}
if (isset($login)) {
$password = md5($formPass);
$username = $formUser;
if (checkAuth($username, $password)) {
setcookie ('cuser', $username,time()+3600, '/','yourhost.com');
setcookie ('cpass', $password,time()+3600, '/','yourhost.com');
$msg = "Authentication was successful! The cookies have been sent!";
}else{
$msg = "Authentication error!";
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<?php
if (isset($login)) {
echo("<meta http-equiv='refresh'
content='5;url=cookie.php'>");
}
?>
<title>Test Cookie</title>
</head>
<body>
<?php
if (isset($cuser) && !isset($relogin)) {
if (checkAuth($cuser, $cpass)) {
echo("<p>Hello $cuser!</p>" .
"<p>Your password (md5 encrypted) is: $cpass</p>" .
"<p><a href='cookie.php?relogin=1'>Delete cookies and force
login<a></p>");
} else {
echo("<p>Authentication error from cookies values</p>" .
"<p><a href='cookie.php?relogin=1'>Delete cookies and force
login<a></p>");
}
} else {
if (!isset($login) || isset($relogin)) {
?>
<form action="cookie.php?login=1" method="POST">
<p>User: <input type="text" name="formUser"></p>
<p>Pass: <input type="password" name="formPass"></p>
<p><input type="submit" name="submit" value="Log
In"></p>
</form>
<?php
} elseif (isset($login)) {
echo("<p>$msg</p>" .
"<p>You'll be redirected in 5 seconds!</p>");
}
}
?>
</body>
</html>