【发布时间】:2017-12-03 15:07:57
【问题描述】:
在使用以下代码维护会话 ID 时,我丢失了会话变量。
index_simplified.html
<!DOCTYPE HTML>
<html>
<body>
<form action="processLogin_simplified.php" method="post">
username:
<input type="text" class="textfield" name="username">
<input type="submit" name="login" value="Login">
</form>
</body>
</html>
processLogin_simplified.php
<?php
session_start();
$_SESSION['username'] = $_POST['username'];
session_write_close(); // tried with and without this line
header('Refresh: 0; URL=https://www.domainname.com/foldername/dashboard_simplified.php');
exit(); // tried with and without this line
?>
dashboard_simplified.php
<?php
session_start();
echo 'session id:' . session_id();
echo '<br><br>username: ' . $_SESSION['username'];
?>
输出显示会话 ID,但没有显示用户名会话变量。
值得注意的是,在我通过服务器公司购买 SSL 并将重定向代码添加到包含这些文件的文件夹的 htaccess 文件之前,我的完整代码一直有效。使用的代码是:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.domainname.com/foldername/$1 [R,L]
这成功地将使用 http 访问该位置的用户重定向到使用 https 的同一位置。我还尝试添加 RewriteCond %{HTTPS} off 以确保在用户最初使用 https 时不会发生重定向,但这没有效果。
是否有解决方案使我的会话变量持续存在?
【问题讨论】:
标签: php .htaccess session redirect session-variables