PHP $_SESSION 在某些浏览器中有效,在其他浏览器中无效

PHP $_SESSION working in some browsers, not others

我有一个使用 PHP $_SESSION 变量的网页。它在我的计算机上使用 Google Chrome(版本 44.0.2403.157(64 位))运行良好,但不适用于其他浏览器或其他版本的 Chrome。

我该怎么做才能解决这个问题?我更愿意继续使用 $SESSION 变量,这样我就不必重新编码我的所有网页,但如果必须的话,有什么替代方案?

对于上下文:我使用 $_SESSION 变量来存储信息,例如谁是 "logged in" 我的网站的身份以及用户 "shopping cart" 中的产品。

代码:我这样开始会话:

function sec_session_start() {
    $session_name = 'sec_session_id';   // Set a custom session name
    $secure = false;
    // This stops JavaScript being able to access the session id.
    $httponly = true;
    // Forces sessions to only use cookies.
    if (ini_set('session.use_only_cookies', 1) === FALSE) {
        header("Location: ../error.php?err=Could not initiate a safe session (ini_set)");
        exit();
    }
    // Gets current cookies params.
    $cookieParams = session_get_cookie_params();
    session_set_cookie_params($cookieParams["lifetime"],
        $cookieParams["path"], 
        $cookieParams["domain"], 
        $secure,
        $httponly);
    // Sets the session name to the one set above.
    session_name($session_name);
    session_start();            // Start the PHP session 
    session_regenerate_id(true);    // regenerated the session, delete the old one. 
}

正如我之前所说,它在某些浏览器中运行良好。有些东西阻止它在其他人身上发挥作用。

"working" 是指浏览器允许使用 $SESSION 变量。我 不是 意思是跨浏览器保存变量。

当我检查浏览器的 cookie 时它不起作用,它说它正在为我的网站存储缓存、cookie 和本地存储。

这是我的代码的一个小例子。在这里,当按下登录按钮时,它会检查登录凭据。

<?php
/**
* 
*
*/


                        include_once 'db-credentials.php';  //get database credentials 
                        $mydb2= logindb(); //login to database

                        sec_session_start(); //start session




//process form data                        
if(isset($_POST['btn-login'])) //if login button was pressed
{

 $email = $_POST['email'];
 $upass = $_POST['pwd'];
 $row = $mydb2->get_row($mydb2->prepare( 
        "select * from users WHERE email='$email'"), ARRAY_A
        );
 if($row['password']==$upass)
 {
  $_SESSION['user'] = $row['user_id'];
  $_SESSION['name'] = $row['username'];
  echo "<script>window.location = 'http://mywebsite.ca/order/'</script>";
 }
 else
 {
  ?>
        <script>alert('Invalid login. Please check your email and password and try again');</script>
        <?php
 }

}

现在,该代码 运行 没问题。使用正确的用户名和密码,程序进入内部 if 语句并将 运行 echo "<script>window.location = 'http://mywebsite.ca/order/'</script>"; 语句。

但是,当它到达 http://mywebsite.ca/order/ 时,它不再保存会话变量!

我明白了。之前,我在调用 session_start() 函数之前调用了 get_header() 函数。这在某些浏览器上运行良好,但在其他浏览器上运行良好。

我更改了它,所以 session_start() 是我的第一个声明。