PHP $_SESSION在某些浏览器中工作,而不是在其他浏览器中


PHP $_SESSION working in some browsers, not others

我有一个使用PHP $_SESSION变量的网页。它在我的电脑上使用谷歌Chrome(版本44.0.2403.157(64位))工作得很好,但它不适用于其他浏览器或其他版本的Chrome。

我能做些什么来解决这个问题?我更喜欢如果我能继续使用$SESSION变量,所以我不必重新编码我所有的网页,但如果我必须,什么是替代方案?

对于上下文:我使用$_SESSION变量来存储信息,例如谁"登录"到我的站点的身份和用户的"购物车"中的产品。

代码:我像这样开始一个会话:

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. 
}

就像我之前说的,它在某些浏览器中工作良好。有些东西阻止了它在别人身上起作用。

通过"工作",我的意思是浏览器允许使用$SESSION变量。我认为不是表示跨浏览器保存的变量。

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

下面是我的代码的一个小例子。在这里,当按下login按钮时,它会检查登录凭据。
<?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()是我的第一个语句。