• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

random url

Szalor

New Member
is there a script or something i can use so when ever i access it through my browser it redirects to a new page at random which i have listed in the file itself or external file

thanks
 
create a mysql table called randomurl with 1 field:
url (varchar(250), primary key)

connect to mysql & select the database, then use this as a script
PHP:
$first = rand(0,mysql_query("SELECT COUNT(*) FROM `randomurl`"))
$query = mysql_query("SELECT url FROM `randomurl` LIMIT $first,1");
$url = mysql_result($query,0,"url");
header("location: $url");

That should work, I havn't tested it though.
Then simply add the URLS through the database. Make sure you include http://

Been a while since I coded any php (couple o' months :O) so bear with me.
 
Last edited:
That looks like it should work. You never use $second after assigning it, though. And if performance is an issue, changing the first query to be a SELECT COUNT(*) instead of counting the number of rows returned would be quicker. But otherwise, yeah, that should do it.
 
shinyblogs.com said:
That looks like it should work. You never use $second after assigning it, though. And if performance is an issue, changing the first query to be a SELECT COUNT(*) instead of counting the number of rows returned would be quicker. But otherwise, yeah, that should do it.

Thanks.. made a couple of changes.
 
Instead of MySQL, you could use a regular PHP array.

PHP:
$urls = array(1 => "www.google.com",
"www.freewebspace.net/forums",
"example.com");
echo "<meta http-equiv='refresh' content='0;url=http://".$urls[array_rand($urls)]."'>";

Should work faster, as it doesn't have to query the database.
 
i'm gonna try tree's first, then i'll try the mysql, see which seems to workout better

thanks all
 
hey man, here's what i came up with, i dunno, but i think it's pretty good, create a text file and put a one link on each line and name the file links.txt

and here's the code

<?
$file = "links.txt";
$size= filesize($file);
$handle = fopen($file, "r");
$content= fread($handle, $size);
fclose($handle);
$links = explode("\n", "$content");
$count = count($links) - 1;
$rand = rand(0, $count);
$website = $links[$rand];

header("Location: $website");

?>
 
Tree's is best, I would say ..... no point in stressing the mysql server if there is no need ....
 
well Szalor asked at the begining for a script that will take the links from a file and that's what i did, i just midfied Tree's script, well kind of used other function, so now all he has to do is put the links in the links.txt file
 
In terms of speed though, trees would move quickest, but usability most prolly the file one, depends how you want to work I guess, I'm always of a mind you shouldn't open files if you don't have to....
 
But if you wanted (in future) to add more features, eg let users submit urls, or even count the amount of times the url was visited.. mySQL would be best as it is most flexible out of the three.
 
Back
Top