PHP $ _SESSION变量未在页面

时间:2015-05-08 16:35:52

标签: php session-variables

我正在开展一个学校项目,我需要将.php页面进行通信。我有header.php,我在其中设置与数据库的连接并启动会话。为了只启动一次会话,我使用了这个:

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

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

我试图在页面之间传递一些$ _SESSION变量,但是当我尝试在没有设置它们的页面上使用它们时它们一直未设置。 我看到很多人抱怨这个,但我仍然找不到解决方案。

登录-form.php的

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

的login.php

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

有一段不适合我的代码片段。 感谢

4 个答案:

答案 0 :(得分:0)

你可以试试这个。

在你的函数文件中放这个

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),这样您就可以确定它在何时重置,然后知道如何处理它。

答案 1 :(得分:0)

可能未设置变量,因为您没有使用session_start()激活会话变量。

session_id() == ''不是正确的条件。改为使用:

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

如果您已启动会话,则可以设置会话变量

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

在拨打$ _SESSION [&#34; login-error&#34;]之前,请输入session_start(),仅用于测试,以查找会话信号丢失的时间。你说

PHP $ _SESSION变量未在页面之间传递

session_start()和SESSION变量需要包含在每个页面的开头或每个页面开头调用SESSION变量的地方(通过公共文件,bootstrap,config或sth)。即需要从服务器读取这些数据的命令。

答案 2 :(得分:0)

由于我的header.php文件包含&#34; connection.php&#34;文件,我把

session_start();

在connection.php的开头,并从header.php文件中删除它。现在它工作正常。谢谢大家的帮助!

答案 3 :(得分:0)

PHP会话依赖于HTTP的组件,例如Cookie和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会话都会这样做,除了生成随机缓存密钥[又名PHPSESSID]并设置为cookie,缓存目录为session.save_path php.ini

相关问题