Random Banner/ Ip/Hostmask ban

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Raahul
Forum Newbie
Posts: 6
Joined: Sat Mar 08, 2003 5:40 am

Random Banner/ Ip/Hostmask ban

Post 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 :(
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 ;)
Raahul
Forum Newbie
Posts: 6
Joined: Sat Mar 08, 2003 5:40 am

Thanks

Post by Raahul »

no man thats enough :)

THANKS :D
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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()
Raahul
Forum Newbie
Posts: 6
Joined: Sat Mar 08, 2003 5:40 am

I used this

Post 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();
    }
}
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
Post Reply