dongyan2445 2018-08-07 08:08
浏览 48
已采纳

PHP中的header()是异步函数吗?

Here is a piece of code in process.php file:

if(isset($_POST['skip'])) {
        session_destroy();
        header('Location: main.php'); 
    } else {
        if(!empty($_SESSION['nameErr']) || !empty($_SESSION['quoteErr'])) {
            header('Location: index.php');
        } else {
            if(mysqli_query($connection, $query)) {
                // echo 'new record';
                session_destroy();
                header('Location: main.php');
            } else{
                // echo "Error: " . $query . "<br>" . mysqli_error($connection);
                header('Location: index.php');
            }
        }
    }

I have a form with two 'submit' buttons in index.php file:

<form action="process.php" method="post">
                <span class="error"> <?php if (isset($_SESSION['nameErr']))
                 {echo $_SESSION['nameErr'];}?> </span> <br>
                <label> Your name: <span>*</span>
                <input type="text" name="name"> </label><br/>
                <span class="error"> <?php if (isset($_SESSION['quoteErr'])) 
                {echo $_SESSION['quoteErr'];}?> </span> <br>
                <label> Your quote: <span>*</span>
                <textarea rows="5" cols="50" name="quote"></textarea></label><br/>
                <input type="submit" value="Add my quote!"name="add"> 
                <input type="submit" value="Skip to quotes!" name="skip">
            </form>

If the 'skip' button is clicked, then redirect to 'main.php' page. The above code in process.php works, but if I changed a little bit:

if(isset($_POST['skip'])) {
    session_destroy();
    header('Location: main.php'); 
} 
    if(!empty($_SESSION['nameErr']) || !empty($_SESSION['quoteErr'])) {
        header('Location: index.php');
    } else {
        if(mysqli_query($connection, $query)) {
            // echo 'new record';
            session_destroy();
            header('Location: main.php');
        } else{
            // echo "Error: " . $query . "<br>" . mysqli_error($connection);
            header('Location: index.php');
        }
    }

When click the 'skip' button, it still stays in the index page. I'm wonder if that because header() is an asynchronous function in PHP? I couldn't find any official reference to it. Please provide some tips. Thanks in advance.

  • 写回答

3条回答 默认 最新

  • doutongxuan1614 2018-08-07 08:21
    关注

    If you have $_POST['skip'] set, you execute session_destroy();.

    Then your sessions will be destroyed, therefore:

    !empty($_SESSION['nameErr']) || !empty($_SESSION['quoteErr']) will always be false.

    And then it comes to this condition:

    if(mysqli_query($connection, $query)) {
        // echo 'new record';
        session_destroy();
        header('Location: main.php');
    } else{
        // echo "Error: " . $query . "<br>" . mysqli_error($connection);
        header('Location: index.php');
    }
    

    Check if that mysqli_query actually returns true otherwise it will always end up on index.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题