Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-12-2010, 11:38 PM   PM User | #1
cgeller100
New Coder

 
Join Date: Jan 2010
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
cgeller100 is an unknown quantity at this point
Simple PHP question

How do I use PHP code to redirect the user to:

(1)a different file at the same web address
(2)a different HTTP address altogether

I thought it was something using the "header" function but don't remember exactly what to do.

Thank You

CGG
cgeller100 is offline   Reply With Quote
Old 10-12-2010, 11:50 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
PHP Code:
header('Location: http://site.com/page.ext'); 
Location is the header. Technically it requires a full URL in order to redirect, but most of the time it will accept a relative url to the page you want to redirect to. It assumes your site if it cannot find a fully qualified url.

Also, add an exit(); after the header unless you want the page to continue processing. These are generally used in if/else type block checks, so you only redirect if the user meets some criteria so it makes sense to add the exit anyway.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 10-13-2010, 12:36 AM   PM User | #3
cgeller100
New Coder

 
Join Date: Jan 2010
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
cgeller100 is an unknown quantity at this point
Fou-Lu,

Thank You. Unfortunately it gave me this error message:

Warning: Cannot modify header information - headers already sent by (output started at /hermes/bosweb/web226/b2264/sl.garryplaza/public_html/delete_maint_req.php:6) in /hermes/bosweb/web226/b2264/sl.garryplaza/public_html/delete_maint_req.php on line 62

CGG
cgeller100 is offline   Reply With Quote
Old 10-13-2010, 12:52 AM   PM User | #4
Keleth
Senior Coder

 
Join Date: Jun 2008
Location: New Jersey
Posts: 2,354
Thanks: 45
Thanked 247 Times in 244 Posts
Keleth is on a distinguished road
You can't set header information once content has been displayed (echo/print/whatever).
Keleth is offline   Reply With Quote
Old 10-13-2010, 12:58 AM   PM User | #5
cgeller100
New Coder

 
Join Date: Jan 2010
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
cgeller100 is an unknown quantity at this point
So how would I redirect a user to a different page AFTER I have displayed information on the current page?

Thx
cgeller100 is offline   Reply With Quote
Old 10-13-2010, 01:12 AM   PM User | #6
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,293
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
But WHY do you need to do it after you display information? Can you post your code please? Almost always you don't need to send a header after you display information, headers are sent normally before any output.
__________________
||||If you are getting paid to do a job, don't ask for help on it!||||
_Aerospace_Eng_ is offline   Reply With Quote
Old 10-13-2010, 10:12 AM   PM User | #7
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,857
Thanks: 9
Thanked 288 Times in 284 Posts
Dormilich is on a distinguished road
Quote:
Originally Posted by cgeller100 View Post
So how would I redirect a user to a different page AFTER I have displayed information on the current page?
you cannot. it’s that simple.

Edit: you could use the HTML refresh meta tag, though that still requires to be printed before the HTML body
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 10-13-2010, 03:41 PM   PM User | #8
olidenia
Regular Coder

 
olidenia's Avatar
 
Join Date: Oct 2009
Location: Sitting In Front Of A Screen
Posts: 109
Thanks: 15
Thanked 4 Times in 4 Posts
olidenia is an unknown quantity at this point
Is this what you are looking for?

You can show the page and then redirect after x time, if you put it anyware in the page it will work.

To adjust time edit the numbers: setTimeout('Redirect()',1000);

for example: setTimeout('Redirect()',500);

Or redirect instantly: setTimeout('Redirect()',0);

Edit url to go to: location.href = 'redirect.html';


Code:
<?php
echo "<script type=\"text/javascript\"> setTimeout('Redirect()',1000); function Redirect(){location.href = 'redirect.html';}</script>";
?>
But ofcourse this is not PHP just Javascript echoed by PHP

Last edited by olidenia; 10-13-2010 at 03:47 PM..
olidenia is offline   Reply With Quote
Users who have thanked olidenia for this post:
cgeller100 (10-14-2010)
Old 10-13-2010, 04:43 PM   PM User | #9
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,857
Thanks: 9
Thanked 288 Times in 284 Posts
Dormilich is on a distinguished road
Quote:
Originally Posted by olidenia View Post
But ofcourse this is not PHP just Javascript echoed by PHP
and it only works, if JavaScript is enabled …
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 10-14-2010, 11:34 PM   PM User | #10
cgeller100
New Coder

 
Join Date: Jan 2010
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
cgeller100 is an unknown quantity at this point
This is Exactly what I was looking for - didn't think of using Javascript rather than PHP. Thanks!
cgeller100 is offline   Reply With Quote
Reply

Bookmarks

Tags
http, redirect

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:34 PM.


Advertisement
Log in to turn off these ads.