php - Preserve session after redirect? -
if var_dump session variable before calling redirect function, looks okay. however, after redirecting external clickabnk's payment page, i'm no longer able access session variable on page after payment.
i'm redirecting using function below :
public function redirect($url) { if(!headers_sent()) { //if headers not sent yet... php redirect header('location: '.$url); exit(); } else { //if headers sent... javascript redirect... if javascript disabled, html redirect. echo '<script type="text/javascript">'; echo 'window.location.href="'.$url.'";'; echo '</script>'; echo '<noscript>'; echo '<meta http-equiv="refresh" content="0;url='.$url.'" />'; echo '</noscript>'; exit(); } exit(); }
i'm using session_start on scripts i'm using sessions.
the page below 1 makes redirection
<?php if(!session_id()) session_start(); require_once("core.php"); $core = new coreoptions(); $options = $core->getchosenoptions(); $numoptions = count($options); $url = ""; switch($numoptions) { case 1: $url = $core->url1options; break; case 2: $url = $core->url2options; break; case 3: $url = $core->url3options; break; case 4: $url = $core->url4options; break; case 5: $url = $core->url5options; break; case 6: $url = $core->url6options; break; case 7: $url = $core->url7options; break; case 8: $url = $core->url8options; break; case 9: $url = $core->url9options; break; default: $url = $core->urldefaultoptions; break; } $core->redirect($url); exit(); ?>
below content of core.php
..... // options public function getchosenoptions(){ if (isset($_session["chosenoptions"])) homecoming $_session["chosenoptions"]; homecoming false; } // set options public function setchosenoptions($c) { $_session["chosenoptions"] = $c; } ..... // function redirect given page public function redirect($url) { if(!headers_sent()) { //if headers not sent yet... php redirect header('location: '.$url); exit(); } else { //if headers sent... javascript redirect... if javascript disabled, html redirect. echo '<script type="text/javascript">'; echo 'window.location.href="'.$url.'";'; echo '</script>'; echo '<noscript>'; echo '<meta http-equiv="refresh" content="0;url='.$url.'" />'; echo '</noscript>'; exit(); } exit(); } .....
the page below 1 want access $_session["chosenoptions"] from. why page below cannot access session variable although set correctly in page makes redirection? :
<?php if(!session_id()) session_start(); require_once("core.php"); $core = new coreoptions(); $uid = $core->uid(); $options = $core->getchosenoptions(); var_dump($options); // line shows null. why ? if (empty($options)) exit("options empty"); if (count((array)$options)>0) { seek { $conn = new pdo() // stripped conn info $conn->exec("set character set utf8"); // sets encoding utf-8 $conn->setattribute(pdo::attr_errmode, pdo::errmode_exception); $sql = "insert availablecommands (for_whom, oname, is_available) values (:fw, :on, :ia)"; $statement = $conn->prepare($sql); foreach($options $o) { $statement->bindvalue(":fw", $uid); $statement->bindvalue(":on", $o); $statement->bindvalue(":ia", 1); $count = $statement->execute(); $conn = null; // disconnect } } catch(pdoexception $e) { echo $e->getmessage(); } } $core->redirect("dashboard.php"); ?>
read session_id(). alter code accordingly:
<?php session_start(); if ( !isset($_session['oldid']) || $_session['oldid'] !== session_id() ) { // new session started ... ? } $_session['oldid'] = session_id(); ...
you need manually check whether current session old 1 or if new session has been started extent ever.
php session redirect
No comments:
Post a Comment