PHP会话变量——消失并重新出现

时间:2022-09-20 12:17:45

I have a template file that contains all my header, footer and common information. It includes the appropriate content for the current page (two-step view pattern).

我有一个包含所有页眉、页脚和普通信息的模板文件。它包括当前页面的适当内容(两步视图模式)。

I am trying to set up a login system using PHP Session variables. I can set the variable and sometimes they work but sometimes they disappear. Clicking on links will sometimes make them come back.

我正在尝试使用PHP会话变量建立一个登录系统。我可以设置变量,有时它们可以工作,但有时它们会消失。点击链接有时会让他们回来。

My site

我的网站

Login with

登录与

username: test password: test

用户名:密码:试验

There are var_dumps of session_id and $_SESSION at the top.

顶部有session_id和$_SESSION的var_dumps。

Click on Home. If the session variables disappear click on home (might take as many as 10 times) to see the session information come back. Click on the other navigation and sometimes the session info sticks around and sometimes it doesn't.

点击回家。如果会话变量消失,单击home(可能需要10次)查看会话信息返回。单击其他导航,有时会话信息会停留在周围,有时则不会。

Here is the session code at the top of my template file.

下面是模板文件顶部的会话代码。

<?php
session_start();

require './classes/DBInterface.php';
$db = new DBInterface();

if($_REQUEST['submit']  ==  'Login') {
    $username=$_POST['username'];
    $password=$_POST['password'];

    echo '-- login -- '.$username;
    $rs = $db->verify($username,$password,"admin",0);
    $admin = $rs->current();
    if ($rs->valid()) {
        $_SESSION['username'] = $username;
    }
}

echo ' -- session id -- ';
var_dump(session_id());
echo ' -- session var -- ';
var_dump($_SESSION);

I am using PHP5.

我使用PHP5。

3 个解决方案

#1


4  

If you are using startlogic (seem you are ?) for your hosting, did you try doing what they say in their FAQ : http://www.startlogic.com/knowledgebase/read_article.bml?kbid=600

如果你正在使用startlogic(似乎是这样的)作为你的主机,你是否尝试过按照他们在FAQ中说的做:http://www.startlogic.com/knowledgebase/read_article.bml?

They indicate this :

他们表示:

To run PHP sessions, include the following code at the top of any PHP script that uses sessions: session_save_path("your home directory path"/cgi-bin/tmp); session_start();

要运行PHP会话,请在任何使用会话的PHP脚本的顶部包含以下代码:session_save_path(“您的主目录路径”/cgi-bin/tmp);session_start();

Maybe this'll help ? Especially if they are using some kind of load balancer, which balances /tmp, but not your home directory ?

也许这将帮助吗?特别是如果他们使用某种负载均衡器,它平衡/tmp,而不是您的主目录?

#2


2  

If you are using a load-balanced setup, it could be that only 1 of the N servers has the correct session-data.

如果您使用的是负载平衡设置,那么可能只有N个服务器中的1个具有正确的会话数据。

By default session-data is stored on the filesystem.
Per session a file is stored in /tmp/ and starts with "sess" followed by the session_id

默认情况下,会话数据存储在文件系统中。每个会话文件存储在/tmp/中,以“sess”开头,然后是session_id。

#3


1  

You're absolutely positive there isn't anything ever being called prior to this? I know session_start() modifies the headers, and other than that, not sure why this isn't working.

你绝对肯定在这之前没有任何事情被调用过吗?我知道session_start()修改了header,除此之外,我不知道为什么它不能工作。

Go ahead and turn on error reporting at the top of the script, right before the session_start() call, and see how that helps track this down:

继续并在脚本的顶部打开错误报告,就在session_start()调用之前,看看这是如何帮助跟踪的:

error_reporting(E_ALL ^ E_NOTICE);
ini_set('display_errors',1);
session_start()

#1


4  

If you are using startlogic (seem you are ?) for your hosting, did you try doing what they say in their FAQ : http://www.startlogic.com/knowledgebase/read_article.bml?kbid=600

如果你正在使用startlogic(似乎是这样的)作为你的主机,你是否尝试过按照他们在FAQ中说的做:http://www.startlogic.com/knowledgebase/read_article.bml?

They indicate this :

他们表示:

To run PHP sessions, include the following code at the top of any PHP script that uses sessions: session_save_path("your home directory path"/cgi-bin/tmp); session_start();

要运行PHP会话,请在任何使用会话的PHP脚本的顶部包含以下代码:session_save_path(“您的主目录路径”/cgi-bin/tmp);session_start();

Maybe this'll help ? Especially if they are using some kind of load balancer, which balances /tmp, but not your home directory ?

也许这将帮助吗?特别是如果他们使用某种负载均衡器,它平衡/tmp,而不是您的主目录?

#2


2  

If you are using a load-balanced setup, it could be that only 1 of the N servers has the correct session-data.

如果您使用的是负载平衡设置,那么可能只有N个服务器中的1个具有正确的会话数据。

By default session-data is stored on the filesystem.
Per session a file is stored in /tmp/ and starts with "sess" followed by the session_id

默认情况下,会话数据存储在文件系统中。每个会话文件存储在/tmp/中,以“sess”开头,然后是session_id。

#3


1  

You're absolutely positive there isn't anything ever being called prior to this? I know session_start() modifies the headers, and other than that, not sure why this isn't working.

你绝对肯定在这之前没有任何事情被调用过吗?我知道session_start()修改了header,除此之外,我不知道为什么它不能工作。

Go ahead and turn on error reporting at the top of the script, right before the session_start() call, and see how that helps track this down:

继续并在脚本的顶部打开错误报告,就在session_start()调用之前,看看这是如何帮助跟踪的:

error_reporting(E_ALL ^ E_NOTICE);
ini_set('display_errors',1);
session_start()