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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题