簡體   English   中英

PHP $ _SESSION在某些瀏覽器中工作,而在其他瀏覽器中工作

[英]PHP $_SESSION working in some browsers, not others

我有一個使用PHP $_SESSION變量的網頁。 在使用Google 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和本地存儲。

這是我的代碼的一個小例子。 在這里,當按下登錄按鈕時,它將檢查登錄憑據。

<?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()函數。 這在某些瀏覽器上效果很好,但在其他瀏覽器上卻沒有。

我進行了更改,因此session_start()是我的第一條聲明。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM