在保持会话ID的同时丢失会话变量

时间:2017-06-29 19:12:18

标签: php .htaccess session redirect session-variables

我在使用以下代码维护会话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时没有发生重定向,但这没有效果。

是否存在我的会话变量持续存在的解决方案?

1 个答案:

答案 0 :(得分:0)

这是由我的托管公司解决的。显然,&#34;会话.save_pth未在php.ini文件中正确设置&#34;。不完全确定这意味着什么,但我很高兴它现在正在运作。希望它能帮助别人。