重定向不使用标题(位置)和会话变量 [英] Redirect not working with Header(Location ) and session variable

查看:99
本文介绍了重定向不使用标题(位置)和会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1:我使用register.php注册客户端,

1: i use register.php to sign up the clients,

2:从表单收集的数据发送到1.php,保存在数据库

2: the data collected from the form is send to 1.php, it is saved in database

3:表格数据保存在数据库后,1.php将选定的表格数据(myValue)转发给register.php?myValue ='abc'

3: after form data is saved in database, 1.php forwards selected form data (myValue) to register.php?myValue='abc'

,我正在保存这样的会话变量

in 1.php, i am saving a session variable like this

@session_start();
$_SESSION['color']='blue';

register.php的代码是

the code of register.php is

 if (isset($_SESSION['color'])) {
            header('Location: http://mydomain.com/thankyou.php');
    }
 else {


@session_start(); 
some other stuff  that was initially use for signing up the clients

我的逻辑是检查对于会话变量并将其重定向到其他页面

my logic is to check for session variable and to redirect it to some-other page


当步骤1,步骤2和步骤3完成时,页面应为
重定向到thankyou.php

when step 1 , step 2 and step 3 are complete, page should be redirected to thankyou.php

当前,当步骤1,步骤2,步骤3完成时,而不是打开thankyou。 php,以下页面正在打开

currently, when step 1, step 2, step 3 are done, instead of opening thankyou.php, the following page is being opened

http://mydomain.com/register.php?myValue='abc'

但是,如果我重新打开register.php或返回第一步(打开register.php) ,thankyou.php显示...

however, if i re-open register.php or go back to step one (opening register.php), thankyou.php is displayed...

有人可以指导我在哪里犯错吗?尽管创建了会话变量,为什么重定向不成功?

can somebody guide me where i am doing the blunder? why redirection is not being successful although session variables are being created?

代码更新

我在我的注册表顶部尝试了以下代码.php

i tried the following code at the top of my register.php

@session_start();


   if (isset($_SESSION['color'])) {
            header('Location:http://mydomain.com/thankyou.php');
            exit;
    }
 else{
remaining stuff

它偶尔会这样做技巧,重定向到页面,有时(数量更多),它无法重定向到thankyou.php,代码需要删除完整的历史记录和缓存工作(这样做后,仍然会错过点击发生..)

it occasionally do the trick, redirects to the page, while on occasion (greater in number), it fails in redirecting to thankyou.php,, also the code needs to delete complete history and cache to work (after doing so, still miss hits occurs..)

推荐答案

在register.php中,在发出session_start之前无法测试会话变量,所以你的代码应该更像是:

In your register.php, you can't test for the session variable before you issue the session_start, so your code should be more like:

session_start(); 
 if (isset($_SESSION['color'])) {
            header('Location: http://mydomain.com/thankyou.php');
    }
 else {
 // Something else....



<编辑:

尝试将会话变量与重定向结合使用时,我发现有用的另一件事是仅在运行函数后才进行重定向。以下是它的工作原理:

Another thing I've found useful when trying to set session variable in conjunction with redirects is to proceed to the redirect only after running a function. Here's how it would work:

$throwAwayVariable = setColor('blue');
if($throwAwayVariable ){  // separated out into a function so it wouldn't redirect before the session variable was saved
    session_write_close();
    header("Location: http://mydomain.com/thankyou.php");
}

function setColor($color){
    @session_start();
    $_SESSION['color']='blue';
    return true;
}

由于并非所有代码都已过帐,您必须弄明白这样,但是在这个过程之后我总是让我的会话变量工作。

Since not all your code is posted, you'll have to figure out where this goes, but I've always had my session vars work after this process.

这篇关于重定向不使用标题(位置)和会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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