Jump to content

header(); presents infinite loop in IE, works on other browsers?


jParnell
Go to solution Solved by jParnell,

Recommended Posts

OK, I'll keep this short n sweet.

 

Goal:

 

If a specific variable is declared in the URL, assign it to a session and refresh the page without the variable. Here's my code:

 

if(isset($_SESSION['referrer'])){
	$referrer = $_SESSION['referrer'];
}else{
	if(isset($_REQUEST['referrer'])){
		$referrer = $_REQUEST['referrer'];
	}else{		
		$referrer = $no_referrer;
	}
}
				
$_SESSION['referrer'] = $referrer;
				
if(isset($_REQUEST['referrer'])){
	header("Refresh:0, url=/");
}

Expected result:

 

If the url http://testsite.com?referrer=abc123 is entered, check for existing session. If it doesn't exist, create one from $_REQUEST['referrer']. Then, check if $_REQUEST['referrer'] exists. If it does, immediately go to the root of the domain (this is located at the root of the domain) so that the url displayed is http://testsite.com instead of the ugly referrer showing.

 

Actual Result:

 

Works great on Chrome, Firefox, Safari, Opera, and Android 4.2.2's web browser, however in IE 8 through IE 11, that code presents an infinite header(); loop. It's executing the header("Refresh:0, url=/"); regardless of whether $_REQUEST['referrer'] is set or not

Link to comment
Share on other sites

ok, so I did an echo $_REQUEST['referrer']; in my header. In Chrome, Firefox, Opera, and Safari, I get the following error message:

 

Notice: Undefined index: referrer in C:\xampp\htdocs\index.php on line 36

 

Which is completely to be expected after the refresh. However, on IE (every version) it is echoing the contents of $_REQUEST['referrer'] as if "?referrer=abc123" were still appended to the URL.

 

If I had to harbor a guess as to why IE is jacking it up,  I would say its the lines: 

if(isset($_REQUEST['referrer'])){
	$referrer = $_REQUEST['referrer'];
}

I think IE is thinking that $referrer should literally be $_REQUEST['referrer'] instead of the value of the request global variable....

 

Anybody have any idea how to fix this?

Link to comment
Share on other sites

 

When you do this:

	}else{	
$referrer = $no_referrer;

you set the variable equal to an empty variable unless there is code we're not shown.

Did you mean to do this:

	}else{	
$referrer = 'no_referrer';

 

I thought I got the $no_referrer variable in there, apparently not. That variable is set to 0 by default. Content on the site is called dynamically, dependent on the $referral variable. The $referral variable is defining the affiliate link of the site.

 

As it says in the manual, code should not rely on the referrer because different user agents treat it differently, and you're never guaranteed that it will have a value, or have the value you think it should have.

 

I'll admit to being no expert in PHP, but as I stated above, the $referrer is the affiliate link. The link is generated from a mysql query dependent on the affiliate's id, the primary row in the database, which is 'referrer'. Each affiliate has a link in their profile.

<?php

session_start();

include('dbconnect.php');

$username = $_SESSION['username'];

$referralid = mysql_query("SELECT ref_id FROM my_table WHERE username = $username");

echo "Your affiliate link is <span class='bold'>http://website.com/?referral=?" . $referralid . "</span><br />";

?>

As I said, I'm no PHP expert, so this is the logical way I saw to creating dynamic affiliate links. If you have a suggestion for an alternative method, I'm open to it. In every single test I've done in my sandbox environment, it has worked flawlessly in every browser, until I decided to add the header() portion, in which case only IE is flubbing it up.

 

I don't know, but in your shoes I'd try first setting the url on the refresh to a full domain name and path or if that doesn't work refresh with a ?dummy=done

 

The problem is on the execution, IE is maintaining the ?referrer=abc123 inside the url. it's adding to my header(); function, therefore when always passes the if() check.

Link to comment
Share on other sites

  • Solution

I've been banging my head against the wall for hours on this, and I finally figured it out.... I changed

if(isset($_REQUEST['referrer'])){
	header("Refresh:0, url=/");
}

to

if(isset($_REQUEST['referrer'])){
	header("Refresh:0; url=/");
}

Low and behold, it works. changing a single character un-breaks it with IE.

 

As much as I abhor IE, at least it makes you make sure your code is correct

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.