You Are Here Home > March 2009

March 2009

PML – A Python template engine

I finally decided to release PML as an open source software. I developed this template engine for a project that unfortunately never happened using Python.

Here is a sample application using PML and Yahoo! Search API:
http://web-search.code-head.com/

You can download it here:

[D O W N L O A D]

The web search example is also included in the download.

PML was written to be fast, simple and compact. Here are some of it’s features:
1 – Template filters
2 – Output filters
3 – Template variable filters
4 – Template cache – default
5 – Bytecode cache – default
6 – Complete output buffer cache
7 – Garbage collection
8 – Output compression – GZIP
9 – A powerful, quick compiler
10 – Ability to add helpers easily
11 – Ability to add custom compiler tags
12 – Auto escaping your variables, even lists, dicts, and tuples
13 – Compile templates once until you edit them

And more.

The web search application above is included in your download and it demonstrates almost all the aspects of this powerful template engine.

PML – A Python template engine
Comments (0)   Filed under: Programming, Python, Web Design, Web Development   Posted by: Codehead

A PHP Competition 2

Say that you are generating an XML sitemap for your site and there are 5,000 URLs.

Here is the code:

<?php
 
	$buffer =
'<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
';
 
	$results = fetch_5000_urls_order_by_importance();
	$count = 0;
	while ($row = $results->fetch_next_url()) {
		$priority = ##########################################;
		$url = $row['url'];
		$date = date('Y-m-d', $row['date']);
		$buffer .= "
			<url>
				<loc>$url</loc>
				<lastmod>$date</lastmod>
				<changefreq>weekly</changefreq>
				<priority>$priority</priority>
			</url>
			";
	}
 
	$buffer .= '
	</urlset>
	';
 
	echo $buffer;
 
?>

What you want to do is to give each URL a priority; URLs between 0 to 1000 will have a priority of 0.9
1000 to 2000 => 0.8
2000 to 3000 => 0.7
3000 to 5000 => 0.6
4000 to 5000 => 0.5

Replace ########################################## with the code that calculates the priorities, and remember not to use ; in it ;)

A PHP Competition 2
Comments (9)   Filed under: Fun, PHP, Programming   Posted by: Codehead

How to clean the screen with PHP in Linux (cls, Linux, PHP)

Here is the trick, in your script use:

system("clear");

To make it work on both Windows and Linux:

if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN')
   system("cls");
else
   system("clear");

I hope this helps someone.

How to clean the screen with PHP in Linux (cls, Linux, PHP)
Comments (2)   Filed under: Operating Systems, PHP, Programming   Posted by: Codehead

A PHP script for dealing with DoS attacks

Here is a simple script that will show you what IP addresses are making how many requests to your server.

<?php
 
## Functions ##
 
function getIP($line) {
	ereg("[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}",$line,$regMatch);
	$ip = $regMatch[0];
	if($ip) return $ip; else return "false";
}
 
function processString($string, $size = 18) {
	$string = "[ ".$string;
	$length = strlen($string);
	$toAdd = $size - $length;
 
	for($x = 0; $x < $toAdd; $x++) {
		$string = $string." ";
	}
	$string = $string."]";
	return $string;
}
 
## Code ##
 
while (true) {
	$cmd = "netstat -n | awk '{ print $5 }'";
	exec($cmd, $netstatArray);
	$ipArray = array();
 
	foreach($netstatArray as $line) {
		$ip = getIP($line);
		if($ip != "false" && ip != "127.0.0.1") {
			if(array_key_exists($ip, $ipArray))
						 {
									$ipArray[$ip]+=1;
						 }
						 else // if not, count=1
						 {
									$ipArray[$ip] = 1;
						 }
		}
	}
 
	asort($ipArray);
 
	system("clear");
	foreach($ipArray as $ip => $count) {
		if ($count < 15)
			continue;
		echo processString($ip);
		echo "\t" .processString(gethostbyaddr($ip), 55);
		echo "\tTimes Accessed: " .$count ."\n";
	}
 
	echo str_repeat("-", 50) ."\n";
	exec("top -n 1", $top_str);
	preg_match("#load average:(.+)#i", $top_str[0], $match);
	echo "Load Average: " .$match[1] ."\n";
	echo str_repeat("-", 50) ."\n";
	echo 'Showing $count >= 15: (Escape with ctrl+c)' ."\n";
 
	sleep(10);
}
 
?>

After identifying the IP addresses that are sending many requests at once to crash your server, you can ban them using a firewall software. I personally recommend APF: http://rfxnetworks.com/apf.php

You can do:
apf -d THEIPADDRESS SOMECOMMENTLIKEPOSSIBLEDOS

This script was originally written by a former employee of Acenet Inc and was modified by me. Acenet Inc is a great web hosting company with great support and fantastic staff members who will help you 24×7.

Here is some info about Denial of Service attacks (DoS attacks).
http://en.wikipedia.org/wiki/Denial-of-service_attack

I hope this helps someone.

A PHP script for dealing with DoS attacks
Comments (4)   Filed under: PHP, Security, Server   Posted by: Codehead