Page 1 of 1

Random Banner/ Ip/Hostmask ban

Posted: Tue Mar 11, 2003 11:07 am
by Raahul
how do i make a random banner rotating thing. Means, the code will read a text file containing links to all images (each link in a new line). It will select a random banner and display it. This is my first question.

My second question. See the script would read a text file with ip and hostmask that the user has entered, if the hostmask and ip matches, the user would be redirected to a new page where it tells you are banned from the website. Please help :(

Posted: Tue Mar 11, 2003 5:45 pm
by volka

Code: Select all

<?php
$banned = file('/path/to/bannedIP.txt'); // read banned ips to an array (one line->one element)
$banned = array_flip($banned); // is_set() is faster than in_array()
if (is_set($banned[$_SERVER['REMOTE_ADDR']])
	die("you're banned");

$links = file('/path/to/links.txt'); // read all links to an array (one line->one element, numerical indices)
srand((double)microtime()*1000000); // initialize rand-generator
$idx = rand(0, count($links)-1); // a random index "within" $links
?>
<img src="http://server.url/<?php echo $links[$idx]; ?>" />
(script tested not even by compiler)
hostmasks are a bit more complex
and I'm too tired for that right now ;)

Thanks

Posted: Wed Mar 12, 2003 1:41 am
by Raahul
no man thats enough :)

THANKS :D

Posted: Wed Mar 12, 2003 2:57 am
by volka
oops, I forgot something. The elements of $banned will have a trailing CR/LF.

Code: Select all

$banned = file('/path/to/bannedIP.txt'); // read banned ips to an array (one line->one element)
$banned = array_map('trim', $banned); // get rid of whitespaces
$banned = array_flip($banned); // is_set() is faster than in_array()

I used this

Posted: Wed Mar 12, 2003 10:37 am
by Raahul
I used this code

Code: Select all

<?php
$banned_person = file('banned.txt');

foreach($banned_person as $banned) {
    $ip = $_SERVER['REMOTE_ADDR'];
    if($ip == $banned){
        echo "The ip is banned, sorry, you can't view this page!";
        exit();
    }
}
?>

Posted: Wed Mar 12, 2003 3:11 pm
by volka
you still need to get rid of the CR/LF.

Code: Select all

$banned_person = file('banned.txt');
$banned = array_map('trim', $banned); // applies trim() to all elements.
foreach($banned_person as $banned) {
because "127.0.0.1" is not equal to "127.0.0.1\n" ;)
http://www.php.net/manual/en/function.file.php wrote:Note: Each line in the resulting array will include the line ending, so you still need to use trim() if you do not want the line ending present.