<?php

    if ($_POST)                                         #if 2nd or subsequent time
    {                                           
        validate();                                     #validate entry of user & pswd  

        if (! $msg)                                     #if no errors
        {
            save_cookie();                                                    #save the login info in cookie
            header("Location: /~sultans/php/demo/5session/cookieStore.php");  #redirect to another page 
        }                                                                     #absolute path so it works with PHP*Tester
    }                                                     

    display(); 

//=============================================================================
// Display the form
//=============================================================================
    function display()
    {
        global $user, $pswd, $save, $msg;   
?>
        <html>
        <head>
        <title>Cookies Set</title>
        </head>
        <body bgcolor=lightyellow>
        <h1>Please Login</h1>
        <form method=post name=frm action=<?php echo $_SERVER['PHP_SELF'] ?> >
        <fieldset style='width:340px'>
        <table>
        <tr><td>Enter user name  <td><input type=text     name=user> <i>(any)</i>
        <tr><td>Enter password   <td><input type=password name=pswd> <i>(any)</i>
        <tr><td>Save login info? <td><input type=checkbox name=save value=Y>
        </table>
        </fieldset>
        <br>
        <input type=submit value="Login" onClick="return pre_validate()">
        </form>
        <div style="color:red;"> <?php echo $msg ?> </div>
        
        <script>                                //javascript validation
        function pre_validate()                 //JS pre-validate function      
        {
            user = document.frm.user.value;     //get entry values
            pswd = document.frm.pswd.value;
            
            if (! user || ! pswd)               //if no user or no password
            {  
                alert('Please enter any user name and password');
                return(false);
            }
        }
        </script>

<?php
    }

//=============================================================================
// Validate all required input fields
//=============================================================================
    function validate()
    {
        global $user, $pswd, $save, $msg;   

        $user = $_POST[user];                           #get HTML form entry fields 
        $pswd = $_POST[pswd];
        $save = $_POST[save];

        if (! $pswd) 
            $msg  = 'Please enter your password!';     
        if (! $user) 
            $msg  = 'Please enter your user name!';     
    }

//=============================================================================
// Save cookie with or without expiratioin date
//=============================================================================
    function save_cookie()
    {
        global $user, $pswd, $save, $msg;   

        $expiration = time() + 1 * 24*60*60;    //compute 1 day expiration in seconds 

        if ($user && $pswd)                     //if logged in
        {
            if ($save)                          //save with future expiration?
            {
                setcookie(userName, $user, $expiration, '/');   
                setcookie(password, $pswd, $expiration, '/');
            }   
            else                                //save with 0 expiration time
            {
                setcookie(userName, $user, 0, '/');
                setcookie(password, $pswd, 0, '/');
            }
        }
    }
    
//=============================================================================
?>

<?php include "../include.php"; ?>              <!-- hyperlink to see the code -->
</body>
</html>