How Could I Do This?

Andrew Scherer

MarketersCenter.com
Feb 12, 2009
5,778
124
0
Mexico
www.marketerscenter.com
I am looking for a simple script that I can load onto an HTML document which will display a different pre-determined URL at random each time its loaded.

This is for a little experiment I'm doing with Microworkers. If you're familiar with the site its an outsourcing site for super small jobs. One of the jobs you can create is for forum posting.

I had an idea of loading a script like this with 100 forums and directing Microworkers to the page. Each worker could potentially get a different forum to make 3 posts and drop a signature/link on.
 


You could do it with html/javascript, but ultimately it would be random rather than different

To ensure each load is different, you'd need to use a scripting language and a database, i.e. php and mysql.

It's a very easy job for someone to do, max of 20 mins to code with a small admin interface to manage the list and track which urls have been used.
 
<?php
function RandomLine($filename) {
$lines = file($filename) ;
return $lines[array_rand($lines)] ;
}
$url = RandomLine("TEXT FILE HERE");
echo $url;

/* Redirect browser */
header("Location: $url");
/* Make sure that code below does not get executed when we redirect. */
exit;
?>

something liek that should work
 
  • Like
Reactions: Andrew Scherer
<?php
function RandomLine($filename) {
$lines = file($filename) ;
return $lines[array_rand($lines)] ;
}
$url = RandomLine("TEXT FILE HERE");
echo $url;

/* Redirect browser */
header("Location: $url");
/* Make sure that code below does not get executed when we redirect. */
exit;
?>

something liek that should work


I remember I did something very similar to this awhile back, that script will work, but like another poster above me said, that will only produce a random URL out of a list, but not guaranteeing that it is unique.

There is another PHP script that is almost identical to that one, except for it keeps count with an external text file. I.e, the first time the page is loaded it saves a text file with '1' in it... then the next time it is loaded it resaves it with the number '2' in it... then 3 and so forth.

The script reads the integer inside the text file and selects the corresponding URL from a list. This way you wouldn't be getting repeat URLs.
 
I remember I did something very similar to this awhile back, that script will work, but like another poster above me said, that will only produce a random URL out of a list, but not guaranteeing that it is unique.

There is another PHP script that is almost identical to that one, except for it keeps count with an external text file. I.e, the first time the page is loaded it saves a text file with '1' in it... then the next time it is loaded it resaves it with the number '2' in it... then 3 and so forth.

The script reads the integer inside the text file and selects the corresponding URL from a list. This way you wouldn't be getting repeat URLs.

I need that!
 
Voila

Ugh, I hate myself sometimes :)

Ok, sooooooooo....I got bored and wanted to brush up on some PHP (which I'm only vaguely familiar with, I just have a knack for logic).. after studying some code, and browsing through some PHP syntax and operator references. I coded what I described above. It can take a list of URLs and cycle through them without the possibility of duplicates with virtually as many URLs as you want (roughly 1,000,000 ^ 1000th power lol). Also once it reaches the end of the list it will start over again.

There are 3 files you must upload all in one directory

1. This script saved as a file with a php extension.

2. A text file saved as 'listofurls.txt' ... self-explanatory.

3. A text file saved as 'count.txt' ... the only thing written in this text file should be the number '0' without the quotes. Anytime you upload a new URL list make sure to re-upload this file with '0' in it.

Code:
<?php

$file = fopen("count.txt", "r");
$count = fread($file, 1024);
fclose($file);
$input = file('listofurls.txt');

if (array_key_exists($count,$input)) {
    
    $order = $count;
    $count = $count + 1;
    $file = fopen("count.txt", "w");
    fwrite($file, $count);
    fclose($file);
    header( 'Location: '.$input[$order] ) ;
    exit;
}

else {

    $count=0;
    $order = $count;
    $count = $count + 1;
    $file = fopen("count.txt", "w");
    fwrite($file, $count);
    fclose($file);
    header( 'Location: '.$input[$order] ) ;
    exit;
}

?>
 
  • Like
Reactions: kimboslice
No problem,

I'm actually thinking about coding IP recognition into it, where it basically assigns each IP address it's own URL and saves it to a textfile. Then every time someone visits the pages it checks there IP against the textfile, if it's already there it just reloads the same URL it did before.

This would help protect against someone accidentally or purposefully cycling through all the URLs and screwing up the rotation. I don't know, just a thought.
 
I'm actually thinking about coding IP recognition into it, where it basically assigns each IP address it's own URL and saves it to a textfile. Then every time someone visits the pages it checks there IP against the textfile, if it's already there it just reloads the same URL it did before.

we have this by default on many of our sites. the way we do it is to create a MySQL record for each user at the time of his first visit (identified by IP). there are many advantages of doing that - for example, you can save CSS style settings and split-test them (or even splittest any other aspect of your site if it's entirely PHP-driven) without the user noticing as long as he comes back from the same IP. another option is remembering where this user last went on the site and showing that at this next visit. yet another that we also actively use is to give a smooth transition from anon to registered posting by taking over stuff previously done from this IP.
 
No problem,

I'm actually thinking about coding IP recognition into it, where it basically assigns each IP address it's own URL and saves it to a textfile. Then every time someone visits the pages it checks there IP against the textfile, if it's already there it just reloads the same URL it did before.

This would help protect against someone accidentally or purposefully cycling through all the URLs and screwing up the rotation. I don't know, just a thought.

I haven't actually tested this (but it should work), but here's how you'd basically go about it if you just wanted to use a text file...

Code:
<?php
$db = 'dbase.txt';
$ip = $_SERVER['REMOTE_ADDR'];

$urls = array(
	'http://url1.com',
	'http://url1.com',
);

function checkIp($ip, $db) {
	$file = file($db);
	$found = 0;

	foreach($file as $key => $val) {
		list($addy, $index) = explode("|", $val);
		if($ip == $addy) {
			$found++;
			$finInfo = $val;
		}
	}
	
	if($found > 0) {
		return $finInfo;
	}
	else
	{
		return false;
	}
}

function countEntries($db) {
	$file = file($db);
	$entries = count($file);
	
	return $entries;
}

if(checkIp($ip, $db)) {
	//ID already exists!
	$url_id = checkIp($ip, $db);
	
	echo $urls[$url_id];
}
else
{
	//doesnt exist!
	$fp = fopen($db, "a");
	$next = countEntries($db)+1;
	$syntax = $ip.'|'.$next."\n";
	fwrite($fp, $syntax);
	
	echo $urls[$next];
}
?>
 
Last edited:
<?php
function RandomLine($filename) {
$lines = file($filename) ;
return $lines[array_rand($lines)] ;
}
$url = RandomLine("TEXT FILE HERE");
echo $url;

/* Redirect browser */
header("Location: $url");
/* Make sure that code below does not get executed when we redirect. */
exit;
?>
this code might help you
 
<?php
function RandomLine($filename) {
$lines = file($filename) ;
return $lines[array_rand($lines)] ;
}
$url = RandomLine("TEXT FILE HERE");
echo $url;

/* Redirect browser */
header("Location: $url");
/* Make sure that code below does not get executed when we redirect. */
exit;
?>
this code might help you


Read before posting. Someone has googled before you.