<?
    $aUserDatabase
["digiways"] = md5("cool");
    
session_start(); // starting session
    // session variables must be global
    
global $strName$hashPassword$sessData;
    
// registering session variables
    
session_register("strName");
    
session_register("hashPassword");
    
session_register("sessData");
    
    
// checking if user is not authenticated
    
if (!isset($strName) || $aUserDatabase[$strName] != $hashPassword)
    {
        
// if not, checking if he just authenticated
        // but we haven't processed that information yet
        
global $HTTP_POST_VARS;
        if (isset(
$HTTP_POST_VARS["form_username"]))
        {
            
$strName $HTTP_POST_VARS["form_username"];
            
$hashPassword md5($HTTP_POST_VARS["form_password"]);
            
/* Using HTTP redirect to make user's web browser refresh this page.
            Otherwise, if user refreshes this page later, browser will ask him
            if he want's to resubmit form data, etc.
            Also PHP variable SID contains session id if cookie is not set.
            */
            
header("Location: http://www.digiways.com/articles/php/sessauth/login.php?".SID);
            exit;
        }
?>
Login: digiways<br>
Password: cool<br>
<form method="post">
Username: <input type="text" name="form_username"><br>
Password: <input type="password" name="form_password"><br>
<input type="submit" value="Submit">
</form>
<?    exit;
    }
?>
    Here's the part where user can get only after authorization.<br>
    Let's set some session variables to share. For instance,
<?
    
// this way we store the time when user authorized in $sessData["time"]
    
if (!isset($sessData["time"]))
        
$sessData["time"] = time();
        
    echo 
"You authorized at ".date("D M j G:i:s T Y"$sessData["time"])."<br>";
?>
    Links to other pages which will share this session.<br>
    <a href="anotherpage.php?<?=SID;?>">anotherpage.php</a><br>
    <a href="logout.php?<?=SID;?>">logout.php</a>