Re: Redirect command in PHP ???

From: Date: Wed, 07 May 2003 12:47:12 +0000
Subject: Re: Redirect command in PHP ???
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-146661@lists.php.net to get a copy of this message
Andrei Verovski (aka MacGuru) wrote:
Hi, George, I have implemented header("Location: script.php) and everything seem to be OK until I have to continue current session. There is a class MyApp which does certain jobs, and instance of this class (not shown here). In function OpenMainPage() I have started session, assigned name to it and registered certain variables. file: MyApp.php class MyApp {
    // --- MORE STUFF HERE
    function OpenMainPage()
    { 
put here global $aaa; or use $_SESSION['aaa'] instead
        $res = $this -> mDb -> Execute('SELECT * FROM contacts');
        if ($res -> RecordCount() == 0)
            $this -> SetupDefaultDBRecords();
        session_start();
        session_name('MyApplicationSession'); 
changing session name must be done before session_start()
        $aaa = "blah blah blah";
        if (! session_register("aaa"))
            echo "failed to register variable";
        header("Location:StartupPage.php?".SID);
    }
} ---------------------------------------------- Then I would like to pass the control to script called StartupPage.php using header("Location:StartupPage.php?".SID). file StartupPage.php session_start(); echo session_name(); // prints PHPSESSION or something like this echo $_SESSION["aaa"]; // prints nothing ---------------------------------------------- However, session seem to be terminated, although I have used SID. I
have tried to move         session_start(); session_name('MyApplicationSession') to the first script where the instance of MyApp class have been created and used, but the result is the same. I have checked PHP options (default install of PHP 4.2.3 and Apache2 on SuSE Linux 8.2), enable-trans-sid and track-vars options are on, so it should work. Quite strange, but {echo SID} prints nothing, although { echo session_ID() } prints long cryptic string. I have some other PHP programs installed on my server, but they working without problem header("Location:script.php?".SID) without cookies (my app should work without cookies enabled in browser).
Any have an idea what I did wrong? Thanks in advance for any suggestion. On Tuesday, May 6, 2003, at 04:04 PM, George Whiffen wrote:
Andrei, Why is header() a problem? If it is just that you cannot use it after having sent output then there are two solutions: 1. The proper solution Change your code so that you check if you want a "redirect" before generating any output. In general you should only generate actual "output" at the end of your php script after executing any logic not directly associated with output. In particular anything that could cause an error or a change in the page you want to go should be handled before worrying about to output. This separation of processing and representation, (output), should make your php much easier to understand and change. 2. The quick fix If you switch output buffering on, then the restriction on redirecting before generating output does not apply. See ob_start() in the PHP manual.
********************************************* * Best Regards --- Andrei Verovski * * Personal Home Page * http://snow.prohosting.com/guru4mac * Mac, Linux, DTP, Development, IT WEB Site *********************************************


« previous php.general (#146661) next »