Implementing Sessions : _SESSION « Cookie Session « PHP






Implementing Sessions

 
<?php
session_start ();
$GLOBALS ['user'] = "test";
$GLOBALS ['pass'] = "test";

function login($username, $password) {
  if (strcmp ( $username, $GLOBALS ['user'] ) == 0 && strcmp ( $password, $GLOBALS ['pass'] ) == 0) {
    $_SESSION ['user'] = $username;
    $_SESSION ['pass'] = md5 ( $password );
    return true;
  
  } else {
    return false;
  }
}
function logout() {
  unset ( $_SESSION ['user'] );
  unset ( $_SESSION ['pass'] );
  session_destroy ();

}
if (login ( "test", "test" )) {
  echo "Successfully logged in with user: " . $_SESSION ['user'] . " and pass: " . $_SESSION ['pass'];
} else {
  echo "Could not login.";
}
logout ();
if (isset ( $_SESSION ['user'] )) {
  echo $_SESSION ['user']; //Outputs nothing. 
}
?>
  
  








Related examples in the same category

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