Link to home
Start Free TrialLog in
Avatar of Nathan Riley
Nathan RileyFlag for United States of America

asked on

PHP Redirect at end of Page

I have a PHP page the goes and updates data in my MYSQL database after that code is complete I would like the page to redirect back to another page.

How do I do this?  I found info on a header redirect, but don't want that, I don't want it to redirect to the next page until after my other code in the php page has executed.
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Nathan Riley


Ah ok, the header redirect worked just fine.

I was confused and since I saw the word header assumed it would just redirect right away rather than execute the rest of the database/php code.

Because PHP runs procedurally, you can run your SQL code (or anything else) and then call the redirect with the header function at the end. You can put some checks in place so that it only gets called if you want it to.

//do something here.

if ($success ) {
} else {
    //do something else...

The header function needs to be called before any output, so will only work if the other part of your script (the database bits) doesn't output anything to the browser.
Oops. Bit late posting that one :)