会话变量在重定向后丢失值

时间:2013-11-06 09:48:54

标签: php session

我有三个文件。

文件1:index.php

重定向到second.php并将id = $ var发送到second.php。

代码:

$var=52013;
window.open('second.php?id=$var','name','width=1000,height=700,scrollbars=yes');    

文件2:second.php

在此文件中,我收到了$var,当我在日志中打印时,它显示的值为$var

<?php
session_start();
$_SESSION['id'] = $_REQUEST['id'];
$GLOBALS["log"]->fatal("Get id of order = ".$_SESSION['id']);

//Here it redirects to third file for some varification online.
//This condition is true first time when this page is called but after
//redirection from third.php this condition becomes false.So code below
//this if statement will be executed after redirection from third file.

if(condition==true)
{
    header("Location: http://third.php");
}

print "Please visit:\n$authUrl\n\n";
print "Please enter the auth code:\n";

//After redirection from third.php i want to write document using value stored
//in session variable which we get at top.($_SESSION['id'])

$val = $_SESSION['id'];

//Write a file
$file_open = fopen("modules/doc/views/document.txt","wb");

$content = $val;
fwrite($file_open,$content);

fclose($file_open);

文件3:Third.php

此文件在线进行一些变更,然后重定向到second.php,以便在document.txt中写入会话ID


所以问题是当third.php重定向到second.php文件时,会话变量会丢失其值。我想在从third.php重定向之后在document.txt中写入会话变量的值,但那时$_SESSION['id']不包含任何内容。

但是这个过程应该是一样的,我不想改变它。这是要求。

即index.php - &gt; second.php - &gt; third.php - &gt; second.php - &gt;写会话值。

由于

1 个答案:

答案 0 :(得分:0)

在second.php的开头,你这样做:

  

在session_start();

     

$ _ SESSION ['id'] = $ _REQUEST ['id'];

清除会话,并放入$ _SESSION ['id'] $ _REQUEST ['id']中收到的任何内容。这就是为什么你在来自third.php的commint中没有$ _SESSION ['id']的东西。请注意,执行此操作时:

  

window.open('second.php?id = $ var'...

你只需在id

中发送字符串'$ var'