Forum Moderators: coopster
<?php
$filename = $_SERVER["DOCUMENT_ROOT"].'testing';
if ($fileContents = file_get_contents($filename)) {
$contentsArray = explode("\n", $fileContents);
$numLines = count($contentsArray)-1;
print($contentsArray[rand(0,$numLines)] . "\n"); // 1
print($contentsArray[rand(0,$numLines)] . "\n"); // 2
print($contentsArray[rand(0,$numLines)] . "\n"); // 3
print($contentsArray[rand(0,$numLines)] . "\n"); // 4
}
else {
die('Could not get contents of: ' . $filename);
}
?>
I believe it is half of what I need. I need to create a program that will take URL's in a text file and automatically forward your Web Browser to that url. This program will choose among links in a browser and post them to the webpage. I would like for it to not do that but instead forward to a new URL (randomly). Is this possible?
Note: I'm a total php noob but I love learning this stuff!
print($contentsArray[rand(0,$numLines)] . "\n"); // 1
print($contentsArray[rand(0,$numLines)] . "\n"); // 2
print($contentsArray[rand(0,$numLines)] . "\n"); // 3
print($contentsArray[rand(0,$numLines)] . "\n"); // 4
with:
$random_url = $contentsArray[rand(0,$numLines)];
header("Location: {$random_url}");
I found that code which someone posted as an answer (it was actually the original code I meant to post here!). Can we make this code forward you to an external website automatically?
There you go. That is the original script from the first post in this thread in action. The URLS it is choosing from are just random ones like www.google.com and www.yahoo.com.
You'll see it point towards [ysg.example.net...]
We need to make it just point to RANDOMSITE
I'd also prefer to use the last script i posted if that is possible. I really appreciate your help.
[edited by: coopster at 2:19 pm (utc) on Feb. 10, 2009]
[edit reason] no personal urls please TOS [webmasterworld.com] [/edit]
<?php
$filename = $_SERVER["DOCUMENT_ROOT"].'testing';
$how_many_to_show = 1;
if ($fileContents = file($filename)) {
shuffle($fileContents);
for ($i = 0; $i < $how_many_to_show; $i++) {
header('Location: '.$fileContents[rand(0,$numLines)]);
}
} else {
die('Could not get contents of: ' . $filename);
}
?>
If you don't want to add "http://" o every link, you can use this instead:
header("Location: [{$random_url}");...]