Log in

View Full Version : redirecting



mayanktalwar1988
09-28-2009, 06:32 PM
location.php


<?php
include("mysql_connect.php");
$id =$_GET['id'];
$sql=mysql_query("SELECT * FROM topics WHERE id='".$id."'");
if($row=mysql_fetch_array($sql))
header("Location:{$row['webpage']}");
mysql_close($con);
?>


with the above scrpt it is redirecting me to http://localhost/pro1/facebook.com istead of facebook.com
the link with which this script get intiated is like this localhost/pro/location.php?id=21
there is a corresponding webpage in the table coresponding to that particular unique id...how to make this script go to http://domain.com
instead of localhost/pro/domain.com

Schmoopy
09-29-2009, 01:31 AM
<?php
include("mysql_connect.php");
$id =$_GET['id'];
$sql=mysql_query("SELECT * FROM topics WHERE id='".$id."'");
if($row=mysql_fetch_array($sql))
header("Location: http://{$row['webpage']}");
mysql_close($con);
?>

Either change your code as above, or just add "http://" to the beginning of the webpage values.

jscheuer1
09-29-2009, 01:37 AM
PHP novice here. But I'm under the impression that you cannot redirect unless it is the very first directive.

Schmoopy
09-29-2009, 01:58 AM
As long as there's no output before the header, then it's fine ^^

jscheuer1
09-29-2009, 02:09 AM
Ah, but isn't an include output?

thetestingsite
09-29-2009, 02:18 AM
John,
Only if the file being included outputs something, but if it just has some variables or functions, then it should be fine.

Hope this helps.

Schmoopy
09-29-2009, 02:21 AM
It may do, but I just did some testing on it, putting the redirect all the way at the bottom, underneath a mountain of php and html all outputting random stuff, and it still managed to redirect without an error.
So hopefully shouldn't cause too many problems.