Newbie - How to redirect to another URL?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

tonydm
Forum Newbie
Posts: 11
Joined: Fri Aug 06, 2004 1:07 pm

Newbie - How to redirect to another URL?

Post 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 :)
ldomingues
Forum Commoner
Posts: 41
Joined: Fri Aug 06, 2004 1:15 pm
Location: Portugal

Post by ldomingues »

You must manipulate the http headers.

use:

header('location: your_url');


http://pt2.php.net/manual/en/function.header.php
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Damn just missed it!
tonydm
Forum Newbie
Posts: 11
Joined: Fri Aug 06, 2004 1:07 pm

Post 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
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post 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...
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post 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...
tonydm
Forum Newbie
Posts: 11
Joined: Fri Aug 06, 2004 1:07 pm

Post by tonydm »

<?php $p = $_GET['id']; ?>
<?php if ($p == "1") {header( "location: http://forums.devnetwork.net ");} ?>
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

try:

Code: Select all

<?php 
if ($_GET['id'] == "1") 
{
 header("location: http://forums.devnetwork.net");
} 
?>
Last edited by Joe on Fri Aug 06, 2004 1:22 pm, edited 1 time in total.
ldomingues
Forum Commoner
Posts: 41
Joined: Fri Aug 06, 2004 1:15 pm
Location: Portugal

Post by ldomingues »

You probably have spaces or line breaks lying around before the header function.
tonydm
Forum Newbie
Posts: 11
Joined: Fri Aug 06, 2004 1:07 pm

Post 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");
}?>
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

And did you try:

Code: Select all

<?php
if ($_GET['id'] == "1")
{
header("location: http://forums.devnetwork.net");
}
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post 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.
Last edited by Joe on Fri Aug 06, 2004 1:28 pm, edited 2 times in total.
tonydm
Forum Newbie
Posts: 11
Joined: Fri Aug 06, 2004 1:07 pm

Post by tonydm »

Yes, I did :)
Post Reply