在PHP中页面重定向后会话丢失

时间:2010-06-11 13:59:03

标签: php session

当我使用php头重定向时,所有会话变量都丢失了......有人说添加exit();在标题之后(“”);将解决问题,但它似乎不是解决方案......

有人可以帮忙吗?

以下是我如何将变量存储到会话中:

include 'dbc.php';

$err = array();

foreach($_GET as $key => $value) {
    $get[$key] = filter($value); //get variables are filtered.
}

if ($_POST['doLogin']=='Login')
{

foreach($_POST as $key => $value) {
    $data[$key] = filter($value); // post variables are filtered
}


$user_email = $data['usr_email'];
$pass = $data['pwd'];


if (strpos($user_email,'@') === false) {
    $user_cond = "user_name='$user_email'";
} else {
      $user_cond = "user_email='$user_email'";

}


$result = mysql_query("SELECT `id`,`pwd`,`full_name`,`approved`,`user_level` FROM users WHERE 
           $user_cond
            AND `banned` = '0'
            ") or die (mysql_error()); 
$num = mysql_num_rows($result);

  // Match row found with more than 1 results  - the user is authenticated. 
    if ( $num > 0 ) { 

    list($id,$pwd,$full_name,$approved,$user_level) = mysql_fetch_row($result);

    if(!$approved) {
    //$msg = urlencode("Account not activated. Please check your email for activation code");
    $err[] = "Account not activated. Please check your email for activation code";

    //header("Location: login.php?msg=$msg");
     //exit();
     }

        //check against salt
    if ($pwd === PwdHash($pass,substr($pwd,0,9))) { 
     // this sets session and logs user in  
       session_start();
       session_regenerate_id (true); //prevent against session fixation attacks.

       // this sets variables in the session 
        $_SESSION['user_id']= $id;  
        $_SESSION['user_name'] = $full_name;
        $_SESSION['user_level'] = $user_level;
        $_SESSION['HTTP_USER_AGENT'] = md5($_SERVER['HTTP_USER_AGENT']);

        //update the timestamp and key for cookie
        $stamp = time();
        $ckey = GenKey();
        mysql_query("update users set `ctime`='$stamp', `ckey` = '$ckey' where id='$id'") or die(mysql_error());

        //set a cookie 

       if(isset($_POST['remember'])){
                  setcookie("user_id", $_SESSION['user_id'], time()+60*60*24*COOKIE_TIME_OUT, "/");
                  setcookie("user_key", sha1($ckey), time()+60*60*24*COOKIE_TIME_OUT, "/");
                  setcookie("user_name",$_SESSION['user_name'], time()+60*60*24*COOKIE_TIME_OUT, "/");
                   }
        if(empty($err)){            
          header("Location: myaccount.php");
         }
        }
        else
        {
        //$msg = urlencode("Invalid Login. Please try again with correct user email and password. ");
        $err[] = "Invalid Login. Please try again with correct user email and password.";
        //header("Location: login.php?msg=$msg");
        }
    } else {
        $err[] = "Error - Invalid login. No such user exists";
      }     
}

重定向代码:

//connect database
    require_once 'dbc.php';

    page_protect();

    $authorID = $_SESSION['user_id'];
    if ( !empty($_POST["answ_content"]) && $authorID != 0 ) {
            //vaqciot html chveulebriv texad
            $content = htmlentities($_POST["answ_content"],ENT_COMPAT,'UTF-8');
            $dro = date('Y-m-d H:i:s');
            $qID = $_POST["question_ID"];
            $author = $_SESSION["user_name"];


            $sql="INSERT INTO wp_comments (comment_ID, comment_post_ID, comment_author, comment_author_IP, comment_date, comment_content, user_id) 
                VALUES
              (NULL, '$qID', '$author', '123.123.123.123', '$dro', '$content', '$authorID')";

            $result = mysql_query($sql);

            //pasuxebis raodenobis ertit gazrda
            $increase = "UPDATE wp_posts SET comment_count = comment_count+1 WHERE ID = $qID";
            mysql_query($increase);

            //gadamisamarteba shekitxvis gverdze  
            $url = 'Location:http://example.com/site/answ/question.php?ID=' .$qID;
            header($url);
    } else {
        echo 'error';
    }

7 个答案:

答案 0 :(得分:11)

您需要在标题重定向后放置exit();,否则您刚刚将两页内容加载到一页。

另外,请确保所有脚本的顶部都有session_start();

答案 1 :(得分:4)

您没有开始会话。为了使用会话变量并让它们跨页面,你需要把

session_start();

位于每页顶部之前。

答案 2 :(得分:2)

我试图使用以下方式设置我自己的会话ID:

session_id('own_generated_session_id_string');

但正如documentation所说,你必须在

之前使用它
session_start();

在session_start()之后使用它,清除会话参数。

答案 3 :(得分:1)

Simples!确保您来自的网页(例如www.example.com)在开头重定向到(例如www.example.com/redirect.php)通知www。如果你在页面之间改变它,那么是的事情变得很糟糕。

答案 4 :(得分:0)

这些会议并不总是像我们期望的那样有效。我的网站使用会丢失的会话也遇到了类似的问题。我基本上通过在第一次加载页面时将我希望保留在会话上的值注入隐藏文本字段来解决它。然后我第二次调用页面(页面提交)我只是从隐藏文本字段中读取值并继续我的其余代码。

在这种情况下,这比使用会话更容易和更清洁!

答案 5 :(得分:0)

出口;应该在头重定向或session_regenerate_id(true)之后放置;可以使用

答案 6 :(得分:0)

您只需要检查 / var / lib / php 目录中的文件权限 将yje公开发表给 / var / lib / php / session 目录。

并且全部完成。