php中页面重定向后会话丢失
当我使用php头重定向时,所有会话变量都会丢失。。。有人说添加exit();就在标题(“”)之后;将解决问题,但似乎不是解决方案 有人能帮忙吗 以下是如何将变量存储到会话中:php中页面重定向后会话丢失,php,session,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']
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';
}
您需要放入exit()
在标题重定向之后,否则您只需将两页内容加载到一页中
还要确保您有
session_start()代码>位于所有脚本的顶部。您没有启动会话。为了使用会话变量并使它们在页面中传递,您需要
session_start();
在每一页的顶部,在任何其他内容之前。简单!确保您所访问的页面(例如www.example.com)在开始时重定向到(例如www.example.com/redirect.php)通知www。如果你在一页又一页地更改,那么肯定会有问题。这些课程有时并不总是像我们预期的那样有效。我的网站在使用丢失的会话时也遇到了类似的问题。我基本上解决了这个问题,在第一次加载页面时,将会话中要保留的值注入到隐藏的文本字段中。然后,第二次调用页面(page submit)时,我只需从隐藏的文本字段中读取值,然后继续执行其余代码
在这种情况下,这比使用会话更简单、更干净 我试图使用以下方法设置自己的会话id:
session_id('own_generated_session_id_string');
但正如医生所说,你必须在使用之前使用这个
session_start();
在会话_start()之后使用它,清除会话参数。退出;应该放在头重定向或会话重新生成id(true)之后;可以使用您只需要检查/var/lib/php目录中的文件权限
将yje公共权限授予/var/lib/php/session目录
全部完成。请显示一些代码和您要重定向到的示例地址。您能给我们显示一些代码吗?用户pc上是否启用了Cookie?您是否在会话中使用cookies,或者您总是将会话id作为get参数发送,但在重定向时忘记了这一点?添加了所有必要的代码…我在其他几台计算机上测试了它,它工作正常。。。找不到任何解释…我添加了exit();但它不起作用!不幸的是,由于站点结构的原因,我不得不使用重定向…这里的键是session_start();在你所有页面的顶端,它也是非常不安全的。某些会话数据仅与服务器相关,不应向客户端公开。与JoSo商定,会话比viewstate更干净,会话数据不被视为viewstate数据…隐藏的文本字段是什么意思…?无法通过查看html源代码检查的文本化?相同的问题,标题重定向从一页到另一页都在变化,我将其切换到整个项目中使用的通用方案,瞧,它起了作用。。至少有一个星期我都在为这事生气。。。谢谢你,你是救世主。。。