簡體   English   中英

PHP $ _SESSION變量未在頁面之間傳遞

[英]PHP $_SESSION variables are not being passed between pages

我在一個學校項目中工作,需要我的.php頁面進行通信。 我有header.php,可在其中設置與數據庫的連接並啟動會話。 為了只啟動一次會話,我使用了以下方法:

if(session_id() == '') {
    session_start();
}

PHP版本是帶有Suhosin-Patch(cli)的PHP 5.3.10-1 ubuntu3.18

我試圖在頁面之間傳遞一些$ _SESSION變量,但是當我嘗試在未設置它們的頁面上使用它們時,它們始終保持未設置狀態。 我看到許多人對此表示抱怨,但仍然找不到解決方案。

登錄-form.php的

    <?php
        if (isset($_SESSION["login-error"])) {
            echo '<p>'.$_SESSION["login-error"].'</p>';
        }   
    ?>

的login.php

 $_SESSION["login-error"]= "Username or password incorrect";

有一段代碼對我不起作用。 謝謝

你可以試試看

在您的功能文件中放入

function is_session_started()
{
    if ( php_sapi_name() !== 'cli' ) {
        if ( version_compare(phpversion(), '5.4.0', '>=') ) {
            return session_status() === PHP_SESSION_ACTIVE ? TRUE : FALSE;
        } else {
            return session_id() === '' ? FALSE : TRUE;
        }
    }
    return FALSE;
}

然后,您可以在要啟動會話的每個頁面中運行此命令

if ( is_session_started() === FALSE ) session_start();

以此為基礎,我認為您應該跨頁面開始會話。 接下來是確保將會話設置為一個值。 如果不確定什么會導致會話var_dump($_SESSION)可以在代碼的不同部分嘗試var_dump($_SESSION) ,以便確定在什么時候復位,然后知道如何處理它。

這些變量可能未設置,因為您尚未使用session_start()激活會話變量。

session_id() == ''不是正確的條件。 改用:

 if (!isset($_SESSION)) { session_start();}

如果您已開始會話,則可以設置會話變量

  if (!isset($_SESSION["login-error"])) { $_SESSION["login-error"]= "Username or password incorrect";}

在調用$ _SESSION [“ login-error”]之前,鍵入session_start() (僅用於測試)以查找缺少會話信號的時間。 你說

PHP $ _SESSION變量未在頁面之間傳遞

session_start()和SESSION變量需要包含在EVERY頁面的開頭,或者在EVERY頁面的開頭被調用SESSION變量的位置(通過通用文件,bootstrap,config或sth)。 即需要從服務器讀取那些數據的命令。

由於我的header.php文件包含“ connection.php”文件,因此我將

session_start();

在connection.php的開頭,並將其從header.php文件中刪除。 現在工作正常。 感謝你的幫助!

PHP會話依賴於HTTP的組件,例如Cookies和GET變量,當您通過CLI調用腳本時,這些組件顯然不可用。 您可以嘗試偽造PHP超全局變量中的條目,但這是完全不建議的。 而是自己實現一個基本的緩存。

<?php
class MyCache implements ArrayAccess {
    protected $cacheDir, $cacheKey, $cacheFile, $cache;

    public function __construct($cacheDir, $cacheKey) {
        if( ! is_dir($cacheDir) ) { throw new Exception('Cache directory does not exist: '.$cacheDir); }
        $this->cacheDir = $cacheDir;
        $this->cacheKey = $cacheKey;
        $this->cacheFile = $this->cacheDir . md5($this->cacheKey) . '.cache';

        // get the cache if there already is one
        if( file_exists($this->cacheFile) ) {
            $this->cache = unserialize(file_get_contents($this->cacheFile));
        } else {
            $this->cache = [];
        }
    }

    // save the cache when the object is destructed
    public function __destruct() {
        file_put_contents($this->cacheFile, serialize($this->cache));
    }

    // ArrayAccess functions
    public function offsetExists($offset) { return isset($this->cache[$offset]); }
    public function offsetGet($offset) { return $this->cache[$offset]; }
    public function offsetSet($offset, $value) { $this->cache[$offset] = $value; }
    public function offsetUnset($offset) { unset($this->cache[$offset]); }
}

$username = exec('whoami');
$c = new MyCache('./cache/', $username);

if( isset($c['foo']) ) {
    printf("Foo is: %s\n", $c['foo']);
} else {
    $c['foo'] = md5(rand());
    printf("Set Foo to %s", $c['foo']);
}

示例運行:

# php cache.php
Set Foo to e4be2bd956fd81f3c78b621c2f4bed47

# php cache.php
Foo is: e4be2bd956fd81f3c78b621c2f4bed47

這幾乎是所有PHP會話的工作,除了會生成一個隨機緩存鍵[aka PHPSESSID]並將其設置為cookie,並且緩存目錄是php.ini session.save_path

暫無
暫無

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

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