Is This PHP Code Correct?

DJPaulyD

Supreme Member
Joined
Aug 16, 2014
Messages
1,217
Reaction score
679
Code:
<?php

$urls[] = 'http://www.google.com';
$urls[] = 'http://www.yahoo.com';

srand ((double) microtime( )*10000000);
$random = rand(0,count($urls));


header('location:' . $urls[$random]);

exit;
?>

Just trying to make a URL rotator
 
Code:
<body>
<div id=banner>
<?php
//Add as many links you want
$mylink[1] = '<a href="http://www.pegor.com">
<img src="images/banner1.jpg" width="468" height="60" alt="some_tex_here">
</a>';
$mylink[2] = '<a href="http://www.lifestinks.info">
<img src="images/banner2.jpg" width="468" height="60" alt="some_tex_here"></a>';
$mylink[3] = '<a href="http://www.mozilla.org"><img src="images/banner3.jpg" width="468" height="60" alt="some_tex_here"></a>';
//$mylink[4] = .....
//$mylink[5] = ....

// this will count your links itself and select a random one
$id = rand(1,count($mylink));

// this will display the random link
echo $mylink[$id];
?>
</div>
 
to ignore errors if any change ur file from .html to .php with the above code inside it and if there are more links i would suggest you to make a text file with links in it and then call them from the code for performance
 
The code has an off-by-one issue/bug with the random number generation. This:
  • rand(0,count($urls));
Generates a number from 0 to 2. It should be:
  • rand(0,count($urls)-1);
Which generates a random number from 0 to 1.

Other than that, the code works fine. Having a custom seed value in this line:
  • srand ((double) microtime( )*10000000);
Is unnecessary and could be removed. This line is also unnecessary:
  • exit;
It actually won't execute, since the user will have already been redirected off of the page at that point.

This solution from https://www.blackhatworld.com/seo/best-way-to-redirect-urls.993463/#post-10640077 works as well and does the same thing:

Code:
<?php
$urls = [
          'https://www.someurl.com/'        ,
          'https://www.someotherurl.com/'   ,
          'https://www.anothersomeurl.com/' ,
          'https://www.someurllmao.com/'    ,
          'https://www.something.com/'      ,
          'https://www.ha-ha-its-a-url.com/',
        ];

$url = $urls[rand(0,sizeof($urls)-1)];

header('Location: '.$url);
?>
 
place all url's in "urls.txt", 1 per line..

If we want to code golf it...
PHP:
<?php
header('location: ' . file("urls.txt")[mt_rand(0,count(file('urls.txt'))-1)]);
?>

Otherwise
PHP:
<?php

$files = file("urls.txt");

$url = $files[mt_rand(0,count($files)-1)];

header('location: ' . $url);
?>

I'd avoid using rand personally, mt_rand is significantly better(and still not great). see https://3v4l.org/Qo7nL - the smaller the pool, the far less random it gets.

I'm just being pedantic really, I don't think your URL rotation needs to be that great.

Pulling URL's into a separate file is nice though, looks neater - less risk of missing a comma or a quote in an array, etc.
 
Here's a simpler way to do it:

Code:
$urls = array(
    'link1',
    'link2'
);

header('Location: ' . $urls[array_rand($urls)]);
 
If you want to set an array that way use something like

$urls = array();
$urls[] = 'yoururl';
$urls[] = 'another url';

This will allow u to add url to the array on the fly. An in a numric order so u could access the array like $urls[1]; which would return another url in above example

An example if u was to pull the urls from a database an have pushed to an array.
Code:
$connect = your mysqli fetch row command
$url = array();
While($connect as $r){
    $url[] = $r[urls];
}
print_r($url);
 
Last edited:
Back
Top