Wednesday 19 March 2014

Using the Header function to Force Page Redirect

using header function to force page redirect
Using the header function to force page redirect in php programming language. In this we are using header function in php. So, if we want to modify header which sends to our browser. Now why we need to modify header which are sends to our browser. Now there are lots of reason for this like enable or disable caching of pages you also may want to change document type you may want to change like to jpeg or png images something like that. In order to process image on page if you are using php gd library. We use header function to redirect our users to another page.

Code for using header function to force page redirect (Screenshot1) :-

Using the Header function to Force Page Redirect
Code For Using Header Function To Force Page Redirect Screenshot1



<?php

$redirect_page = 'http://google.co.uk';

$redirect = true;



header('Location: '.$redirect_page);



?>

Output (Screenshot2) :-

Using the Header function to Force Page Redirect
Output Screenshot2
In this example i am creating variable called $redirect which will be going to true.  I want to do that if this variable is true then redirect page to other page like example http://google.co.in. This type of functions you can also use when you want to redirect users to specific page like users sign in or sign out you want to redirect them to specific pages. So, we declare header function inside that we specify location which had predefined text 'location:' after that the page we want to redirect now there are two ways to declare location one is header('Location: http://google.co.uk'); or header('Location:'.$variable_name); variable name is the name of the variable in which we had stored the path of the redirect file here we had variable $redirect_page. As we refresh this file it will redirect us to http://google.co.in. 

Now you can see how its useful to redirecting pages to another page. If you will see my further article you may come to know that this function is not only used for redirect its just used to modify header information your client or your browser. Location feature is very important. Its a quickly way to redirect the user without using any html meta tags. 

Code for using header function to force page redirect (Screenshot3):-

code for using header function to force page redirect
Code for Using Header Function to Force page Redirect Screenshot3


<h1>My page</h1>

<?php

$redirect_page = 'http://google.co.uk';

$redirect = true;



header('Location: '.$redirect_page);



?>

output (Screenshot4):-

header function php
Output Screenshot4
As you can see in this example is the same example of header function which is in screenshot1. But in this code we had declare h1 tags before header function its importance to notice in header functions if data is passed to browser it will not work to redirect. header function must be declare first or other wise we can use ob_start in php article which is stands for output buffering. You can see screenshot4 of warning. 

But if we declare h1 tag after the header function it will redirect the page. but you will not gonna see that h1 tag because it will redirect the page. 


No comments:

Post a Comment

Thanks For Comment Will get you Soon..

About Me

Welcome to Extra Tutorials! My name is Mohammed and I am the 22 year writer, website developer, and photographer behind the blog. Thanks for visiting! Tutorials Jackpot… In addition to Developer, I love to develop websites and I love to write. Starting a php Blog was inevitable for me. What began as a simple way to share all of my Tutorials with friends and family has developed into my Part time job.

Mohammed Padela

WHAT IS PHP PROGRAMMING

WHAT IS PHP PROGRAMMING
WHAT IS PHP PROGRAMMING

Follow Us

Popular Posts

Designed ByBlogger Templates