How to Redirect the Pages in PHP after a few seconds

This article is a Snippet about how to redirect from one page to another page in PHP after a few seconds. In this article, I’m going to show you,

  1.  How to redirect the pages in PHP without setting time.
  2.  How to redirect the pages in PHP with setting the time.

Before starting the coding, let me explain the theme setup, First I’m going to consider the two PHP pages in one folder.

  1. loginpage.php
  2. homepage.php

In loginpage.php page, add the form input fields with username, password and submit button and style it. whenever a user tries to login, first it validates the form and next if form values are set it redirects to the homepage.php.

This is how the login page looks after styling it.

redirect-pages-php

Note: I’m using the local server wamp for this demo. and I’m not going to explain about HTML and CSS in this article.

How to redirect the pages in PHP without setting time.

redirect-pages-php

PHP code:
redirect-pages-php
code explanation:
whenever a user submits the login button, submitted value sets into a isset() function and next if a user enters the login credentials then if-else statement checks, whether the form values are set or not, if form values are set it redirects to the homepage.php and if form values are not set it shows the error message.

whenever login credentials are not set, then the user gets an error message.
redirect-pages-php

How to redirect the pages in PHP with setting the time.

redirect-pages-php

PHP code:
redirect-pages-php
code explanation:
whenever a user submits the login button, submitted value sets into a isset() function and next if a user enters the login credentials then if-else statement checks, whether form values are set or not, if form values are set it shows the success message then after 3 seconds it redirects to the homepage.php and if form values are not set it shows the error message.

Redirecting code without time set:

header('location:URL ADDRESS');

Redirecting code with three seconds time set:

header('refresh:3; url=URL ADDRESS');
Rajesh DN
Latest posts by Rajesh DN (see all)
0 0 votes
Article Rating
Subscribe
Notify of
guest

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
PheHePhe
PheHePhe
6 years ago

You need to set a HTTP 301 or 301 status code as well. Just setting the Location header isn’t enough.

jacky
jacky
1 year ago

I’m done