重定向

时间:2016-02-01 14:55:10

标签: php html sql

在我的主页面上,我有以下代码:

<?php
$required = array('post_question');
// making sure post field is not empty
$error = false;
foreach($required as $field) {
if (empty($_POST[$field])) {
$error = true;
}
}
if(isset($_POST['post_question'])){
if ($error) {
echo "please type your question and submit!";
} else {
session_start();
$ps = $_POST['post_question'];
$_SESSION['postquestion']=$_GET['ps'];
header("Location: http://localhost/voidportals/success.php");
}     
}
?>

我在同一页面上有相应的表格。

<form action="" method="post">
<div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3 col-sm-offset-3">
<input type="text" placeholder="ask your question!"  class="assin assin-success assin-autosize" name="post_question">
<input type="submit">
</div>
</form> 

然后当你看到我的PHP代码运行并重定向到另一个php页面

<?php
$pss = "";
session_start();
if(isset($_SESSION['ps'])){
$pss = $_SESSION['ps'];
}   
echo $_SESSION['pss'];
?>

现在回声没有回应pss。我之前从未使用过会话,也不知道出了什么问题。任何人都可以帮助我。

我收到以下错误: undefined index pss error.

1 个答案:

答案 0 :(得分:0)

您从未在代码中设置$_SESSION['ps']。 在设置exit();之后,将其设置为如下面的代码所示,并始终header('location:'),否则脚本执行不会终止。单独设置另一个标头不足以重定向。

<?php
$required = array('post_question');
// making sure post field is not empty
$error = false;
foreach ($required as $field) {
    if (empty($_POST[$field])) {
        $error = true;
    }
}
if (isset($_POST['post_question'])) {
    if ($error) {
        echo "please type your question and submit!";
    } else {
        session_start();
        $ps = $_POST['post_question'];
        $_SESSION['ps'] = $ps;
        header("Location: http://localhost/voidportals/success.php");
        exit();
    }
}
?>

并在第二页:

<?php
$pss = "";
session_start();
if(isset($_SESSION['ps'])){
    $pss = $_SESSION['ps'];
}
echo $pss;
?>