Forum Moderators: coopster

Message Too Old, No Replies

PHP Redirecting with Refresh or Location?

refresh location redirect

         

trnsfrmr

3:43 pm on Apr 24, 2007 (gmt 0)

10+ Year Member



Hi, I was wondering if anyone could explain the difference between:
Header("Refresh: 2;$url");
and
Header("Location: $url");
with particular reference to how the referring domain looks/ is handle by the browser?

I'm trying to understand the mechanics of tracking script I'm using: [webmasterworld.com...]

[edited by: encyclo at 4:53 pm (utc) on April 24, 2007]
[edit reason] fixed thread link [/edit]

joelgreen

4:14 pm on Apr 24, 2007 (gmt 0)

10+ Year Member



Here is my opinion, not sure if it is correct.

- browser on example.com/page1.html
- Header("Refresh: 2;example.com/page2.html") sent to browser
- browser waits 2 seconds, redirects to example.com/page2.html, and example.com/page2.html shown in the browser URL area

- browser on example.com/page1.html
- Header("Location: example.com/page2.html
") sent to browser
- browser redirects to example.com/page2.html, and example.com/page2.html shown in the browser URL area

Difference may be in the http status code.
Seems like Header->Location send 302, and i suspect Refresh sends another http status.

This may be more important to crawlers than regular surfers.

Again, this is only my opinion, never tried/tested it.

eelixduppy

4:20 pm on Apr 24, 2007 (gmt 0)



Refresh
isn't officially part of HTTP 1.1 and is usually used in HTML:

<META HTTP-EQUIV="Refresh" CONTENT="2; URL=http://host/path">

Refresh
, however, is only supported as a header in Internet Explorer and Netscape, I believe. Therefore you will run into some trouble there. If you want to redirect somewhere, use
Location
.

trnsfrmr

8:05 pm on Apr 24, 2007 (gmt 0)

10+ Year Member



Ahah! Yes, Refresh does seem to be incredibly unreliable, I just discovered my script was failing every time in IE 6. So that explains where my clicks are going. Probably should've have tested more thoroughly in the first place. ;)

My only problem with Location is that it appears to work too quickly and doesn't allow my Javascript tracking a chance to call it's functions.

Here is my code:


<?php
// GET PARAMS
$pid=$_GET['pid'];
$aid=$_GET['aid'];
$sid=$_GET['sid'];
$url=$_GET['url'];
if (strlen($sid) < 1){
$sid = 'organic';
}
// BUILD URL
$url = "http://www.example.net/click-$pid-$aid?sid=$sid&url=$url";
// PRINT
echo '<p>Loading...</p>';
echo '<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">';
echo '</script>';
echo '<script type="text/javascript">';
echo '_uacct = "UA-#*$!#*$!X-X";';
echo 'urchinTracker("/tracking/");';
echo '</script>';
// REDIRECT TO URL
Header("Location: $url");
exit;
?>

Is it possible to use 'Location' redirects and keep Javascript tracking code working?

[edited by: eelixduppy at 8:10 pm (utc) on April 24, 2007]
[edit reason] exemplified [/edit]

trnsfrmr

2:56 pm on Apr 25, 2007 (gmt 0)

10+ Year Member



Any ideas?

eelixduppy

11:52 pm on Apr 28, 2007 (gmt 0)



Well, you cannot send info to the browser and then change the page's headers afterwards. You can, however, use JavaScript to redirect the page after the tracking information has been sent using something like this:

document.location.href = "http://www.example.com";

This, I feel, will be your best bet in terms of redirecting the page because you cannot change the headers. You can, as you have suggested, use a REFRESH, but I'd use a meta refresh and not set the header. Both the javascript and the meta refresh have disadvantages as both of them can be disabled.

trnsfrmr

6:44 pm on May 3, 2007 (gmt 0)

10+ Year Member



Thanks!