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

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

相关推荐 更多相似问题