Avatar of swendell
swendell

asked on 

PHP Session variable not available from page redirected to

I confirmed the username session variable has been been successfully set on the eis.php page.
When I return to the eis.php page after the variable has been set; I should be redirected to newxhtml.xhtm by the Loggedin() Function called at the top of the page but I am not.

The loggedin() function is located in the included eisfunctions.php file. When I check the username session variable on that page; it has no value....  

Here is eis.php:
<?php
// This is the EIS LOG In Page
include 'eisfunctions.php';  
session_start();
// If we are already logged in there is no need to re-login, so lets go right to the site
//var_dump(loggedin());
//echo "xxx";
//die('exit');
if (loggedin())
    { 
    // die("Point 1");  
    header("Location: newxhtml.xhtml");
    exit();
    }

// The form's Action Posts the form's fields back to this page, causing this script to run 
if(isset($_POST['login']))
{
//    die("Point 1");  
//    
// Here we store the form element's names in variables    
$username = filter_input(INPUT_POST, 'username');
$password = filter_input(INPUT_POST, 'password');
$rememberme = filter_input(INPUT_POST, 'rememberme');
  

if ($username&&$password)
    {
    if (md5($password) == "Deleted for experts exhange post")
        {$loginok = TRUE;}
    else
        {$loginok = FALSE;}
            // die("Point 1 LoginOK = $loginok");
        if ($loginok==True)   
        {
            // When logging in if remember me is checked we want to create a cookie on the client machine
            // The setcookie() function defines a cookie to be sent along with the rest of the HTTP headers
            // We need to set the first 3 paramters. setcookie(name,value,expire,path,domain,secure,httponly);
            if($rememberme=="on")
            setcookie("username",$username, time()+7200);    
            else if ($remeberme=="")
            $_SESSION["username"]=$username;
            //die("Point 1 $username ");
            // echo "Session username is " . $_SESSION["username"] . "<br>";
            header("location: newxhtml.xhtml");
            exit();
        }
        else 
        {
            die("Incorrect username / password combination");
        }            
    }
else
    {
    die("Please enter a username and password");
    }
}
?>        

<form action="eis.php" method="POST"> 
    User Name: <br />
    <input type= "text" name="username"> <p />
    Password: <br />
    <input type= "password" name="password"> <p />
    
<!--    The check box element when checked has value 'ON', when unchecked it's value is null-->
    <input type= "checkbox" name="rememberme">  Remember me <p />
    <input type= "submit" name="login" value= "Log In"> <p /> 
</form>

Open in new window


Here is the eisfunctions.php page
<?php
//eis functions 

//login check function
function loggedin()
{
    session_start();
    //echo "Session username from function is: " . $_SESSION["username"] . ".<br>";
    //Has a cookie or session already been established by a prior successful login?
    if (isset($_SESSION['username'])||isset($_COOKIE['username']))
    {
    $loggedin = TRUE;
    return $loggedin;
    }
    else
    {
        //echo "No Cookie or session found with loggedin function";
        //die("Point 1 in eisfunction - No session or cookie"); 
    }
    
}
// WARNING: I had some space after the closing php here. When this file was included in another file; that other file could no longer use header to redirect!!!
// This will cause some literal whitespace to be sent as output, preventing you from making subsequent header calls.
?>

Open in new window

PHP* zend

Avatar of undefined
Last Comment
swendell

8/22/2022 - Mon