Forum Moderators: coopster

Message Too Old, No Replies

php redirection issue

         

dave1236

2:31 pm on Oct 12, 2006 (gmt 0)

10+ Year Member



All:

Many thanks to eelixduppy for walking me through a difficult challenge and having the patience to help me!

My final issue is this:

I have a field in my database called "externallink"

externallink can be either a link to a page within my site or to an external site. I populate this field with the complete url, and after I have grabbed the database row, I want the page to redirect to the link specified in $externallink

I have run into problems with the following:

This does not load the page:
{
echo file_get_contents($externallink);
}

This redirects me to the homepage:
<SCRIPT LANGUAGE="JavaScript">
setTimeout("document.location = '<?=$link?>'", 5000);

</SCRIPT>

If I simply display $externallink, it prints fine...this is what I use:

/* Display results in a table */
echo "<table cellspacing='15'>";
echo "<tr><td colspan='3'><hr></td></tr>";
{
echo "<tr>\n
<td>$story</td>\n
<td>$externallink</td>\n
</tr>\n";
echo "<tr><td colspan='3'><hr></td></tr>\n";
}
echo "</table>\n";

Any thoughts are appreciated!

barns101

3:25 pm on Oct 12, 2006 (gmt 0)

10+ Year Member



Before any content is sent to the browser (i.e. cookies, text) you can use this code:

<?php
header("Location: $externalurl");
?>

and that will send a 302 redirect request to the browser and take the visitor to $externalurl.

henry0

5:23 pm on Oct 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Or if OB is turned "On" in your php.ini
you could use barns's code from anywhere in your script

dave1236

5:55 pm on Oct 12, 2006 (gmt 0)

10+ Year Member



does a query of my db constitute output to my browser if i put the header after i have queried it...

eelixduppy

5:58 pm on Oct 12, 2006 (gmt 0)



>> does a query of my db constitute output to my browser if i put the header after i have queried it...

No; Only if something is echoed to the browser.

[added]
>>Many thanks to eelixduppy

You're welcome! I wish I could have helped more!
[/added]

[edited by: eelixduppy at 6:06 pm (utc) on Oct. 12, 2006]

rokec

6:03 pm on Oct 12, 2006 (gmt 0)

10+ Year Member



<SCRIPT LANGUAGE="JavaScript">
setTimeout("document.location = '<? echo($link);?>'", 5000);
</SCRIPT>

dave1236

5:20 pm on Oct 13, 2006 (gmt 0)

10+ Year Member



You guys are all great!

These suggestions both provided the results I desire...

<?php
header("Location: $externalurl");
?>

<SCRIPT LANGUAGE="JavaScript">
setTimeout("document.location = '<? echo($link);?>'", 5000);
</SCRIPT>

Thanks!

dave1236

5:25 pm on Oct 13, 2006 (gmt 0)

10+ Year Member



Buidling on the success you helped me achieve, I thought I could simply do the following...

I define a registered user by using the following:

.str_replace("tracking",$_SESSION['tracking'],$link).

I thought I could simply take that string above and replace

header("Location: $externalurl");

with

header("Location: $.str_replace("tracking",$_SESSION['tracking'],$link");

but that does not actually work! I then tried defining a new variable:

$URL = .str_replace("tracking",$_SESSION['tracking'],$link);
header("Location: $URL");

but that didn't work either...

Thoughts?

herculese

6:03 pm on Oct 14, 2006 (gmt 0)

10+ Year Member



Dave,

Make sure that the URL begins with an "http://", otherwise it might not redirect properly.

Also, there were a few errors in the code lines you pasted. They should read as follows:

-------------


header("Location: ". str_replace("tracking",$_SESSION['tracking'],$link));

-------------

$URL = str_replace("tracking",$_SESSION['tracking'],$link);
header("Location: $URL");

-------------

Hope it helps.

dave1236

1:40 am on Oct 15, 2006 (gmt 0)

10+ Year Member



Excellent! Thanks a bunch!