CoolAffiilate Posted January 6, 2008 Share Posted January 6, 2008 Hello everyone, Maybe you can help me figure out what I am doing wrong. I am trying to split test 2 affiliate links. The user is sent to a PHP file and the file will randomly pick a link, and forward the user. But, the code is not working. Here is what I have tried. Try #1 <?php $webarray = array("http://www.site1.com", "http://www.site2.com", "http://www.site3.com", "http://www.site4.com"; $rand = rand(0,size($webarray)); header( 'Location: '.$webarray[$rand]) ; ?> Gives an Error Line 5. Try #2 <?php $rand = rand(1,2); if($rand =1) header( 'Location: http://www.site1.com' ) ; else header('Location: http://www.site2.com); ?> Just goes to the 1st rand. Will not even go to the 2nd site. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/84749-random-links/ Share on other sites More sharing options...
trq Posted January 6, 2008 Share Posted January 6, 2008 You never closed your array. <?php $webarray = array("http://www.site1.com", "http://www.site2.com", "http://www.site3.com", "http://www.site4.com"); $rand = rand(0,size($webarray)); header( 'Location: '.$webarray[$rand]) ; ?> Quote Link to comment https://forums.phpfreaks.com/topic/84749-random-links/#findComment-431872 Share on other sites More sharing options...
CoolAffiilate Posted January 6, 2008 Author Share Posted January 6, 2008 How do I close the array? Quote Link to comment https://forums.phpfreaks.com/topic/84749-random-links/#findComment-431874 Share on other sites More sharing options...
wildteen88 Posted January 6, 2008 Share Posted January 6, 2008 How do I close the array? Umm.. Thorpe did it for you. Look at the code above! Quote Link to comment https://forums.phpfreaks.com/topic/84749-random-links/#findComment-431919 Share on other sites More sharing options...
CoolAffiilate Posted January 6, 2008 Author Share Posted January 6, 2008 Fatal error: Call to undefined function size() in random.php on line 6 When tested the above fixed code gives this error. Quote Link to comment https://forums.phpfreaks.com/topic/84749-random-links/#findComment-432035 Share on other sites More sharing options...
mike1313 Posted January 6, 2008 Share Posted January 6, 2008 Is size your own function? If so post code. Quote Link to comment https://forums.phpfreaks.com/topic/84749-random-links/#findComment-432037 Share on other sites More sharing options...
CoolAffiilate Posted January 6, 2008 Author Share Posted January 6, 2008 Nope, I got this code from a programming buddy. I really have no clue. Quote Link to comment https://forums.phpfreaks.com/topic/84749-random-links/#findComment-432038 Share on other sites More sharing options...
john010117 Posted January 6, 2008 Share Posted January 6, 2008 <?php $webarray = array("http://www.site1.com", "http://www.site2.com", "http://www.site3.com", "http://www.site4.com" ); $rand = rand(0,sizeof($webarray)); header( 'Location: '.$webarray[$rand]) ; ?> Quote Link to comment https://forums.phpfreaks.com/topic/84749-random-links/#findComment-432041 Share on other sites More sharing options...
redarrow Posted January 6, 2008 Share Posted January 6, 2008 what about this way then............ <?php $webarray = array("http://www.site1.com", "http://www.google.com", "http://www.msn.com", "http://www.yahho.com"); $a=array_flip($webarray); $b=array_rand($a); header("location: $b"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/84749-random-links/#findComment-432049 Share on other sites More sharing options...
CoolAffiilate Posted January 6, 2008 Author Share Posted January 6, 2008 Ok, one more thing. When i use the script posted above. I have a homepage with a link pointing the PHP file, sometimes when I click the link, it will just go back to the current page. Why? Quote Link to comment https://forums.phpfreaks.com/topic/84749-random-links/#findComment-432051 Share on other sites More sharing options...
mike1313 Posted January 6, 2008 Share Posted January 6, 2008 How do you have the code for your <a> setup? Quote Link to comment https://forums.phpfreaks.com/topic/84749-random-links/#findComment-432053 Share on other sites More sharing options...
CoolAffiilate Posted January 6, 2008 Author Share Posted January 6, 2008 ok, you edited it... Works great now! Thanks for your help everyone! Quote Link to comment https://forums.phpfreaks.com/topic/84749-random-links/#findComment-432054 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.