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.
- Header string to send(Required)
- 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.
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.
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.
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.
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
$a = $_REQUEST['uname'];
$b = $_REQUEST['upassword'];
$res = mysqli_query($mysqli,"select* from users where uname='$a'and upassword='$b'");
//if condition is wrong .
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:https://technosmarter.com/index.php");//complete URL path . To
redirect the page
after login or success
process etc .
Integrate Payumoney payment gateway in PHP website | Source Scripts
Paypal payment form with email, invoice generator in PHP website | Scripts
Student result management system for school and college in PHP website