doupu1727 2011-06-12 13:53
浏览 41
已采纳

PHP会话可能因fb而无法正常工作? 请帮忙

I have a question about PHP sessions. I use a session to keep a visitor logged in. I have made a site before with this and works perfect. Now I am making a Facebook app.

When logging in (checking if user is registered in database), I register id. After that I use:

if(session_is_registered("id"))
{
echo "Logged in";
}

So if it shows "Logged in" in the browser, I am absolutely sure that the session is registered. But when I go to the next page (which has session_start(); at the top of the page), there's no session anymore. But if I go to the logout page (with session_destroy();), and then proceed to the login, the session is registered correctly. Also if I close all the browser windows and then go to login, it won't register correctly.

I tried destroying the session right before registering the 'id', but that also doesn't work.

I'm guessing I made a basic error, so someone on here should be able to help me without wasting a lot of time.

Please help me. I have wasted days on this. Thanks in advance.


More code:

Where session is registered:

$id_query = mysql_query ("
    SELECT * FROM Tour11_deelnemers WHERE fb_id = '$user'");
    while ($record = mysql_fetch_assoc ($id_query))
        {
        $id = $record['deelnemer_id'];
        }
if($id > 0)
{
$speelid = $id;
session_register("speelid");
}

After that to check if it is registered:

if(session_is_registered("speelid"))
{
echo "Ingelogd";
}

session_is_registered() is same as isset() for $_SESSION.

So the thing I don't understand is while session_is_registered() is true after it is registered, on the next page it is false again :( unless I login immediately after going to logging out page (session_destroy();). So even if i destroy the session just before restarting it and registering again, it doesn't help.

  • 写回答

1条回答 默认 最新

  • duanph1978 2011-06-12 14:13
    关注

    Here is a solution for you.

    Set the session like this:

    if(!isset($_SESSION['speelid'])){
       $id_query = mysql_query ("SELECT * FROM Tour11_deelnemers WHERE fb_id = '$user'");
            while ($record = mysql_fetch_assoc ($id_query)){
                $id = $record['deelnemer_id'];
            }
        if($id > 0){
            $_SESSION['speelid'] = $id;
        }
    }
    

    Check if a session is set like this:

    if(isset($_SESSION['speelid'])){
        echo "Ingelogd";
    }
    

    Update

    It seems like the issue is related to >= IE6 refusing to accept the session cookie generated by the php, when a .php file is referred from an .html file on a different server.

    .HTML to .PHP session IE issue

    When using session variables in a .php file referred by a frame (.html, or other file type) at a different server than the one serving the .php:

    Under these conditions IE6 or later silently refuses the session cookie that is attempted to create (either implicitly or explicitly by invoquing session_start()).

    As a consequence, your session variable will return an empty value.

    According to MS kb, the workaround is to add a header that says your remote .php page will not abuse from the fact that permission has been granted.

    Place this header on the .php file that will create/update the session variables you want:

    Here is a full thread on this issue

    Solution

    The solution is to add this at the very top of the page which will SET the session.

    <?php header('P3P: CP="CAO PSA OUR"'); ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 速帮,学校需要在外上班没空
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义