Weird issue with storing and unsetting session from redirect in Google Chrome with PHP -
i'm getting weird issue in google chrome. (it's fine in firefox, ie) code below recreate this. steps are:
- navigate page sets session variable , redirects
- the new destination page calls function that:
- reads session variable , stores in local variable
- unsets session variable
- returns local variable
- then page reads return value , echos html (or tries to)
i see 2 things:
if call echo before first output of page, echos browser but not actual source (i.e. "view source" not show text "echo"-ed)
if call echo within html, doesn't echo , doesn't print source @
the page redirects (you can go directly page)
<?php class landingredirector { private static $session_variable = "redirectedfrom"; function __construct() {} public function redirect() { //make sure have session session_start(); //set redirect variable $_session[ self::$session_variable ] = "red.php"; session_write_close(); //redirect correct page header( "location: /end.php" ); exit(); } //call , correctly prints value public function setonly() { //make sure have session session_start(); //set redirect variable $_session[ self::$session_variable ] = "red.php"; session_write_close(); } public function getredirect() { try { //make sure have session session_start(); //it's set if ( isset( $_session[ self::$session_variable ] ) ) { $redfrom = $_session[ self::$session_variable ]; //unset unset( $_session[ self::$session_variable ] ); return $redfrom; } } catch (exception $e) {} return null; } } //create $redirector = new landingredirector(); //redirect if ( !isset( $stophere ) || $stophere != true ) $redirector->redirect(); ?>
the destination page
<?php $stophere = true; //handles redirect code require_once $_server[ "document_root" ] . "/red.php"; //were redirected? $redirectorigin = $redirector->getredirect(); //this echos browser not in page when "view source" if ( $redirectorigin ) echo $redirectorigin; ?> <html> <head> <script> <?php //this not echo browser , not visible in "view source" either! if ( $redirectorigin ) echo $redirectorigin; ?> </script> </head> <body></body> </html>
edit: added function gets called if go directly end.php instead of via redirect. same setting, retrieving , unsetting of session variable, yet 1 work!
edit 2: more code show weirdness
compare these 2 functions. if call function 1 (notice neither function checks isset()
), you'll error notice: undefined index: redirectedfrom
. if call function 2, won't error, exist. difference added unset call. , error occurs on line prior unset call!
public function getredirect1() { //make sure have session session_start(); //this should exist, throws index error $redfrom = $_session[ self::$session_variable ]; //unset unset( $_session[ self::$session_variable ] ); return $redfrom; } public function getredirect2() { //make sure have session session_start(); $redfrom = $_session[ self::$session_variable ]; return $redfrom; }
google chrome requesting page twice (they speed responses).
this dumb solution 1 work.
notice have create 2 other session variables doesn't keep returning true forever , second check can unset, i'll still guaranteed have variable value after unset.
<?php class landingredirector { private static $session_variable = "redirectedfrom"; private static $chrome_session_variable = "redirectedfromchrome"; private static $chrome_session_variable_to_read = "redirectedfromchromeread"; function __construct() {} public function redirect() { //make sure have session session_start(); //set redirect variable $_session[ self::$session_variable ] = "red.php"; $_session[ self::$session_variable ] = "red.php"; session_write_close(); //redirect correct page header( "http/1.1 301 moved permanently" ); header( "location: /end.php" ); exit; } public function setonly() { //make sure have session session_start(); //set redirect variable $_session[ self::$session_variable ] = "red.php"; session_write_close(); } public function getredirect() { try { //make sure have session session_start(); //it's set if ( isset( $_session[ self::$session_variable ] ) ) { $redfrom = $_session[ self::$session_variable ]; //this deal chrome's double request bug $_session[ self::$chrome_session_variable ] = $redfrom; $_session[ self::$chrome_session_variable_to_read ] = $redfrom; //unset unset( $_session[ self::$session_variable ] ); return $redfrom; } //not set, chrome second request? else if ( isset( $_session[ self::$chrome_session_variable ] ) ) { $redfrom = $_session[ self::$chrome_session_variable_to_read ]; //unset 1 check unset( $_session[ self::$chrome_session_variable ] ); return $redfrom; } } catch (exception $e) {} return null; } } //create $redirector = new landingredirector(); //redirect if ( !isset( $stophere ) || $stophere != true ) $redirector->redirect(); ?>
Comments
Post a Comment