This is a collection of hints, tips, and code samples that may be useful to people who are thinking of developing in PHP.

The usual disclaimers apply to all code samples (including the MACROs').

  Blocking Unwanted IP Addresses  
  <?php

$blacklist = array("123.123.",
        "234.234.234.234",
        "111.111.222");

if(in_array($_SERVER['REMOTE_ADDR'],$blacklist)) {

     // this handles exact matches

     echo '<meta http-equiv="refresh" content="0; URL=http://localhost">' ;
     exit();

} else {

     // this handles wild card matches

      foreach($blacklist as $ip) {
       if(eregi($ip,$_SERVER['REMOTE_ADDR'])) {
    echo '<meta http-equiv="refresh" content="0; URL=http://localhost">' ;
         exit();
        }
      }
}
?>
This code should be placed at the top of a php webpage and it will stop the IP addresses that are defined from accessing the webpage.

In this example we would block all ip addresses that start 123.123, the specific IP address 234.234.234.234 and all the addresses that start 111.111.222.

In the spirit of being nice, if these addresses try and access the webpage they will be sent back to their own localhost (home address 127.0.0.1). You could direct them to any webpage (your own or someone else's)

This code could easily be adapted to only allow certain IP address to access your website if needed.
 
 
 
  Blocking Unwanted IP Addresses II  
  <?php

   $fd = fopen ("BlockIP.txt", "r");
   while (!feof ($fd))  {
      $buffer = fgets($fd, 4096);
      $lines[] = rtrim($buffer);
      if(preg_match("/".$buffer."/i",$_SERVER['REMOTE_ADDR'])) {
         echo '<meta http-equiv="refresh" content="0; URL=http://localhost">' ;
         exit();
      }
   fclose ($fd);

   //Cycle through all the IP Addresses in the file

   foreach($lines as $Spam)  {
      if(in_array($_SERVER['REMOTE_ADDR'],$lines))  {

      // this is for exact matches

      echo '<meta http-equiv="refresh" content="0; URL=http://localhost">' ;
      exit();
      }   }
 }
?>
This version of the code performs exactly the same function as the code above but the bad guys IP addresses are in an external file.

The file for IP addresses should be located in the same directory as that of the code executing the code. The file is a plain text file.

A sample text file of IP Addresses can be found here.

Change the file name (highlighted in red in the code sample) to match the name of your own file.
 
 
 
  Adding a Last Modified Date to a Webpage  
  <?php
echo '<p align="right">';
$file = __FILE__;

$last_modified = filemtime($file);

$lmdate = date("l, jS F, Y ", $last_modified);
$lm = date('l jS \of F Y ', $last_modified);
echo 'Last modified : ' . $lm . ' ';
echo '</P>'; ?>
This code will place the last modified date of a webpage on that webpage.

This this case it is placed on the right side of the screen.

Note: The code must be on the target page and can not be included from another file otherwise the last modified date will be of the included file.
 
 
 
  Automatic Year Increase for Copyright  
 

Copyright &copy; Abbydale Systems LLC 2015-<?php echo date("Y"); ?>


 
This simple piece of code will automatically increase the year on a copyright statement.  
 
 
 
 


If you need any support or assistance with any of the code on this site
or
if you would like to contact us, please click here

follow us on facebook
Follow us on Facebook

 

Number of unique visitors 297

Copyright © Abbydale Systems LLC 2015-2024

Abbydale Systems LLC Lic. 802696149. All rights reserved.

Last modified : Friday 31st of December 2021