Highspeed
Highspeed

Reputation: 442

PhP Headers and output buffering

So... if you have a script that states something like so...

while($result = mysql_fetch_array($resource))
    {
        if($result['TITLE'] == $this->title)
        {
            header("LOCATION: profile.php?error=11");
        }
        echo 'testing';
    }

    //Create the database profile
    $second_query = "INSERT INTO profiles(USER_ID, KEYWORDS, TITLE, INTRO) ";
    $second_query .= "VALUES(".$this->userId.",'".serialize($this->keywords)."','".$this->title."','".$this->intro."')";
    echo $second_query;
    if($result = mysql_query($second_query))
    {
        if(isset($file))
        {
            $this->update_profile($this->files);    
        }
        return true;    
    }else
    {
        return false;   
    }

and the first condition fails and sends the header back... If you don't return false after sending the header, does it continue running the script? I had an issue to where if the title was found in my database it would return the error, but it would continue running that script, thus inserting a duplicate title entry into my database.

So again... does a script continue executing even after you send a header? aka (in this case) a redirect?

Upvotes: 0

Views: 151

Answers (2)

Mihai Iorga
Mihai Iorga

Reputation: 39724

If a location header is sent without an exit yes it continues to run script.

Valid:

header("Location: profile.php?error=11");
die(); // or exit();

Think about that header isn't executed by the PHP itself, it's executed by the browser, same thing when you apply a header("Content-Type: application/force-download"); it tells the browser that the following outputted block has to be downloaded.

So even if you set the header to another location, all code inside script, unless we exit, gets processed by PHP and then the browser gets the location and redirects.

Upvotes: 3

Tarun
Tarun

Reputation: 3165

Yes it will ,so exit your script after sending header

header("Location: profile.php?error=11"); 
exit;

Upvotes: 0

Related Questions