PHP Code

Navigate back to home page
z/OS related collateral
Windows related collateral
SQL/mySQL related collateral
Link to code that can be used on websites.
Link to ASL Program Documentation
All our latest news
Contacting Abbydale Systems
Obtain product support/suggestions
https://www.abbydalesystems.com/Terms and con
General disclaimer
All about Abbydale Systems
How we got our company name
Our privacy policy
Our development strategy
Visit our guestbook

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.


This material has been used successfully by Abbydale Systems LLC. and to the best of our knowledge this material and any system(s) of which it is a part are operational as of the service level or date stated in the body of this material (if so stated). However, NO warranty is given or implied as to the accuracy of this material or any related material or systems, and no responsibility is assumed for any effect or modification directly or indirectly caused by the use of this material.

It is the responsibility of any user of this material to evaluate its usefulness to the user's environment.

Abbydale Systems LLC. does not guarantee to keep this or any related material current, nor does it guarantee to provide any corrections or extensions described by any users of this material or any corrections or extensions made in the future by Abbydale Systems LLC. itself.


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

follow us on facebook
Follow us on Facebook




Subscribe to our YouTube Channel Subscribe to our YouTube channel


CBT Tape Home Page MVS and OS/390 Freeware Link to the CBT website


Copyright © Abbydale Systems LLC 2015-2026

Abbydale Systems LLC Lic. 802696149. All rights reserved.