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 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助