简体   繁体   English

会话变量在IE和Safari中工作,但不是Chrome,Opera或Firefox?

[英]Session variable working in IE and Safari, but not Chrome, Opera or Firefox?

I'm setting a session variable from a log in page ($_SESSION['login']). 我正在从登录页面设置会话变量($ _SESSION ['login'])。 I've tested thoroughly, the variable is definitely being created and the correct value is being stored ($_SESSION['login'] = '1'), but the variable isn't recognized in the next page using Chrome, Opera or Firefox - oddly enough, the same script works as I expect in Safari and Internet Explorer. 我已经彻底测试过,变量肯定是在创建并且存储了正确的值($ _SESSION ['login'] ='1'),但使用Chrome,Opera或Firefox在下一页中无法识别变量 - 奇怪的是,相同的脚本在Safari和Internet Explorer中的工作效果如我所料。

Any help at all would be greatly appreciated - I've been trying to figure this one out for a while and just can't get it - I'm at a complete loss here. 任何帮助都会非常感激 - 我一直试图想出这个问题一段时间而且无法得到它 - 我在这里完全失去了。

Here's the step by step - 这是一步一步的 -

<?php
// log in page
// if logged in successfully set the session variable

session_start();

if (// log in credentials good) {
    $_SESSION['login'] = '1';
    header('location:inside_page.php');

    // echo $_SESSION['login'] ;
    // ----------------------
    // commenting out the redirect and echoing
    // the session variable works as expected
    // in IE, Safari, Chrome, Opera and Firefox
}

?>

Here's my Check_login class - it get's instantiated in the next page (inside_page.php) - 这是我的Check_login类 - 它将在下一页(inside_page.php)中实例化 -

class: 类:

<?php

if (! defined('EXT')){ exit('Invalid file request'); }

class Check_login { 
    private $login = null ;

    function __construct() {
        session_start();
        $this->login = $_SESSION['login'];
        if ($this->login != '1') {
            header('location:../index.php');
        }

        // $login = $this->login ;
        // echo $login ;
        // $session = $_SESSION['login'];
        // echo $session ;
        // ----------------------
        // commenting out the redirect and echoing the
        // session variable shows 1 in IE and Safari, but
        // empty in Chrome, Opera and Firefox
    }

}

?>

page (inside_page.php): page(inside_page.php):

<?php

require('../lib/Check_login.php');

session_start();

$login = new Check_login;

?>

Again, any help at all would be really awesome. 再一次,任何帮助都会非常棒。 Thanks! 谢谢!

I figured this out - I can't say that I understand WHY this is the case, but this is what fixed my problem. 我想出了这个 - 我不能说我明白为什么会这样,但这就是解决我的问题的原因。 In my log in page, after verifying the login credentials I set the session variable 'login' to 1 and then explicitly redirected to another page. 在我的登录页面中,在验证登录凭据后,我将会话变量“login”设置为1,然后显式重定向到另一个页面。 I realized after testing, that using a relative URL for my redirection fixed the problem. 我在测试后意识到,使用相对URL进行重定向可以解决问题。

My original post didn't even illustrate the URL because I didn't think it was necessary, but here's where the problem was - 我的原帖甚至没有说明URL,因为我认为没有必要,但问题出在哪里 -

<?php
// log in page
// if logged in successfully set the session variable

session_start();

if (// log in credentials good) {
    $_SESSION['login'] = '1';
    header('location:https://www.somedomain.com/inside_page.php'); // *** PROBLEM
}

?>

By setting the location relatively, my problem went away - 通过相对设置位置,我的问题消失了 -

<?php
// log in page
// if logged in successfully set the session variable

session_start();

if (// log in credentials good) {
    $_SESSION['login'] = '1';
    header('location:inside_page.php'); // *** FIX
}

?>

I double-checked, and the secure connection definitely wasn't the problem, it was the explicit URL. 我仔细检查过,安全连接肯定不是问题,它是显式网址。

Using a relative location my session variable showed up in the next page in Internet Explorer, Safari, Chrome, Opera and Firefox. 使用相对位置,我的会话变量出现在Internet Explorer,Safari,Chrome,Opera和Firefox的下一页中。 Using an explicit location it showed up in IE and Safari, BUT NOT in CHROME, OPERA and FIREFOX. 使用显式位置,它出现在IE和Safari中,但不是在CHROME,OPERA和FIREFOX中。

You can recreate this easily and try it for yourself - 您可以轻松地重新创建它并自己尝试 -

<?php

// ------- page_one.php -------

session_start();

$_SESSION['val'] = '1';

header("location:http://www.somedomain.com/page_two.php") ;
// header("location:page_two.php") ;

?>

<?php

// ------- page_two.php -------

session_start();

if ($_SESSION['login'] != '1') {
    echo 'false' ;
} else {
    echo 'true' ;
}

?>

Hopefully this saves someone else some time (and headache). 希望这能节省一些时间(并且头痛)。

Move session_start() above your require statement in inside_page.php. session_start()移到inside_page.php中的require语句上方。

Sessions should be initiated at the very beginning of a script and almost always before includes. 会话应该在脚本的最开始时启动,并且几乎总是在包含之前启动。

I had a similar problem trying to integrate digitalaccesspass (DAP) onto a PHP5.5 server (breaking in all Webkit/Blink browsers, so breaking logins for Chrome, Safari, and Opera), and a constellation of these changes helped. 尝试将digitalaccesspass(DAP)集成到PHP5.5服务器上时遇到了类似的问题(打破了所有Webkit / Blink浏览器,因此破坏了Chrome,Safari和Opera的登录),这些变化的星座也有所帮助。

  • I made sure the very first line in the authenticator was session_start() 我确定验证器中的第一行是session_start()
  • Right before all redirects ( header("Location:") calls) I added session_write_close() . 在所有重定向( header("Location:")调用之前)我添加了session_write_close()
  • The redirects were edited to be relative paths, rather than absolute. 重定向被编辑为相对路径,而不是绝对路径。

For people stumbling across this via Google, if you're hitting this with DAP, make these changes in /dap/authenticate.php 对于通过Google绊倒的人来说,如果您使用DAP进行此操作,请在/dap/authenticate.php中进行这些更改

My PHP session was also not working in Chrome. 我的PHP会话也无法在Chrome中运行。

I found that the PHPSESSID cookie path was not synced between my pages despite each page using simply session_start() to initialize each session. 我发现我的页面之间没有同步PHPSESSID cookie路径,尽管每个页面只使用session_start()来初始化每个会话。

Adding this line above session_start() to the page on which I wrote data to the session solved the problem: session_start()上面的这一行添加到我向会话写入数据的页面解决了问题:

session_set_cookie_params(600, '/');

See: 看到:

http://php.net/manual/en/function.session-set-cookie-params.php http://php.net/manual/en/function.session-set-cookie-params.php

暂无
暂无

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

相关问题 代码可在Chrome,Firefox和Safari中运行,但不能在IE或Opera中运行 - Code working in Chrome, Firefox and Safari but not in IE or Opera file_get_contents编码 - 使用Chrome和Safari,不使用Firefox,Opera,IE - file_get_contents encoding - working Chrome and Safari, not working Firefox, Opera, IE php会话无法在chrome和firefox中使用,但可以在IE中 - php session not working in chrome and firefox but works in IE Chrome和Firefox不会取消设置会话数组,但IE和Safari会取消设置 - Chrome and Firefox do not unset session array but IE and Safari is do Safari,Opera和IE中的PHP会话问题 - PHP Session Problem in Safari, Opera, and IE 启用JavaScript的检查例程可在Chrome / Opera / IE上运行,但不能在Firefox / Safari上运行 - JavaScript-enabled checking routine works on Chrome/Opera/IE but not Firefox/Safari Ajax 调用/javascript - 在 IE、Firefox 和 Safari 上运行良好,但在 Chrome 上运行不正常 - Ajax call/javascript - working fine on IE, Firefox and Safari but not on Chrome 我有用于通过ajax或jquery更新mysql的表,并且适用于firefox,opera,chrome和safari,但不适用于IE8 - I have table that i use to update mysql via ajax or jquery and works for firefox,opera,chrome and safari but not IE8 在使用Chrome和IE而不是Firefox进行测试时,会话变量有效 - Session variables working when testing with Chrome and IE but not Firefox 会话无法在Chrome和Firefox上运行 - Session not working on Chrome and Firefox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM