We help IT Professionals succeed at work.

PHP Redirect at end of Page

Last Modified: 2012-05-27
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.
Watch Question

Fixer of Problems
Most Valuable Expert 2014
This one is on us!
(Get your first solution completely free - no credit card required)
Nathan RileyFounder


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.

Most Valuable Expert 2018
Distinguished Expert 2019

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.
Most Valuable Expert 2018
Distinguished Expert 2019

Oops. Bit late posting that one :)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.