Archive

Posts Tagged ‘php snippets’

Compressing php source files for embedded applications

September 6th, 2011 Comments off

Compressing php code is not been a tough task, and this is not for the purpose of obfuscating or encoding to make closed source distribution. But to create compact files which can be distributed by pasting into inline text, or into email without the need for attaching. The size will be drastically reduced. Actually at Saturn we did this to help us achieve to load a whole application on to a very space constrained flash disk. We were the least bothered about the process cycles taken to evaluate the php files since they will happen only once in a blue moon when the embedded system restarts. I am pretty sure that for closing source, there are other solutions.
Read more…

Categories: Code Snippets, PHP Tags:

WordPress – preview and modify a theme till we are satisfied

January 11th, 2011 2 comments

Since we at Saturn provide wordpress as a CMS, and have more than a handful of experienced WordPress customizers, and themeres we have handled several challeges related to WordPress. The latest one being that the client was offered a facelift, and they wanted to preview the progress of integration. I searched the plugin repositories and blogs, found a lot of solutions, which include theme switchers, theme preview, and even suggestions to have a local copy. Well with our client, we could not think of a local copy, since the database dump itself was about 320GB and the images and videows was a whopping 1200 GB. Well after reading through the source of get_option, a wave struck me.



function new_theme_preview(){
    if(isset(
$_COOKIE['ShowNewTheme'])){
        return 
'myNewTheme';
    }
    return 
false;
}
 
add_filter('pre_option_current_theme''new_theme_preview'10);
 


I added the above to the end of an active plugin, then created two files newtheme.php and oldtheme.php, containing code to set and remove the cookie. The files were put into the top level folder. Once I am done with the theming, I could remove all these manually.

// newtheme.php
setcookie 'ShowNewTheme''1');
header("Location: ./");
 
//oldtheme.php
setcookie 'ShowNewTheme'false);
header("Location: ./");
 

php-cron – schedule jobs to run by the second and more

January 6th, 2011 No comments

Yet another challenge, mostly like all other developers, ours at Saturn too believed that scheduled jobs could be run to the lowest frequency at every minute. This stands true if the job is handled only by *nix cron or by windows scheduler. For even further control one should write a Sleep-And-Run. I used to do this in shell script for most of my requirements. Just the other day some one challenged me to get done with one similar in php.

The outcome is a crude one, ServiceManager, which runs based on a configuration file and its run frequency. Best not to change the frequency or your configuration would never support anything less than the frequency as well as will only support multiples of the frequency. The configuration file is the famous ini file format with item names in square brackets, with configuration on two lines. The sample attached ServiceManager.ini should give an idea.
Read more…

Categories: PHP Tags: , ,

php smtp email direct to mail box delivery

January 5th, 2011 No comments

For sending status mails, with varying from addresses, for several of our projects at Saturn, we were using the phpmailer which uses our smtp server with authentication. Well our smtp host had a limitation of 250 emails per day. When our requirements grew out of this limit, mails started to pile up. Sure I could install exim4 or sendmail on my boxes, and that is what I did for immediate resolution. But here comes a new requirement, that the mails sent should be marked as such, and those which failed should be marked with the exact response of the receiving end mailserver.

At this point I thought about an SMTP direct to mail box Delivery system. My favorite language being PHP, and primary library being Google, I tried all possible ways, according to me, and they were not the right ones as I came to know later. All these did not get me in the right direction. And finally thought about writing one. Here too, being lazy, wanted to have the code from some ones work to ignite me. Okay I found the phpbb’s smtp.php referred on the net, and the function smtpmail from the same was the right choice.
Read more…

A Mathematical Captcha – Reinventing the Wheel

April 19th, 2010 2 comments

For a long time I have been using the image captcha with random characters, and got fed up. I had seen numeric computational captcha or Mathematical captcha where one has to calculate the answer of an equation with simple arithmetic and supply the result for verification. Last night I was pondering over this while watching a movie on the TV. Well the out come is obvious.
Read more…

Categories: Code Snippets, PHP, Tips Tags: ,

Function to format bytes in human readable format

April 8th, 2010 No comments

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…

Categories: Code Snippets, PHP, Tips Tags: ,

Formatted XML, the php dom way – Best for debugging

April 3rd, 2010 No comments

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…

Categories: Code Snippets Tags: ,

php MsSQL Backup

January 30th, 2010 6 comments

A php script to dump database as sql from MS SQL

We started porting (provide dual support) one of our application from MySQL to MSSQL, sorry, actually we were trying to run the same code base on either MSSQL or MySQL as the client wishes. And those who knows both, will agree to the level of toughness and agony when considers the fact that all of us are on MySQL and are new to MSSQL. In this context, we wanted to provide backup and restore to the application administrator through the system itself, and was not contented with the .bak files provided by MSSQL backup.
Read more…

Paged Navigation – My own way

October 24th, 2009 No comments

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…

Importance of event logging in server side scripting

October 12th, 2009 No comments

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…