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?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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?
Modified my answer, you should understand now.
Saw your edit... I'll check it off when it allows me, 4 more minutes, thanks again.
0

Yes it will ,so exit your script after sending header

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.