juanli
juanli

Reputation: 613

Is header() in PHP a Asynchronous function?

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.

Upvotes: 0

Views: 370

Answers (3)

Tarun Kumar
Tarun Kumar

Reputation: 201

No, header() is not asynchronous function in PHP. Whats happening above is since you have removed else block, that code is executed anyway. This line if(mysqli_query($connection, $query)) { is returning false and your code is ultimately reaching line header('Location: index.php'); which is overriding the location header and resetting it the same index.php page.

There could be multiple solutions for this problem, one could be as dn Fer mentioned, use exit() function after setting location header if you want that to be the final redirection.

Upvotes: 1

DigiLive
DigiLive

Reputation: 1103

The header function sends a raw http header to the browser. In case of the location tag, it sends a redirect header to the browser which in turn opens the new location, after the script execution finished.

To prevent execution of code after sending the redirection header, you should exit the script.

<?php
header("Location: http://www.example.com/"); /* Redirect browser */

/* Make sure that code below does not get executed when we redirect. */
exit;
?>

Upvotes: 1

C&#233;sar Ferreira
C&#233;sar Ferreira

Reputation: 681

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

Upvotes: 1

Related Questions