How to Block IP Addresses with PHP

Change google.com with the website you want the end user to be redirected to if they are on the deny IP list.

<?php
$deny = array("111.111.111", "222.222.222", "333.333.333");
if (in_array ($_SERVER['REMOTE_ADDR'], $deny)) {
   header("location: http://www.google.com/");
   exit();
} ?>

Using File::Basename to get file extensions

#!/usr/bin/perl

use strict;
use File::Basename;
use CGI qw/:standard/; #if running as a CGI application
#use CGI::Carp qw/fatalsToBrowser/; # uncomment for debugging only
print header,start_html; # if running as a CGI application

my $dir = '/home/username/public_html';
opendir(DIRHANDLE,$dir) or die "Can't open $dir: $!";
my @filenames = sort readdir(DIRHANDLE);
close(DIRHANDLE);
foreach my $file (@filenames) {
   my(undef, undef, $ftype) = fileparse($file,qr"..*");
   print "$ftype
n"; } print end_html; #if running as a CGI application

Script Execution Time

This basic snippet will calculate how many seconds it to a script to start and then end.

<?php
//Create a variable for start time
$time_start = microtime(true);

//Create a variable for end time
$time_end = microtime(true);

//Subtract the two times to get seconds
$time = $time_end - $time_start;

echo 'Script took '.$time.' seconds to exicute';
?>

iPhone Detector

Simply add to your .htaccess file. If iPhone is detected, the end user will be redirected to your mobile website or a different web site/page.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} .*iPhone.*
RewriteRule ^index.html$ http://mobile.mydomain.com [L]
RewriteRule  ^/$ http://www.mydomain.com/index.html [L]
</IfModule>

Force charset utf-8

If you can not change the configuration of Apache server, use this code to force decoding of page to utf-8.

AddDefaultCharset utf-8

Formatting today’s date

<cfoutput>#dateformat(now(),"dddd, mmmm dd, yyyy")#</cfoutput>

Show today’s date

<cfoutput>#Now()#</cfoutput>

Email Address Validation

function is_valid_email($email)
{
	if(preg_match("/[a-zA-Z0-9_-.+]+@[a-zA-Z0-9-]+.[a-zA-Z]+/", $email) > 0)
		return true;
	else
		return false;
}

IE only HTML element

<!--[if IE ]>
   <body class="ie">
<![endif]-->
<!--[if !IE]>->
   <body>
<!--<![endif]-->

First part is if the browser is IE and the second part is if not IE, which is stated using !IE.

Who’s Online

Add the following to your global.asa

<SCRIPT LANGUAGE=VBScript RUNAT=Server>

Sub Application_OnStart
   Application("WhoOn") = 0
End Sub

Sub Session_OnStart
   Application.Lock
   Application("WhoOn") = Application("WhoOn") + 1
   Application.Unlock
End Sub

Sub Session_OnEnd
   Application.Lock
   Application("WhoOn") = Application("WhoOn") - 1
   Application.Unlock
End Sub

</SCRIPT>

To display, add the following to your ASP webpage:

<%= Application("WhoOn") %>