会话变量在页面之间丢失

会话变量在页面之间丢失

问题描述:

我有3个.php文件:

I have 3 .php files:

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;
?>

我通过ajax调用login.php:

I invoke login.php via ajax:

$.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";
  }
});

重定向到我的index.php后,$ _SESSION ['comp_id']为空.我在这里做错了什么?预先感谢.

After redirect to my index.php $_SESSION['comp_id'] is empty. What am I doing wrong here? Thanks in advance.

忘记提及这些页面在社交网络中的iframe应用中.也许这是问题所在?

Forgot to mention that these pages are in iframe app in social network. Maybe this is the problem?

问题肯定在iframe中.我的本地计算机上的会话很好.有任何解决方法的想法吗?

Problem is definitely in iframe. Sessions are fine ob my local machine. Any ideas how to fix it?

您必须调用session_start();在使用该会话的每个页面的开头.

You must call session_start(); at the beginning of every page that uses the session.