Setting Up Cookie Authentication
<?php
session_start ();
?>
<html>
<title></title>
<?php
$GLOBALS ['user'] = "test";
$GLOBALS ['pass'] = "test";
if (isset ( $_POST ['user'] ) && isset ( $_POST ['pass'] )) {
if (strcmp ( $_POST ['user'], $GLOBALS ['user'] ) == 0. && strcmp ( $_POST ['pass'], $GLOBALS ['pass'] ) == 0) {
$_SESSION ['user'] = $_POST ['user'];
$_SESSION ['pass'] = $_POST ['pass'];
} else {
?>
Sorry, you have entered an incorrect login.
<?php
}
}
if ($_POST ['logout'] == "yes") {
unset ( $_SESSION ['user'] );
unset ( $_SESSION ['pass'] );
session_destroy ();
}
function checkcookies() {
if (strcmp ( $_SESSION ['user'], $GLOBALS ['user'] ) == 0. && strcmp ( $_SESSION ['pass'], $GLOBALS ['pass'] ) == 0) {
return true;
} else {
return false;
}
}
?>
</head>
<body>
<?php
if (checkcookies ()) {
?>
<p>you are logged in!</p>
<form action="index.html" method="post">
<input type="hidden" name="logout" value="yes" />
<input type="submit" value="Logout" /></form>
<?php
//Or else present a login form.
} else {
?>
<form action="index.html" method="post">Username:<input type="text"
name="user" maxlength="25" /> Password:<input type="password"
name="pass" maxlength="25" /> <input type="submit" value="Login" />
</form>
<?php
}
?>
</body>
</html>
Related examples in the same category