douzhi1937 2012-02-20 18:21
浏览 38
已采纳

重定向不使用标题(位置)和会话变量

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

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

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

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

when step 1 , step 2 and step 3 are complete, page should be redirected to 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'

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?

code Update

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

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..)

  • 写回答

4条回答 默认 最新

  • drh19790711 2012-02-20 18:27
    关注

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

    EDIT:

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错