PHP Redirect : How to make a redirect in PHP?

Let’s learn about how to make a redirect in PHP. There are various ways to make redirects in PHP. In this article we will discuss what are some of the best ways to redirect your PHP page.

You are wondering now. Why do we want to redirect a PHP instead of sticking to the page the user is in.

Well, there could be many reasons. One of them is user login module. Every time user signs or logging into a site, the website wants to verify user credentials before letting user access various modules and parts of the websites.

One of the best practice is to have a separate login verification PHP page e.g. login_check.php that connects to the database, verifies user credentials and then redirects user back to the original page the user was in. Let’s assume original page was where user is in.

We’ll show you how to accomplish the functionality above.

READ : PHP Interview Questions Answers : 20 Commonly asked PHP Questions

Basic Solution to how to make a redirect

$ref_url = $_SERVER[‘HTTP_REFERER’];
header(‘Location: ‘ . $ref_url, true, 302);

in this case, $ref_url variable will store the HTTP Referrer URL. in our example which is and using header() function you will pass the URL to location with HTTP Protocol 302 to redirect. 

HTTP Protocol 302 is used to perform URL redirection.

Alternative Solution to Redirect

You can also redirect using just Location URL via header() function but you will have to send this HTTP header before any HTML text. which means before you declare any <!DOCTYPE…> in HTML. Take a look at the example below.

header(‘Location: ‘.$ref_url);

This is not the best alternative since most of the time you would want to redirect once you already have defined your HTML elements in your PHP page.

Your PHP code snippet on login page should look something like if you use the First Basic Solution:

include(‘inc/dbcon.php’); //where dbcon.php is my file that connects to database

$loginemail = $_POST[‘loginemail’]; //variable name for login email
$loginpword = md5($_POST[‘loginpword’]); //pswd variable encrypted in md5

if ($loginemail <> “”){
$query=”select * from member where email = ‘$loginemail'”;
$result = mysql_query($query);
$resultcon = mysql_num_rows($result);

//checks whether user input for email exists in the database or not

$data = mysql_fetch_array($result);
$loginseq = $data[seq] ;
$loginauth = $data[auth];
$loginpword2 = $data[pword] ;
$loginnick = $data[nick];
$visited = $data[visited];

//checks if password matches with the entry in database

if ($loginpword2 == $loginpword){

$query = “UPDATE member SET lasttime=’$lasttime’, lastip=’$lastip’, visited=’$visited’ WHERE seq =’$loginseq'”;
$loginfailed = “”;

//matches the username and password
//write logic here for all user-password match e.g. add session or cookies etc.
// the code below will redirect user to the original referrer page with logged in session

$ref_url = $_SERVER[‘HTTP_REFERER’];
header(‘Location: ‘ . $ref_url, true, 302);

$loginfailed = $_SESSION[“loginfailed”] + 1 ;

//login fails so prompt user to enter again and send back to the original page

$ref_url = $_SERVER[‘HTTP_REFERER’];
header(‘Location: ‘ . $ref_url, true, 302);


Hope this article and sample code helps. Leave your comments or questions if you need any help in PHP. We’ll get back to you.

READ : 15 Common SQL Interview Questions and Answers

READ : WordPress Interview Questions and Answers


This entry was posted in Uncategorized by admin. Bookmark the permalink.