会话变量在页面之间丢失 [英] session variable lost between pages

查看:50
本文介绍了会话变量在页面之间丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有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.

这篇关于会话变量在页面之间丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆