Avatar of Nathan Riley
Nathan Riley
Flag 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.
PHPMySQL ServerWeb Development

Avatar of undefined
Last Comment
Chris Stanyon

8/22/2022 - Mon
Dave Baldwin

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
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.

Chris Stanyon

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 ) {
    header('Location: http://www.yourdomain.com/nextpage.php');
} 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.
Chris Stanyon

Oops. Bit late posting that one :)
Your help has saved me hundreds of hours of internet surfing.