Please note, this is a STATIC archive of website technosmarter.com from July 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
 

PHP header() function | Redirect Operation


Uses of header() function in PHP

We can use Meta refresh to redirect a page to another. Meta is a simple tag of HTML language, which allows you to redirect the page directly, but if you have to redirect one page to another page in PHP, then you can redirect using header() function. The header() function plays an important role in PHP.

1. You can redirect any page to another page with the help of header() function.

2. With the help of header(), you can redirect the website to another website URL.

3. Redirect page on timing via header() function. You can set the timing refresh for redirection.

Definition of header() function

PHP header() function is used to send HTTP header to the client. If you are working on a project and want to use the PHP header() function. The header() function redirects the page after code executed.

The header function helps to send HTTP Header. If you want to redirect on another page after the success process. You need to define the http_response_code under the header function.

syntax



    
header(string,replace,http_response_code)

string- Header string to send(Required)

replace - Allows multiple headers of the same type(optional)

HTTP response code.- . It forces the HTTP response code to the specified value(optional)

The header() function prevents more than one header to be sent at once. If header has already sent , then you get a fatal error like "header already sent "

Examples of header() function

In this example, we use a simple header() function to redirect one page to another page.

Example

<?php
header("location:page_name.php");
?>

In the above example, we can use any page name. To redirect one page to another, we have to give the name of the page on which we want to redirect.

Redirection using website URL via header() function

As you saw in the above example, we can also redirect from page to page by giving a simple page name. Page location should be correct. By using the header() function, we can also redirect from one website URL to another website URL.

Example

<?php
header("location:https://example.com/");
?>

Redirecting on page URL to another page URL using header() function

If you are working the same website, you can redirect from one page URL to another page URL by using the header () function.

Example

<?php
header("location:https://example.com/page_name.php");
?>

Time duration redirection using header() function

In the above example, we redirect a page to another page without given any time duration. In this example, we set some time (seconds). By using the header() function, we can redirection after some time. In this example, we set a few seconds, from which the redirection operation takes as long as we set. After time ends, it gets redirected to another page.

Example

<?php
header("refresh:7;  url=https://example.com");
echo "Redirecting.....";
?>
Run

It redirects another page or URL after seven seconds

Login system example

The following example of the header() function will show you how to work header() function in PHP. You can set it inside the login system, sign up system , update operation , delete operation or etc. When a user fill username and password and it will redirect to another page.

Login Exmple using Header



<?php
session_start ();
include ("config.php"); 

if(isset($_REQUEST['sub']))
{
$a = $_REQUEST['uname'];
$b = $_REQUEST['upassword'];

$res = mysqli_query($mysqli,"select* from users where uname='$a'and upassword='$b'");
$result=mysqli_fetch_array($res);
if($result)
{
	
	$_SESSION["login"]="1";
	header("location:index.php");//header 

}

//if condition is wrong .
else	
{
	header("location:login.php?err=1");//header
	
}
}
?>
Try Demo

In the above example, we have used the PHP login script and header() function. If the condition is true then user redirects to another page (index.php page) if the condition is false it will show an error on the same login page. like "invalid username and password " .
We can define http response code as =>


header("location:index.php");
or
 
header("location:https://technosmarter.com/index.php");//complete URL path . To 
redirect the page 
after login or success 
process  etc . 


Please Share

Recommended Posts:-