重定向后保留会话?

时间:2014-06-22 16:42:07

标签: php session redirect

如果我在调用重定向函数之前var_dump我的会话变量,它看起来没问题。但是,在重定向到外部Clickabnk的付款页面后,我无法再在付款后访问页面上的会话变量。

我正在使用以下功能重定向:

public function redirect($url) {
if(!headers_sent()) {
    //If headers not sent yet... then do php redirect
    header('Location: '.$url);
    exit();
} else {
    //If headers are sent... do javascript redirect... if javascript disabled, do 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();
}

我在所有使用会话的脚本上使用session_start。

下面的页面是进行重定向的页面

<?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();

?>

以下是core.php的内容

.....
// get options
public function getChosenOptions(){
if (isset($_SESSION["chosenOptions"])) return $_SESSION["chosenOptions"];
return false;
}

// set options
public function setChosenOptions($c) {
$_SESSION["chosenOptions"] = $c;
}

 .....
 // function to redirect to given page
public function redirect($url) {
if(!headers_sent()) {
    //If headers not sent yet... then do php redirect
    header('Location: '.$url);
    exit();
} else {
    //If headers are sent... do javascript redirect... if javascript disabled, do 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();
}
.....

下面的页面是我想要访问$ _SESSION [“chosenOptions”]的页面。为什么下面的页面无法访问会话变量,尽管它在进行重定向的页面中设置正确? :

 <?php if(!session_id())  session_start();


require_once("core.php");
$core = new coreOptions();
$uid = $core->uid();
$options = $core->getChosenOptions();
var_dump($options); // this line shows NULL. Why ?
if (empty($options)) exit("Options Is Empty");

if (count((array)$options)>0) {

try {
    $conn = new PDO() // stripped conn data
    $conn->exec("SET CHARACTER SET utf8");      // Sets encoding UTF-8
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = "INSERT INTO availablecommands (for_whom, oname, is_available) VALUES (:fw, :on, :ia)";
    $statement = $conn->prepare($sql);

    foreach($options as $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");



?>

1 个答案:

答案 0 :(得分:1)

仔细阅读session_id()。相应地更改您的代码:

<?php 
session_start();

if ( !isset($_SESSION['oldid']) || $_SESSION['oldid'] !== session_id() )
{
   // new session started ... ?
}
$_SESSION['oldid'] = session_id();

 ...

您需要手动检查当前会话是旧会话还是新会话已启动到何种程度。