Page 1 of 2

Newbie - How to redirect to another URL?

Posted: Fri Aug 06, 2004 1:07 pm
by tonydm
Hi,

I am fairly new to PHP. I am trying to from a link on page.htm pass a var to a php script file. There I want to then test the value and direct the browser to another site.

my link to the test.php file is:

test.php?id=1

<?php $p = $_GET['id']; ?>
<?php if ($p == "1") ...what should I now do... ?>

I can validate $p but don't know what function or steps to take from there.
I would like to redirect the browser to say http://forums.devnetwork.net

The extension to this would also be able to say, have a member of my team log in, validate, then direct their browser to a specific dir and or html file as extracted from a table.

Thanks for any help!

tonydm :)

Posted: Fri Aug 06, 2004 1:15 pm
by ldomingues
You must manipulate the http headers.

use:

header('location: your_url');


http://pt2.php.net/manual/en/function.header.php

Posted: Fri Aug 06, 2004 1:16 pm
by Joe

Posted: Fri Aug 06, 2004 1:17 pm
by Joe
Damn just missed it!

Posted: Fri Aug 06, 2004 1:19 pm
by tonydm
Man you guys are quick.

Tied that. Got this

Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\TSW\Apache2\htdocs\phptest.php:8) in C:\Program Files\TSW\Apache2\htdocs\phptest.php on line 9

Posted: Fri Aug 06, 2004 1:19 pm
by Joe
You could also integrate html to do the work:

print("<meta http-equiv=refresh content='3; url=page.php'>");

That would redirect after 3seconds...

Posted: Fri Aug 06, 2004 1:19 pm
by Joe
Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\TSW\Apache2\htdocs\phptest.php: in C:\Program Files\TSW\Apache2\htdocs\phptest.php on line 9
Thats because you have output before the redirection. Please post your code...

Posted: Fri Aug 06, 2004 1:20 pm
by tonydm
<?php $p = $_GET['id']; ?>
<?php if ($p == "1") {header( "location: http://forums.devnetwork.net ");} ?>

Posted: Fri Aug 06, 2004 1:21 pm
by Joe
try:

Code: Select all

<?php 
if ($_GET['id'] == "1") 
{
 header("location: http://forums.devnetwork.net");
} 
?>

Posted: Fri Aug 06, 2004 1:22 pm
by ldomingues
You probably have spaces or line breaks lying around before the header function.

Posted: Fri Aug 06, 2004 1:25 pm
by tonydm
Same message, diff line num

Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\TSW\Apache2\htdocs\phptest.php:8) in C:\Program Files\TSW\Apache2\htdocs\phptest.php on line 11

no incorrect spacing that I can find...

<?php $p = $_GET['id']; ?>
<?php if ($p == "1")
{
header( "location: http://forums.devnetwork.net");
}?>

Posted: Fri Aug 06, 2004 1:25 pm
by Joe
And did you try:

Code: Select all

<?php
if ($_GET['id'] == "1")
{
header("location: http://forums.devnetwork.net");
}
?>

Posted: Fri Aug 06, 2004 1:27 pm
by feyd
if you have html or any text of any kind (including white-space) output before the header call is reached, you will get this error..

Posted: Fri Aug 06, 2004 1:27 pm
by Joe
You should not close the script then restart with a statement. Looks messy. If you wanted that code though try:

Code: Select all

<?php $p = $_GET['id']; ?>
<?php if ($p == "1")
{
 print("<meta http-equiv=refresh content='3; url=http://forums.devnetwork.net'>"); 
}
?>
Simple html integration... You will just get the header error with your original code as output is already being sent.

Posted: Fri Aug 06, 2004 1:27 pm
by tonydm
Yes, I did :)