I have a script that sends the user to a specific url to download files. I want to change the script to include several URL's instead of just one. So instead of the users going to Location: http://WEBSITE.COM/$id I want the script to randomly choose to send them to Location: http://WEBSITE.COM/$id or Location: http://WEBSITE2.COM/$id or Location: http://WEBSITE3.COM/$id Here is the beginning portion of my php code. <? session_cache_limiter('public'); session_start(); require 'init.php'; $id=$_GET['id']; $fl=$_GET['fl']; $go=$_POST['www_form_submit']; $id2=$_POST['id']; if($_SESSION['auth'] && $_SESSION['auth']['ip_address'] == ip2long(get_real_ip()) && isset($_SESSION['auth']['sid'])) { $ok = 1; } else { header("Location: /signin.html"); exit(); } if($_SESSION['auth']['donor']==1){ //$ms = rand(1, 2); header("Location: http://WEBSITE.COM/$id"); exit; } $url=$id; if($go!=1){ ?> PHP: I am also a beginner in PHP codes. So if you have a solution in how I can modify this code to include more than 1 url please include as much detail as possible.
There are lots of ways you can do this; a simple solution would be something like this; I am assuming you actually have a different address rather than just a number on the end (in which case you can just append a random number) Using your code as a base, at the top, speicify your URLs # Here, you create an array of your website URLs $websites = array(); $websites[] = "http://www.WEBSITE1.com"; $websites[] = "http://www.WEBSITE2.com"; $websites[] = "http://www.WEBSITE3.com"; PHP: Then, when you refer the visitor, you just need to use; # Call the random number generator FIRST as it can help to improve the randomness rand(); $url = $websites[rand(0, sizeof($websites)-1)]; header("Location: $url/$id"); PHP:
starting/calling/seeding the RNG is no longer required >= PHP v.4.2.0 and has absolutely no effect on randomness as the RNG is automatically seeded when the random function is called. <?php $sites=array ( 'google.com', 'yahoo.com', 'myspace.com', ); header('Location: http://www.'.$sites[rand(0,count($sites)-1)].'/'.$id); ?> PHP: for performance, I prefer concatenation and less code as php doesnt parse the double-quoted strings. additionally it is wise not to replicate redundant data when said data can be included in the code once, instead of many times in the data. Makes for an overall smaller app and less space consumed for the data. this also can be database driven instead of array driven and you can add other data if you ever want to add or incorporate into existing administration functionality, like id, name, summary, permissions, etc. The latter is especially helpful if your sites data grows over time.
Of course its not required - if you read what was written - and has been proven many times due to dubious behaviour with libc, it gives a more random number, hence the point. (Which is why mt_rand was implemented, but this isnt the place for discussion on the randomness returned)