douji8549 2014-07-29 05:03
浏览 46
已采纳

会话自动清除它自己

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

  • 写回答

1条回答 默认 最新

  • douluan4644 2014-07-29 05:25
    关注

    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()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数