duanliao3826 2016-02-01 14:55
浏览 33
已采纳

重定向时不会回显会话

At my main page i have the following code:

<?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");
}     
}
?>

I have the corresponding form on same page.

<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> 

and then as you see my php code runs and redirects to another php page

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

now the echo doesn't echo the pss. I have never used session before and have no clue what goes wrong.Can anybody help me.

I'm getting the following error: undefined index pss error.

  • 写回答

1条回答 默认 最新

  • dongtangu8403 2016-02-01 15:02
    关注

    You have never set $_SESSION['ps'] in your code. set it as shown in below code and also always exit(); after you set header('location:') Otherwise the script execution is not terminated. Setting another header alone is not enough to redirect.

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

    and in 2nd page:

    <?php
    $pss = "";
    session_start();
    if(isset($_SESSION['ps'])){
        $pss = $_SESSION['ps'];
    }
    echo $pss;
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程