php中页面重定向后会话丢失

50pmv0ei  于 2023-02-03  发布在  PHP
关注(0)|答案(8)|浏览(174)

当我使用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';
    }
4dc9hkyq

4dc9hkyq1#

你需要把exit();放在你的标题重定向之后,否则你就把两页内容加载到了一页中。
还要确保所有脚本的顶部都有session_start();

7ivaypg9

7ivaypg92#

您没有启动会话。为了使用会话变量并使它们跨页传递,您需要将

session_start();

在每一页的顶部,在其他任何东西之前。

epfja78i

epfja78i3#

我尝试使用以下命令设置自己的会话ID:

session_id('own_generated_session_id_string');

但是正如documentation所说,您必须在之前使用它

session_start();

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

vc9ivgsu

vc9ivgsu4#

很简单!确保你所来自的页面(例如www.example.com)重定向到一个(例如www.example.com/redirect.php),注意在开始的时候www.如果你在页面之间改变,那么是的,事情会变得不稳定。

sbtkgmzw

sbtkgmzw5#

这些会话有时并不总是像我们期望的那样工作。我的网站使用丢失的会话时也遇到过类似的问题。我基本上通过在页面第一次加载时将我想保留在会话上的值注入隐藏文本字段来解决它。然后,我第二次调用页面(页面提交)时,我只是从隐藏文本字段读取值,然后继续我的其余代码。
在这种情况下,这比使用会话更容易、更简洁!

zwghvu4y

zwghvu4y6#

exit;应该放在标头重定向或session_regenerate_id(true)之后;可用于

yebdmbv4

yebdmbv47#

你只需要检查**/var/lib/php目录中的文件权限给予你对/var/lib/php/session**目录的公共权限。
都搞定了。

ztmd8pv5

ztmd8pv58#

  • 包括会话_开始();在会话之前的两个文件中。
  • 注意不要在重定向文件中使用session_destroy()。

相关问题