dongyan2445 2018-08-07 00: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 00: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部