0

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?

Highspeed
  • 442
  • 3
  • 18

2 Answers2

3

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.

Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
  • Another quick question... I think in the back of my head I understand the answer to this; but when I put echo "Testing" even when the script continued, I didn't see that output anywhere on my page. I'm assuming this has something to do with it sending headers first and ignoring all output, yet, still running the script; am I correct? – Highspeed Sep 11 '12 at 19:22
  • Modified my answer, you should understand now. – Mihai Iorga Sep 11 '12 at 19:27
  • Saw your edit... I'll check it off when it allows me, 4 more minutes, thanks again. – Highspeed Sep 11 '12 at 19:28
0

Yes it will ,so exit your script after sending header

header("Location: profile.php?error=11"); 
exit;
Tarun
  • 3,162
  • 3
  • 29
  • 45