Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I redirect a php page to another php page? [duplicate]

Tags:

php

Possible Duplicate:
How to redirect users to another page?

I'm a beginner in the programming field....I want to redirect a php page to another one..How is it possible in the most easiest way? My concept is just to move,to the home page from the login page after doing the checking of user id and password.

like image 530
arunrc Avatar asked Mar 06 '12 09:03

arunrc


4 Answers

<?php
   header("Location: your url");
   exit;
?>
like image 88
Juvanis Avatar answered Nov 18 '22 08:11

Juvanis


While all of the other answers work, they all have one big problem: it is up to the browser to decide what to do if they encounter a Location header. Usually browser stop processing the request and redirect to the URI indicated with the Location header. But a malicious user could just ignore the Location header and continue its request. Furthermore there may be other things that cause the php interpreter to continue evaluating the script past the Location header, which is not what you intended.

Image this:

<?php
if (!logged_id()) {
    header("Location:login.php");
}

delete_everything();
?>

What you want and expected is that not logged in users are redirected to the login page, so that only logged in users can delete_everything. But if the script gets executed past the Location header still everything gets deleted. Thus, it is import to ALWAYS put an exit after a Location header, like this:

<?php
if (!logged_id()) {
    header("Location:login.php");
    exit; // <- don't forget this!
}

delete_everything();
?>

So, to answer your question: to redirect from a php page to another page (not just php, you can redirect to any page this way), use this:

<?php 

header("Location:http://www.example.com/some_page.php"); 
exit; // <- don't forget this!

?>

Small note: the HTTP standard says that you must provide absolute URLs in the Location header (http://... like in my example above) even if you just want to redirect to another file on the same domain. But in practice relative URLs (Location:some_page.php) work in all browsers, though not being standard compliant.

like image 35
Hendrik Richter Avatar answered Nov 18 '22 09:11

Hendrik Richter


Send a Location header to redirect. Keep in mind this only works before any other output is sent.

header('Location: index.php'); // redirect to index.php
like image 2
MrCode Avatar answered Nov 18 '22 09:11

MrCode


<?php  
header('Location: http://www.google.com'); //Send browser to http://www.google.com
?>  
like image 1
Rick Hoving Avatar answered Nov 18 '22 08:11

Rick Hoving