dongren9966 2015-08-26 19:12
浏览 175
已采纳

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

I have a webpage that uses PHP $_SESSION variables. It works fine on my computer using Google Chrome (version 44.0.2403.157 (64-bit)), but it doesn't work for other browsers or other versions of Chrome.

What can I do to fix this? I would prefer if I could keep using $SESSION variables so I don't have to recode all my webpages, but if I must, what is an alternative?

For context: I use the $_SESSION variables to store information such as the identity of who is "logged in" to my site and products in a user's "shopping cart".

Code: I start a session like this:

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

Like I said before, it works fine in certain browsers. Something is preventing it from working in others.

By "working", I mean the browser allows for the use of $SESSION variables. I do not mean the variables save across browsers.

When I check the cookies of the browser where it does not work, it says that it is storing cache, cookies, and local storage for my website.

Here a small example of my code. Here, when the login button is pressed, it checks the login credentials.

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

}

Now, that code runs fine. With a correct username and password, the program gets into the inner if statement and will run the echo "<script>window.location = 'http://mywebsite.ca/order/'</script>"; statement.

However, when it gets to http://mywebsite.ca/order/, it no longer has the session variables saved!

  • 写回答

1条回答 默认 最新

  • dptpn06684 2015-08-27 14:18
    关注

    I figured it out. Before, I was calling the get_header() function before I was calling the session_start() function. This worked fine on some browsers but not others.

    I changed it so session_start() is my first statement.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)