Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
PHP - Redirect to Random URLs
Old 01-28-2009, 09:19 PM PHP - Redirect to Random URLs
Novice Talker

Posts: 14
Trades: 0
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:
<?php
if($_SERVER['HTTP_REFERER'])
{
$random = rand(0, 3);
$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 posted a similar thread yesterday on here about setting up a url redirect for just one site. I also asked about changing the referer, I was told that it's not possible to change the referer. I'm not trying to create 2 posts on the same subject. My main concern is the random url code.

Although yesterday when I was wondering how to change the referer I was trying to change a different website/link as the referer and they said it can't be done.

Example: User is on Website1 ----> clicks link to Website2 (normally now referer is set as Website1) -------> Website3 (above website with random urls) -----> Website 4 (user is now redirected to the random website and referer says it is Website3). Would this be possible? In yesterdays post, I was trying to change the referer to "Website2" but, I would like to change it to "Website3" if possible.

If not, it's ok, I'm not going to keep discussing it. Just wondering if that last scenario could work.


Thanks

Last edited by abcdabcd; 01-28-2009 at 09:22 PM..
abcdabcd is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-28-2009, 10:02 PM Re: PHP - Redirect to Random URLs
mad_willsy's Avatar
Super Spam Talker

Latest Blog Post:
PC World Placement
Posts: 801
Name: Will Craig
Location: Cheltenham, Gloucestershire, UK
Trades: 0
Your problems would be so much easier solved if you were clearer in what you say. Your example is so ambigious, yet what you are trying to say can probably be acchieved.

Your code tells me what you are trying to do is redirect a user to a random website IF they were sent to your website from another website such as clicking on your link from google. Why you would want to do this is beyond me, however if this is what you want to do then I'd try the following:

PHP Code:
<?php
if($_SERVER['HTTP_REFERER'] != "")
{
$random rand(02);
$aff_array = array("http://www.RANDOMSITE1.COM",
                  
"http://RANDOMSITE2.COM",
                  
"http://RANDOMSITE3.COM");
header('Location: $aff_array[$random]');
exit();
}
?>
When posting, try and define the user experiance.
E.g. User clicks on my link from another website, php redirects them to a random website. User types my URL into the browser OR their browser is blocking referer headers, they see my website.

That explains what the code mentioned does, which is why I can't see a use. If this isn't what you are trying to do, try explaining what you do want in the way I just did.
__________________
Wont :P

Please login or register to view this content. Registration is FREE

Last edited by mad_willsy; 01-28-2009 at 10:03 PM..
mad_willsy is offline
Reply With Quote
View Public Profile Visit mad_willsy's homepage!
 
Old 01-28-2009, 10:39 PM Re: PHP - Redirect to Random URLs
Novice Talker

Posts: 14
Trades: 0
yes you're correct in what i'm trying to do, sorry about that,

I tried your code and it didn't work and 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!

here it is:

Code:
<?php
if($_SERVER['HTTP_REFERER'] != "")
{
$random = rand(0, 2);
$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 do that with php headers.
abcdabcd is offline
Reply With Quote
View Public Profile
 
Old 01-28-2009, 10:42 PM Re: PHP - Redirect to Random URLs
mad_willsy's Avatar
Super Spam Talker

Latest Blog Post:
PC World Placement
Posts: 801
Name: Will Craig
Location: Cheltenham, Gloucestershire, UK
Trades: 0
PHP Code:
<?php
if($_SERVER['HTTP_REFERER'] != "")
{
$aff_array = array("http://www.RANDOMWEBSITE1.COM",
                  
"http://RANDOMWEBSITE2.COM",
                  
"http://RANDOMWEBSITE3.COM");
header("Location: ".$aff_array[rand(0,2)]);
exit();
}
?>
You don't need the $random variable declaration. Can I ask why you wanted to do this?
__________________
Wont :P

Please login or register to view this content. Registration is FREE
mad_willsy is offline
Reply With Quote
View Public Profile Visit mad_willsy's homepage!
 
Old 01-28-2009, 10:45 PM Re: PHP - Redirect to Random URLs
Novice Talker

Posts: 14
Trades: 0
Quote:
Originally Posted by mad_willsy View Post
PHP Code:
<?php
if($_SERVER['HTTP_REFERER'] != "")
{
$aff_array = array("http://www.RANDOMWEBSITE1.COM",
                  
"http://RANDOMWEBSITE2.COM",
                  
"http://RANDOMWEBSITE3.COM");
header("Location: ".$aff_array[rand(0,2)]);
exit();
}
?>
You don't need the $random variable declaration. Can I ask why you wanted to do this?
it's was what I was advised from here: http://www.phpfreaks.com/forums/inde...,236222.0.html
abcdabcd is offline
Reply With Quote
View Public Profile
 
Old 01-28-2009, 11:13 PM Re: PHP - Redirect to Random URLs
mad_willsy's Avatar
Super Spam Talker

Latest Blog Post:
PC World Placement
Posts: 801
Name: Will Craig
Location: Cheltenham, Gloucestershire, UK
Trades: 0
Thats just the same as this thread, doesn't describe why you need this code anyway. Also you do realise unless the referer is blocked by the browser or the user goes to this page by entering into the address bar, the if statement will allways be true. Refrerer INCLUDES your webpages, not just external websites.
__________________
Wont :P

Please login or register to view this content. Registration is FREE
mad_willsy is offline
Reply With Quote
View Public Profile Visit mad_willsy's homepage!
 
Old 01-29-2009, 12:26 AM Re: PHP - Redirect to Random URLs
stevej's Avatar
Professional Multitasker

Posts: 996
Location: Not positive
Trades: 0
If you're having problems with your current code, you could just use this:
PHP Code:
<?php
$num 
Rand (1,3);
switch (
$num) {
case 
1header('Location: http://RANDOMSITE1.COM');
break;
case 
2header('Location: http://RANDOMSITE2.COM');
break;
case 
3header('Location: http://RANDOMSITE3.COM');
break;
}
?>
- Steve
stevej is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to PHP - Redirect to Random URLs
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.48927 seconds with 12 queries