簡體   English   中英

會話變量在重定向后失去價值

[英]Session variable loses value after redirection

我有三個文件。

文件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重定向后將session變量的值寫入document.txt中,但當時$_SESSION['id']包含任何內容。

但是過程應該是相同的,我不想更改它。 這是必要條件。

即index.php-> second.php-> third.php-> second.php->寫入會話值。

謝謝

在second.php的開頭,您可以執行以下操作:

session_start();

$ _SESSION ['id'] = $ _REQUEST ['id'];

這將清除會話,並將$ _REQUEST ['id']中收到的任何內容放入$ _SESSION ['id']。 這就是為什么從Third.php破壞$ _SESSION ['id']中沒有任何內容的原因。 請注意,執行此操作時:

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

您只需在ID中發送字符串'$ var'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM