Author Topic: random website redirect  (Read 1227 times)

0 Members and 1 Guest are viewing this topic.

Offline abcdabcdTopic starter

  • Irregular
  • Posts: 23
    • View Profile
random website redirect
« on: January 28, 2009, 08:04:37 PM »
Hello,

I'm trying to setup php code (i'm new to this) what I'm trying to do is list 3 different websites in the php code and have the website visitor sent to any of the listed websites randomly.

Here's my current code which isn't working:

Code: [Select]
<?php
if($_SERVER['HTTP_REFERER'])
{
$random rand(03);
$aff_array = array("http://www.RANDOMSITE1.COM",
                  
"http://RANDOMSITE2.COM",
                  
"http://RANDOMSITE3.COM");
header('Location: $aff_array[$random]');
exit();
}
?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>

If you're experienced in php you can probably understand what the code does but, I'll explain anyway.

First there's a code that determines if the website visitor has a referer (ie: came from another website) if they have a referer I would like to them to be redirected randomly to one of the above websites.

If they don't have a referer then the website just loads it's content (reason I left the html tags at the bottom of the code) and doesn't redirect.

The person that posted this script (they removed it, I think it had too many errors) also said this:

"What that does, it creates a random number from 0 to 3, then in the aff_array put all of your different websites, only 1 of them will be chosen to be redirected to."

They also stated this:

"Just to clear things up again, this script changes the referrer from wherever you posted your link to the referrer of the website with above code and then randomly picks 1 of several websites and redirects to it. "

I checked yesterday on various php forums and was told that it's not possible to change the referer? Is that correct?


Thanks


Offline DeanWhitehouse

  • Addict
  • Posts: 2,507
  • Gender: Male
  • Mr.Grumpypants
    • View Profile
    • Dean Whitehouse - Portfolio
Re: random website redirect
« Reply #1 on: January 28, 2009, 08:08:31 PM »
What is this supposed to achieve?
if($_SERVER['HTTP_REFERER'])
{

also try
Code: [Select]
<?php
$aff_array 
= array("http://www.RANDOMSITE1.COM",
                  
"http://RANDOMSITE2.COM",
                  
"http://RANDOMSITE3.COM");
shuffle($aff_array); //to make it slightly more random ;)
header("Location: ".rand(0,2));//0,2 because array keys start at 0
exit();
?>

« Last Edit: January 28, 2009, 08:09:08 PM by Blade280891 »
Read the rules http://www.phpfreaks.com/page/rules-and-terms-of-service#toc_forum_guidelines

Quote
99.9% of my replies are correct, the other 0.1% means you asked the wrong question

Offline genericnumber1

  • Addict
  • Posts: 1,854
    • View Profile
Re: random website redirect
« Reply #2 on: January 28, 2009, 08:10:20 PM »
try..
Code: [Select]
<?php
if($_SERVER['HTTP_REFERER'])
{
   
$aff_array = array("http://www.RANDOMSITE1.COM",
                  
"http://RANDOMSITE2.COM",
                  
"http://RANDOMSITE3.COM");
   
header('Location: '.$aff_array[array_rand($aff_array)]);
   exit();
}
?>

Offline ratcateme

  • Devotee
  • Posts: 1,207
  • Gender: Male
    • View Profile
Re: random website redirect
« Reply #3 on: January 28, 2009, 08:10:58 PM »
i am fairly sure you cannot change the refer if you are using header redirects.
i think if you redirect with javascript or a meta tag (Google both you will find info on both) the redirect should change.
also a easier way to get a random element from a array shuffle() like this:
$aff_array shuffle(array("http://www.RANDOMSITE1.COM",
                  
"http://RANDOMSITE2.COM",
                  
"http://RANDOMSITE3.COM"));
header('Location: ' $aff_array[0]);

and just looking at your code
header('Location: $aff_array[$random]');
wont work but
header("Location: $aff_array[$random]");
will

Scott.

Offline ratcateme

  • Devotee
  • Posts: 1,207
  • Gender: Male
    • View Profile
Re: random website redirect
« Reply #4 on: January 28, 2009, 08:14:34 PM »
Code: [Select]
<?php
$aff_array 
= array("http://www.RANDOMSITE1.COM",
                  
"http://RANDOMSITE2.COM",
                  
"http://RANDOMSITE3.COM");
shuffle($aff_array); //to make it slightly more random ;)
header("Location: ".rand(0,2));//0,2 because array keys start at 0
exit();
?>

how exactly is that meant to work it would make a header looking like
Location: 0
or
Location: 1
should it look like
Code: [Select]
<?php
$aff_array 
= array("http://www.RANDOMSITE1.COM",
                  
"http://RANDOMSITE2.COM",
                  
"http://RANDOMSITE3.COM");
shuffle($aff_array); //to make it slightly more random ;)
header("Location: ".$aff_array[rand(0,2)]);//0,2 because array keys start at 0
exit();
?>
also i don't see the point in randomizing your selection twice if it is random then one randomization should yield the same spreed as 2 or 3 randomizations

Scott.


Offline genericnumber1

  • Addict
  • Posts: 1,854
    • View Profile
Re: random website redirect
« Reply #5 on: January 28, 2009, 08:15:36 PM »
Actually, shuffle() takes a reference and returns a bool.

So you'd need to do this..

Code: [Select]
<?php
$aff_array 
= array("http://www.RANDOMSITE1.COM",
                  
"http://RANDOMSITE2.COM",
                  
"http://RANDOMSITE3.COM");
shuffle($aff_array);
header('Location: ' $aff_array[0]);

It's annoying to remember which functions take references, especially when the function can possibly take such a small data set as shuffle() does in this case, so it's a completely understandable mistake.
« Last Edit: January 28, 2009, 08:16:43 PM by genericnumber1 »

Offline DeanWhitehouse

  • Addict
  • Posts: 2,507
  • Gender: Male
  • Mr.Grumpypants
    • View Profile
    • Dean Whitehouse - Portfolio
Re: random website redirect
« Reply #6 on: January 28, 2009, 08:17:03 PM »
Oops i missed out a bit should of been
Code: [Select]
<?php
$aff_array 
= array("http://www.RANDOMSITE1.COM",
                  
"http://RANDOMSITE2.COM",
                  
"http://RANDOMSITE3.COM");
shuffle($aff_array); //to make it slightly more random ;)
header("Location: ".$aff_array[rand(0,2)]);//0,2 because array keys start at 0
exit();
?>


Read the rules http://www.phpfreaks.com/page/rules-and-terms-of-service#toc_forum_guidelines

Quote
99.9% of my replies are correct, the other 0.1% means you asked the wrong question

Offline abcdabcdTopic starter

  • Irregular
  • Posts: 23
    • View Profile
Re: random website redirect
« Reply #7 on: January 28, 2009, 08:42:03 PM »
try..
Code: [Select]
<?php
if($_SERVER['HTTP_REFERER'])
{
   
$aff_array = array("http://www.RANDOMSITE1.COM",
                  
"http://RANDOMSITE2.COM",
                  
"http://RANDOMSITE3.COM");
   
header('Location: '.$aff_array[array_rand($aff_array)]);
   exit();
}
?>

Thanks for everybodys quick replies.

This is the code I tried since it was the only one with if($_SERVER['HTTP_REFERER']).

That code is used to forward visitors to one of those random websites if they have a website referer. Example: If they typed in the url directly into the browser they shouldn't have a referer so the current page will load and the user will not be redirected.

So for the other codes I'll try I guess, I just place:

Code: [Select]
<?php
if($_SERVER['HTTP_REFERER'])
{

above where the $aff_array = array code starts?

the error I got first was error in line 103 and I checked my script and line 103 there is nothing on there, it's a blank line right below the last </html> closing tag.

I deleted the line and tried again then I got the same error when I trying myself: 403 forbidden.

Here's what my code looks like now, is this correct:

Code: [Select]
<?php
if($_SERVER['HTTP_REFERER'])
{
$aff_array = array("http://www.RANDOMWEBSITE1.COM",
                  
"http://RANDOMWEBSITE2.COM",
                  
"http://RANDOMWEBSITE3.COM");
shuffle($aff_array); //to make it slightly more random ;)
header("Location: ".$aff_array[rand(0,2)]);//0,2 because array keys start at 0
exit();
?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>


Thanks again

Offline DeanWhitehouse

  • Addict
  • Posts: 2,507
  • Gender: Male
  • Mr.Grumpypants
    • View Profile
    • Dean Whitehouse - Portfolio
Re: random website redirect
« Reply #8 on: January 28, 2009, 08:43:24 PM »
But that will mean if they visit your site and go to another one of your sites pages then back to this one they will be redirected randomly
Read the rules http://www.phpfreaks.com/page/rules-and-terms-of-service#toc_forum_guidelines

Quote
99.9% of my replies are correct, the other 0.1% means you asked the wrong question

Offline abcdabcdTopic starter

  • Irregular
  • Posts: 23
    • View Profile
Re: random website redirect
« Reply #9 on: January 28, 2009, 08:47:06 PM »
But that will mean if they visit your site and go to another one of your sites pages then back to this one they will be redirected randomly

My website is only one page but, thanks for letting me know since that didn't occur to me.

Edit: actually I realized this could be a problem if they visit any website and click back on their browser same thing could occur correct? I'll have to test to find out.
« Last Edit: January 28, 2009, 08:50:18 PM by abcdabcd »

Offline abcdabcdTopic starter

  • Irregular
  • Posts: 23
    • View Profile
Re: random website redirect
« Reply #10 on: January 28, 2009, 08:56:53 PM »
Oops i missed out a bit should of been
Code: [Select]
<?php
$aff_array 
= array("http://www.RANDOMSITE1.COM",
                  
"http://RANDOMSITE2.COM",
                  
"http://RANDOMSITE3.COM");
shuffle($aff_array); //to make it slightly more random ;)
header("Location: ".$aff_array[rand(0,2)]);//0,2 because array keys start at 0
exit();
?>



I tried this code as well and got -

Parse error: syntax error, unexpected $end in xxxxxxx on line 102

this is what the code looks like:

Code: [Select]

<?php
if($_SERVER['HTTP_REFERER'])
{
$aff_array = array("http://www.RANDOMWEBSITE1.COM",
                  
"http://RANDOMWEBSITE2.COM",
                  
"http://RANDOMWEBSITE3.COM");
shuffle($aff_array); //to make it slightly more random ;)
header("Location: ".$aff_array[rand(0,2)]);//0,2 because array keys start at 0
exit();
?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>



here's are the last few lines of index.php and line 102 is </html>

Code: [Select]

</div><!-- end #footer -->
</div>
</body>
</html>



any help would be appreciated,

thanks


Offline DeanWhitehouse

  • Addict
  • Posts: 2,507
  • Gender: Male
  • Mr.Grumpypants
    • View Profile
    • Dean Whitehouse - Portfolio
Re: random website redirect
« Reply #11 on: January 28, 2009, 09:00:56 PM »
Code: [Select]
<?php
if($_SERVER['HTTP_REFERER'])
{
$aff_array = array("http://www.RANDOMWEBSITE1.COM",
                  
"http://RANDOMWEBSITE2.COM",
                  
"http://RANDOMWEBSITE3.COM");
shuffle($aff_array); //to make it slightly more random ;)
header("Location: ".$aff_array[rand(0,2)]);//0,2 because array keys start at 0
exit();
?>
should
be
Code: [Select]
<?php
if($_SERVER['HTTP_REFERER'])
{
$aff_array = array("http://www.RANDOMWEBSITE1.COM",
                  
"http://RANDOMWEBSITE2.COM",
                  
"http://RANDOMWEBSITE3.COM");
shuffle($aff_array); //to make it slightly more random ;)
header("Location: ".$aff_array[rand(0,2)]);//0,2 because array keys start at 0
exit();
}
?>
Read the rules http://www.phpfreaks.com/page/rules-and-terms-of-service#toc_forum_guidelines

Quote
99.9% of my replies are correct, the other 0.1% means you asked the wrong question

Offline abcdabcdTopic starter

  • Irregular
  • Posts: 23
    • View Profile
Re: random website redirect
« Reply #12 on: January 28, 2009, 09:40:27 PM »
Thanks!

somebody from another forum also recommended code to use and didnt' work so I combined both your codes together to get a working code and it works!

Code: [Select]

here it is:

[code]
<?php
if($_SERVER['HTTP_REFERER'] != "")
{
$random rand(02);
$aff_array = array("http://www.RANDOMWEBSITE1.COM",
                  
"http://RANDOMWEBSITE2.COM",
                  
"http://RANDOMWEBSITE3.COM");
header("Location: ".$aff_array[rand(0,2)]);
exit();
}
?>



Now the only problem is the referer but, there doesn't seem to be a way to change that with php headers.


Offline premiso

  • Karma Chameleon
  • Staff Alumni
  • Freak!
  • *
  • Posts: 6,717
  • Gender: Female
  • effing right
    • View Profile
    • PHP Help
Re: random website redirect
« Reply #13 on: January 28, 2009, 10:30:58 PM »
mmmm, you could just use array_rand()    no?

Code: [Select]
<?php
if($_SERVER['HTTP_REFERER'] != "") {
    
$aff_array = array("http://www.RANDOMWEBSITE1.COM",
                  
"http://RANDOMWEBSITE2.COM",
                  
"http://RANDOMWEBSITE3.COM");
    
header("Location: ".array_rand($aff_array));
    exit();
}
?>


If you want to send a false referrer, you would need to use cURL, however that is not really redirecting the user :)