简体   繁体   English

标头重定向后,会话似乎消失了

[英]Session seems to disappear after header redirect

I have four pages so far: 到目前为止,我有四页:

index.php
login.php
setsessionconfig.php
account.php

The background: 的背景:

When I click the login w/ facebook button from the index it takes me to login.php which if the user is already registered with the site it doesn't go through the facebook website it just continues. 当我从索引中单击带Facebook的登录w / facebook按钮时,它将带我进入login.php,如果用户已经在该网站上注册,则它不会通过facebook网站,而是继续。 Login.php pulls the neccessary information for the session then header redirects to setsessionconfig.php which has the code below: Login.php提取会话的必要信息,然后标头重定向到setsessionconfig.php,其代码如下:

login.php has a 5 second delay for loading visual then redirects to ... login.php加载视觉效果有5秒的延迟,然后重定向到...

header( "refresh:5;url=".URLBASE."/setsessionconfig.php?uid=".$uid."&email=".$email );

setsessionconfig.php setsessionconfig.php

$uid = isset($_GET['uid']) ? $_GET['uid'] : "";
$email = isset($_GET['email']) ? $_GET['email'] : "";

if( $uid != "" && $email != "") {

  session_start();
  $_SESSION[SESSION_UID] = $uid;           
  $_SESSION[SESSION_EMAIL] = $email;
  $_SESSION[SESSION_IS_LOGGEDIN] = 1;
  header("Location: account");

}

The Problem 问题

When setsessionconfig.php redirects to account.php it checks to see if the users is logged in via the SESSION_UID global variable then displays the user information OR it displays the "you are not logged in" text. 当setsessionconfig.php重定向到account.php时,它将检查用户是否通过SESSION_UID全局变量登录,然后显示用户信息,或者显示“您尚未登录”文本。 No matter what I do I think the header redirect to account.php is destroying session variables. 不管我做什么,我都认为标题重定向到account.php破坏了会话变量。

I even checked to see if the session was available with the following code in account.php. 我什至还检查了会话是否可以在account.php中使用以下代码进行访问。

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 ) 
    echo "<script>console.log('FALSE');</script>";
else
    echo "<script>console.log('TRUE - ".session_id()."');</script>"; 

Unfortunately that part actually returns the TRUE and the session ID... So I am sort of stuck because I have never had this issue with sessions before... 不幸的是,该部分实际上返回了TRUE和会话ID。所以我有点卡住了,因为我以前从未在会话中遇到这个问题...

try using exit(); 尝试使用exit(); after the header 在标题之后

Since the function is_session_started, returned TRUE and also returned the session ID, obviously the session ID is passed properly. 由于函数is_session_started已返回TRUE,并且还返回了会话ID,因此显然可以正确传递会话ID。

I hope the account.php code looks something like this 我希望account.php代码看起来像这样

<?php               
session_start();
if (! empty($_SESSION['SESSION_UID']))
{
?>

your code here

<?php
}
else
{
     echo 'You are not logged in.';
}
    ?>

Edit : 编辑:

Try 尝试

$_SESSION['SESSION_UID'] = $uid;           
  $_SESSION['SESSION_EMAIL'] = $email;
  $_SESSION['SESSION_IS_LOGGEDIN'] = 1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM