Setting Up Cookie Authentication : _SESSION « Cookie Session « PHP






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

1.Count Visits with session
2.Counting page accesses with a session
3.Implementing Sessions
4.Printing session data
5.Registering a variable by including it in $_SESSION
6.Session based counter
7.Session preferences
8.Session using the proper $_SESSION super global
9.Verifying session info
10.check for a valid login in sessions
11.create_session_variable.php
12.delete_session_variable.php
13.Doing something special for a logged in user