PHP Redirect From One webPage To Another Using header() function

In this PHP tutorial, we discuss PHP redirect that how to redirect from one page to another using PHP.In HTML, we are using hyperlink tag <a> which is used to redirect from one page to another page but there is a condition on it, That you need to click on a  link tag. But in PHP, we are going to redirect from one page to another page.

Let see, there is a function in PHP which is used for redirection called header() function.

php redirect

PHP Redirect using header() function

Now we create an example for it so we can understand better.we are going to create a login form and when we get the result we move to another page.

Login Form in Html:

Below code contains two input and one submit button. Two input are for username and password.

<!DOCTYPE html>
<html>
<head>
<title>Redirect in PHP using header() function </title>
</head>
<body>
<div class="first">
<div class="second">
<h3>Redirect Form On Submit using PHP header() function</h3>
<form action="" id="#form" method="post" name="#form">
<label>Username :</label>
<input id="username" name="username" placeholder='Username'type='text'>
<label>Password :</label>
<input id="password" name="password" placeholder='Password' type='password'>
<input id='submit' name="submit" type='submit' value='Submit'>
</form>
</div>
</div>
</body>
</html>

CSS Designing For Login Form:

Next Step using CSS to design a login form so it looks better.

.first{
	width:100%;
}
.second{
	width:20%;
	margin-left:40%;
	padding:2%;
	box-shadow:0 0 5px gray;
}
.second:hover{
	box-shadow:0 0 10px gray;
}
input
     {
      width:100%;
      border-radius: 20px;
      text-align:center;
      padding: 10px 0px 10px 0px;
      margin-top:8px;
      font-size:14px;
      margin-bottom:20px;
      box-shadow:0 0 7px;
      border:none
}

PHP Script:

Now PHP script for fetching post variables when we submit the form.

<?php

if(isset($_POST['submit'])){
//  Fetching variables from $_POST  
$username = $_POST['username'];
$password = $_POST['password'];

if($username !=''&& $password !='')
{
//  To redirect form to a another page
header("Location:http://www.codekila.com/");
}
else{
?><?php echo "All Fileds are Mandatory...";?><?php
}
}
?>

Complete Code:

And here is a complete code to test the header() function.

<!DOCTYPE html>
<html>
<head>
<title>Redirect in PHP </title>
<style>
.first{
	width:100%;
}
.second{
	width:20%;
	margin-left:40%;
	padding:2%;
	box-shadow:0 0 5px gray;
}
.second:hover{
	box-shadow:0 0 10px gray;
}
input
{
	border-radius: 20px;
	text-align:center;
	width:100%;
padding: 10px 0px 10px 0px;
margin-top:8px;
font-size:14px;
margin-bottom:20px;
box-shadow:0 0 7px;
border:none
}



</style>
</head>
<body>
<div class="first">
<div class="second">
<h3>Redirect Form On Submit using PHP header() function</h3>
<form action="" id="#form" method="post" name="#form">
<label>Username :</label>
<input id="username" name="username" placeholder='Username'type='text'>
<label>Password :</label>
<input id="password" name="password" placeholder='Password' type='password'>
<input id='submit' name="submit" type='submit' value='Submit'>
</form>


<?php

if(isset($_POST['submit'])){
//  Fetching variables from $_POST  
$username = $_POST['username'];
$password = $_POST['password'];

if($username !=''&& $password !='')
{
//  To redirect form on a another page
header("Location:http://www.codekila.com/");
}
else{
?><?php echo "All Fileds are Mandatory...";?><?php
}
}
?>


</div>
</div>
</body>
</html>


Conclusion:

This is all you need to redirect to another page using PHP.I hope you like this post. If you have any query related to this post.Feel free comment on the comment box.Do not forget to share this post because someone else also needs this post. And don’t forget to like on Facebook Page.

 

Also Read:

  1.  PHP Tutorial – PHP Introduction
  2.  PHP Echo And Print Function
  3.  PHP Explode And Implode Function

Leave a Reply

Your email address will not be published. Required fields are marked *