Archive

Posts Tagged ‘php snippets’

Function to format bytes in human readable format

April 8th, 2010

I was digging through my old codes to get this, still could not find. Hence when I wrote it, just made a note here such that later it wont be difficult to find atleast for me.
Read more…

Code Snippets, PHP, Tips ,

Formatted XML, the php dom way – Best for debugging

April 3rd, 2010

Recently in a discussion on linkedin PHP webservice – Logging Requests/Responses, Roy de Kleijn had asked “how can I print the XML in XML structure, in stead of a single line of text”, which triggered me a thought. This may be trivial for veterans, and still a new information for newbies.

Read more…

Code Snippets ,

Paged Navigation – My own way

October 24th, 2009

Recently while working for a wordpress plugin, though there are a lot of solutions for this, I could not find the exact thing which I needed, and thought about writing one of my own. Just writing it and using in the current project would be a waste, so thought I would slap it on here. It would help me in the long run.. Read more…

Code Snippets , ,

Importance of event logging in server side scripting

October 12th, 2009

Now a days script driven web applications are getting more and more complicated with background operations and triggered events. Debugging or event tracking is tough once the application is moved into production. Fresh and aspiring programmers are always too cautious to wade into deeper waters, and always go with line by line testing. Almost always in the course of debugging or code optimizations I see a lot of them using file_put_contents or echo to check variables at a particular point of execution.

I always gave the pressure to use a good logging system from the start itself, and to add level based message logging with debug_backtrace wherever needed. The most recent class abstraction for php programmers which is being used in our custom framework is attached to the downloads here. The file logging is being done after serializing, compressing and base64_encoding to keep logs in single lines, and to make sure they dont take up too much of the space.
Read more…

General Articles, PHP, Tips , , ,

Invoke shell from MySQL trigger

June 20th, 2009

No I am not insane, and after a long days search over the wide Internet, even google admitted defeat, there seemed to be no way to do this. Finally I had already dropped the idea or even let off the thing altogether. But recently for another project I needed to check in for an entirely different requirement and stumbled on the fact. And yes I checked it, voila the shell invoke from mysql trigger is possible.
Read more…

Code Snippets, PHP, mysql ,

function getIpBehindProxy

May 19th, 2009

We were worried, about all the comments on kerala online, being marked as spam by the akismet plugin. When on detailed examination, we found that the basic problem was that wordpress was logging only the immediate downsteam ip as the remote address, well ours was a bit confusing setup, but to handle the traffic we needed it that way.

A search for wordpress behind reverse proxy, landed me to the wordpress support page. In fact the 5th entry on that page is done by Gopka, who is the lead on this project from Saturn.

We started to correct the remote address by overriding the global variable making slight changes to the wp-config, such that we will not accidentally overwrite the changes while upgrading wordpress. Well the code


if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        
$list explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
        
$_SERVER['REMOTE_ADDR'] = $list[0];
  }


when added to the wp-config, it started to log the first IP, and that would be mostly private IPs if the request was from organisations where internet was shared through proxies or using NAT. The case was same at our office, so we had to find the first public ip from the list of IPs and the code for function getIpBehindProxy was the out come.
Read more…

Code Snippets , ,

Wordpress object caching in file system

May 12th, 2009

During the past couple of months, since we are maintaining a heavily loaded wordpress implementation, Kerala News Portal for Asianet, distributed across two servers, and having a daily visit of 50K, I was on the hunt for better caching and optimizing techniques. On the move, I happened to read the article by Jeff Starr, written some 15 months ago, and all the associated comments. It was sad to see that the file based cache was removed from wordpress, due to several reasons though.
After reading through this, I saw the official Function Reference for WP Cache, and the eAccelerator for WordPress by NeoSmart Technologies. Reading through the code written by Computer Guru of NeoSmart Technologies, tickled me and I just modified some parts, by pulling out the eaccelerator specific functions, and switching to a file system based store. I am not adding the code as pretty php code. Drop the file into wp-content, create a cache folder in wp-content and chmod folder to 755.
File based object caching for Wordpress (314)

Wordpress Plugins , ,

Web Spider using php cli

May 7th, 2009

Purely on academic basis, I had once helped to cook up a web spider, which is used to build site maps. The spider was written in php and uses a couple of reg-exp matches, and finally writes the full sitemap from the start url. The system is assembled using two classes, WebPage and WebSpider. Then to make it similar to linux utilities in the command line environment, some functions were scooped in.
Read more…

Code Snippets ,

JavaScript Aggregate; Wordpress plugin

April 30th, 2009

After reading about Website Performance Tweaks, and a lot of other blogs and slides, I thought about how to cook up a javascript aggregate plugin for wordpress. The out come is wp-jsmin. Though this is in its infancy, it is being used in this blog, to combine all linked javascript to a single link, thereby reducing server requests. This code is still in the testing stage, and may break if the used scripts do not pass lint checks. I am planning to shift all the options to a options page in the wordpress admin page.

Those who are daring enough could download and try. Please put a comment here if you are using this on your wordpress. And for others who would like professional help, I would be most pleased to extend my services through RentACoder. Or for a whole dedicated wordpress team to design, build and maintain sites like Kerala News, Asianet Cable Vision: Corporate Site or Campagin Portal for Shashi Tharoor, send an inquiry to Saturn.

WP-JsMin (168)

Code Snippets, Wordpress Plugins ,

Moving contents from one folder to another

October 25th, 2006

We always try to store images in a web path where heavy image linking is needed, and mostly the images will be categorised in some sort of directory tree.. to limit the no of files in a folder.

Once editing, for example, re assinging a set of students from one scientist to another, was needed, and the hell, we had to face.. finally the following piece of code was written, for a future reuse.

<?php
function mkdir_r($dirName$rights=0777){
        
$dirs explode('/'$dirName);
        
$dir='';
        foreach (
$dirs as $part) {
        
$dir.=$part.'/';
        if (!
is_dir($dir) && strlen($dir)>0)
                
mkdir($dir$rights);
        }
}
 
function 
xMove($source$target$drop true){
        if(!
is_dir($source)) return false;
        if(!
is_dir($target)) mkdir_r($target);
 
        
$d dir($source);
        while (
false !== ($entry $d->read())) {
                if(!
is_dir($source"/$entry")){
                        
copy($source "/$entry"$target "/$entry");
                        
unlink($source "/$entry");
                }
        }
        
$d->close();
        if(
$drop)
                
rmdir($source);
}
 
?>

If any body is wondering (just a thought) how I have managed the colour highlighting, then better take a look at Phpizer Plugin

Code Snippets, PHP ,