Tag: php
[PHP]Empty an array
by Chuck Kozler on Dec.20, 2009, under Computers, Home, Programming, Web Dev
This simple function will empty an array for you. I had to use this for some stuff I was writing for parsing the results of a MySQL query.
I am surprised that there isnt a built in PHP function for this. If there is, let me know…please!
<?php function emptyArray( $array ) { $size = sizeof( $array ); for( $i = 0; $i < $size; $i++ ) { array_pop( $array ); } } ?>
I would use this so that I could use the same array over and over again in a class and have it be dumped every execution of the function it was being utilized in.
Sphere: Related Content[PHP]Deny by Host/Hostname
by Chuck Kozler on Mar.16, 2009, under Computers, Networks/Servers, Programming
Well, it seems my Wordpress blog here is getting slammed with Russian spambots lately. My hoster has not gotten back to me yet if they can block a host for me (Henrik!
), so, I decided to write this up.
The comments in the code explain it pretty well
<?php //Get remote host $_get_addr = $_SERVER['REMOTE_ADDR']; //Get the host from the address $_get_host = gethostbyaddr( $_get_addr ); //Ban list array. The text before => is a key name and //after the => is the host you want to ban. This allows //for aliasing your problematic hostnames $_config_hostbanlist = array( russia1 => "nn-88-237.nm-s.ru" ); /* Ban by full hostname. The list is populated by the hostbanlist array. Simply it just compares their host with an entry in the hostbanlist array */ function isHostBanned( $host, $_ban_list ) { //Iterate each entry in the banned list array foreach( $_ban_list as $entry ) { //If we get a match, return true (the host is banned) if( strtolower( $host ) == strtolower( $entry ) ) return true; } return false; } /* This will ban by IP. This list is populated dynamically by the hostbanlist variable. It then resolves IP's for each hostname in the list and then checks it. */ function isIPBanned( $_remote_addr, $_ban_list ) { foreach( $_ban_list as $entry ) { $entry_ip = gethostbyname( strtolower( $entry ) ); if( $_remote_addr == $entry_ip ) return true; } return false; } function doAlert( $alert ) { echo "<script language='JavaScript'>window.alert( \"$alert\" )</script>"; } /* We will be checking for true/false of isIPBanned first since the IP banned list is populated by the $_config_hostbanlist array and isIPBanned resolves the hostname to an IP. Then, if that fails, it also then checks the hostname and compares to the list again via hostname. Flow: isIPBanned? -> loop hostbanlist -> resolve IP for given loop iteration -> check IP resolved to REMOTE_ADDR -> Match? => Banned -> No Match? => Not in that list, assume they are either not in the list or their IP could not be resolved. Go to isHostBanned function to ensure they are not in the list isHostBanned -> loop hostbanlist -> check if their host is equal to an entry in the array -> Match? => Banned -> No match? => Not banned */ if( isIPBanned( $_get_addr, $_config_hostbanlist ) || isHostBanned( $_get_host, $_config_hostbanlist ) ) { doAlert( "You are banned" ); echo "<br><br><br><p align='center'>Your host [$_get_host] and/or IP [$_get_addr] has been banned. <br> Please contact <a href='mailto:webmaster@ckozler.net'>webmaster@ckozler.net</a> if you feel you have reached this page in error</p>" . "<br>"; exit; } ?>
When you use this, all you need to do is copy and paste this into a file and then in each file you want to ban the host from, you do:
require('hostblock.php');
Of course, the example above assumes you name the file hostblock.php
Sphere: Related Content