Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php sessions being lost

I do the following to set my session, this works because the echo appears. but when I go to the next page or another the session is not there? what am I doing wrong?

$session_start();

if ($username==$dbusername&&$password==$dbpassword)
    {
        echo"<b>Login Successful</b><br><a href='systemadmin.html'><br>Click here to access the <strong>System Admin Page</strong></a>";
        $_session['username']=$dbusername;
        if($username == "admin")
        {
            $_session['admin'] = true;
        }

I am trying to get the following to work with these sessions:

<?php
session_start();
if($_session['admin'] == true)
{
// do nothing
}else{
    header( 'Location: home.html' ) ;
}

?>

Update:

the uppercase sessions work but now the sessions arent destroying when i use the logout.php

<?php

session_start();

session_destroy();

header("location: home.html");

?>
like image 755
Anicho Avatar asked Jun 04 '26 01:06

Anicho


1 Answers

$_session should be => $_SESSION.

http://php.net/manual/en/reserved.variables.session.php

The first works because you are setting a 'normal' variable (which is available for the request).

UPDATE

To destroy the session:

<?php
// Initialize the session.
// If you are using session_name("something"), don't forget it now!
session_start();

// Unset all of the session variables.
$_SESSION = array();

// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (ini_get("session.use_cookies")) {
    $params = session_get_cookie_params();
    setcookie(session_name(), '', time() - 42000,
        $params["path"], $params["domain"],
        $params["secure"], $params["httponly"]
    );
}

// Finally, destroy the session.
session_destroy();
?>

http://php.net/manual/en/function.session-destroy.php#example-4368

Additionaly you should always use exit(); after you do a redirect to prevent further execution of the script.

like image 142
PeeHaa Avatar answered Jun 05 '26 18:06

PeeHaa