Forum Moderators: coopster

Message Too Old, No Replies

Random URL Forwarding

         

yousillygoose

5:07 am on Feb 9, 2009 (gmt 0)

10+ Year Member



Hello. I found this:

<?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!

STeeL

5:17 am on Feb 9, 2009 (gmt 0)

10+ Year Member



Try replacing these lines:

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}");

IanKelley

5:17 am on Feb 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yep that script is basically what you need.

You would replace the 4 print statements with:

header('Location: '.$contentsArray[rand(0,$numLines)]);

[Edit: The above will work too, we posted at the same time :-) ]

[edited by: IanKelley at 5:18 am (utc) on Feb. 9, 2009]

yousillygoose

5:26 am on Feb 9, 2009 (gmt 0)

10+ Year Member



Well I want it to forward to an external website. Those codes forward it to my domain/randomaddress. I want it to forward to an external random address. It's sooo close!

yousillygoose

5:30 am on Feb 9, 2009 (gmt 0)

10+ Year Member



<?php
$filename = $_SERVER["DOCUMENT_ROOT"].'/inc/pub/pub_array.inc';
$how_many_to_show = 4;
if ($fileContents = file($filename)) {
shuffle($fileContents);
for ($i = 0; $i < $how_many_to_show; $i++) {
print($fileContents[$i]. '<br />');
}
} else {
die('Could not get contents of: ' . $filename);
}
?>

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?

STeeL

5:40 am on Feb 9, 2009 (gmt 0)

10+ Year Member



When you were printing out URLs using those four lines, can you give us an example of what your sample URL looks like?

yousillygoose

5:43 am on Feb 9, 2009 (gmt 0)

10+ Year Member



<snipped link>

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]

yousillygoose

5:46 am on Feb 9, 2009 (gmt 0)

10+ Year Member



This is the latest edition of the script. I used the little scripting knowledge I have to add your editions to the second script that I posted. It is still putting it after my domain rather than forwarding it straight to the URL. I'm trying to not be TOTALLY useless!

<?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);
}
?>

STeeL

5:49 am on Feb 9, 2009 (gmt 0)

10+ Year Member



To redirect properly your URL has to include "http://", for example "http://www.google.com".

If you don't want to add "http://" o every link, you can use this instead:

header("Location: [{$random_url}");...]

yousillygoose

5:53 am on Feb 9, 2009 (gmt 0)

10+ Year Member



YOU ARE AWESOME! I figure it is no trouble to add the http (and I can write a little bash script to check if http is there and if not to simply add it on. I know some java and some more bash but I'm php clueless. THANK YOU SOOOOOO MUCH for the help. You have no idea how much I appreciate it!

STeeL

6:13 am on Feb 9, 2009 (gmt 0)

10+ Year Member



You're welcome. You can also do it using PHP:

$url = $contentsArray[rand(0,$numLines)];
$url = (substr($url, 0, 7) == 'http://') ? $url : 'http://'.$url;
header("Location: {$url}");

Line in the middle will check if first 7 characters are equal to "http://" and if not - will add them to url.

g1smd

7:33 pm on Feb 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



With just the location header present, you have a 302 redirect.

Are you sure that is what you want? Or maybe a 301 instead?