简体   繁体   English

页面之间的会话变量丢失

[英]session variable lost between pages

I have 3 .php files: 我有3个.php文件:

index.php index.php

<?php
session_start();

if (isset($_SESSION['comp_id']))
{
    require_once 'scan_albums.php';
}
else
{
    require_once 'login.php';
}

echo "SESSION['comp_id']=" .$_SESSION['comp_id'];
?>

scan_ablums.php scan_ablums.php

<?php
session_start();
if (!isset($_SESSION["comp_id"]))
{
    header('Location: index.php');
}
//...
?>

login.php login.php

<?php
session_start();
if (isset($_SESSION['comp_id']))
{
    header('Location: index.php');
}
$_SESSION['comp_id'] = 3;
?>

I invoke login.php via ajax: 我通过ajax调用login.php:

$.ajax(
{
  type: 'POST',
  url: 'login.php',
  data: {'login': login, 'password': hpass},
  success: function(data)
  {
    console.log("ajax: [comp_id]=" + data + "\n");
    window.location = "index.php";
  }
});

After redirect to my index.php $_SESSION['comp_id'] is empty. 重定向到我的index.php后,$ _SESSION ['comp_id']为空。 What am I doing wrong here? 我在这里做错了什么? Thanks in advance. 提前致谢。

EDIT: Forgot to mention that these pages are in iframe app in social network. 编辑:忘记提及这些页面在社交网络的iframe应用程序中。 Maybe this is the problem? 也许这是问题所在?

EDIT: Problem is definitely in iframe. 编辑:问题肯定在iframe中。 Sessions are fine ob my local machine. 我的本地计算机上的会话很好。 Any ideas how to fix it? 任何想法如何解决?

You must call session_start(); 您必须调用session_start(); at the beginning of every page that uses the session. 在使用该会话的每个页面的开头。

http://pt2.php.net/manual/en/function.session-write-close.php http://pt2.php.net/manual/en/function.session-write-close.php

Try calling session_write_close(); 尝试调用session_write_close(); after you do the "login" 完成“登录”后

Check the session_id in the pages to see if it's all equal. 检查页面中的session_id以查看它们是否相等。

Also see: 另请参阅:

When session.use_trans_sid is enabled, the session_start() function will register an internal output handler for URL rewriting. 启用session.use_trans_sid时,session_start()函数将注册内部输出处理程序以进行URL重写。

In session_start 在session_start中

Check with your hosting provider about the details of their PHP configuration, specifically how it handles session data. 向您的托管服务提供商咨询有关其PHP配置的详细信息,特别是它如何处理会话数据。

Ideally compare their php.ini configuration with your local (working) configuration. 理想情况下,将其php.ini配置与本地(工作)配置进行比较。 Check the session section. 检查会话部分。

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

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