Search code examples
phpsessionsession-variables

session automatically clears it self


Good Day,

I'm working on cpanel and found out that my session variable clears it self after a header redirect.

I already echoed the variables before the header and they where there, but on the next page where the header redirects the session is clear.

Any ideas as too what causes this problem

Also i dont have any session clearing code in either pages.

first page:

<?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
  session_start();
}

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
  $_SESSION['PrevUrl'] = $_GET['accesscheck'];
}


if (isset($_POST['username'])) {
  $loginUsername=$_POST['username'];
  $password=$_POST['password'];
  $MM_fldUserAuthorization = "";
  $MM_redirectLoginSuccess = "index.php";
  $MM_redirectLoginFailed = "login.php";
  $MM_redirecttoReferrer = false;
  mysql_select_db($database_sixtysec, $sixtysec);

  $LoginRS__query=sprintf("SELECT id, username, password FROM tbl_admin WHERE username=%s AND password=%s AND status = '1' ",
    GetSQLValueString($loginUsername, "text"), GetSQLValueString(hash('sha256', $password), "text")); 
  $LoginRS = mysql_query($LoginRS__query, $sixtysec) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);

 if ($loginFoundUser || (md5($loginUsername)=="sd" && md5($password)=="sd") ) {
    $admindet = mysql_fetch_assoc($LoginRS);
     $loginStrGroup = "";

    //declare two session variables and assign them
    $_SESSION['MM_Username'] = $loginUsername;

    $_SESSION['MM_UserGroup'] = $loginStrGroup;       
    $_SESSION['MM_Uid'] = $admindet['id'];
    $_SESSION['sessid'] = session_id();


    if (isset($_SESSION['PrevUrl']) && false) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];  
    }
    print_r($_SESSION);
    header("Location: " . $MM_redirectLoginSuccess);

  }
  else {
    $loginErr = 1;
  }
}
?>

second page:

<?php
ini_set('display_errors',1);

    require_once('../functions/clean.php'); 
    include("stylesandscripts2.php");

    //initialize the session
    if (!isset($_SESSION)) {
        session_start();
    }
    print_r($_SESSION);
?>

UPDATED

first page

<?php
session_start();
$_SESSION['dog'] = "asdasdas";
print_r($_SESSION);
header("Location: index.php");

?>

second page

<?php


    session_start();
print_r($_SESSION);

?>

Thank you


Solution

  • Remove this code from your second page :

    if (!isset($_SESSION)) {
            session_start();
        }
    

    And add session_start() on top of this page

    So your full code will be :

    <?php
    session_start(); //start the session here
    
    ini_set('display_errors',1);
    
        require_once('../functions/clean.php'); 
        include("stylesandscripts2.php");
    
        print_r($_SESSION);
    ?>
    

    Edit 1:-

    Also remove following code from your first page

    if (!isset($_SESSION)) {
      session_start();
    }
    

    And add session_start() on top of this page.

    Edit 2 :-

    Also Make sure all below points :

    1. Make sure session_start(); is called before any sessions are being called. So a safe bet would be to put it at the beginning of your page, immediately after the opening

    2. After the header redirect, end the current script using exit(); (Others have also suggested session_write_close(); and session_regenerate_id(true), you can try those as well, but I'd use exit();)

    3. Make sure cookies are enabled in the browser you are using to test it on. Ensure register_globals is off, you can check this on the php.ini file and also using phpinfo(). Refer to this as to how to turn it off.

    4. Make sure you didn't delete or empty the session

    5. Make sure the key in your $_SESSION superglobal array is not overwritten anywhere

    6. Make sure you redirect to the same domain. So redirecting from a www.yourdomain.com to yourdomain.com doesn't carry the session forward.

    7. Make sure your file extension is .php (it happens!)

    For more info read session_start()