Archive for the 'PHP [Hypertext Preprocessor]' Category

Transparant debugging, tracing and performance monitoring in PHP

Tuesday, April 25th, 2006

Since I am always interested in the performance of the applications I write I always try to trace average time requests take wile developing. When applications are in production however; I do not have time to monitor them and assume reasonable performance. The same goes for debugging; I hope to try out all posible requests and check the responses. However, sometimes I just need some statistics of applications already running on my server; or even from applications I never even wrote; such as Roundcube, or PHPMyAdmin.

I have implemented a simple script which allows me to do all these sort of tracing. So, download it here, and add this to a .htaccess

php_value auto_prepend_file '/location/of/file/php_access_logging.inc.php'

Don’t forget to create the table as well:

CREATE TABLE php_logging (
  `date` timestamp NOT NULL default CURRENT_TIMESTAMP,
  ip varchar(15) NOT NULL default '',
  host tinytext ,
  browser tinytext NOT NULL,
  referer tinytext,
  site tinytext,
  url tinytext,
  request text,
  user_unique varchar(32) NOT NULL default '',
  duration int(10) NOT NULL default '0',
  PRIMARY KEY  (`date`,ip, url),
  KEY user_unique (user_unique)
)

I will post some usefull queries as well:

browser_usage
SELECT COUNT(*) as c ,browser FROM `php_logging` GROUP BY browser ORDER BY c DESC
referers
SELECT COUNT(*) as count, referer FROM `php_logging` GROUP BY referer ORDER BY count DESC
unique_users
SELECT COUNT(*) AS count, host, user_unique FROM `php_logging` GROUP BY user_unique ORDER BY count DESC
slowest_urls
SELECT COUNT(*), site, url, duration FROM `php_logging` GROUP BY url ORDER BY duration DESC
average_duration_per_site
SELECT COUNT(*), site, AVG(duration) FROM `php_logging` GROUP BY site

Hope you enjoy it!

Using PHP to serve files

Wednesday, March 15th, 2006

Sometimes you need PHP to passthru files instead of using a webserver like you should. Sometimes you need to check permissions; or do other stuff which requires you to make PHP behave like a webserver. So, what is the fastest way to passthru a simple file using no logic? I have been working of several ways of benchmarking this. First; I used Apache Bench to run a PHP file a few hundred times; but the results were too unstable. Sometimes I got 2 requests per second, sometimes I got 30 requests per second using the same settings. So I started looking for a different way of benchmarking. I found a way which was pretty stable. However; the results weren’t really interesting either. I ran all commands 100 times for different file sizes; the only noticeable change was readfile becoming slower as the file size increased (from 2.3 to 3.2 for a 6 Mb file). All other functions (fgets, file_get_contents, fpassthru, fread, readfile) remained constantly at 2.3 seconds. Conclusion is it doesn’t really matter what function you use; the speed difference is negligible.

simpleTemplate - a simple templateparser in PHP

Friday, January 6th, 2006

In a project of mine I wanted to use a templateparser to easily output some HTML. I started with Smarty; but found it too heavy weighted; with it’s endless list of classes and files I didn’t want. So I continued with TinyButStrong which proclaims to be simple with only one class. Having tried some time I didn’t like it either. It’s code is unreadable to me; and so are the templates. Even worse; it uses blocks instead of arrays; so I needed to define all my arrays as blocks before the templateclass could use it. So I started thinking about creating something myself. As Google can tell you, other people have thought the same thing. I hope you can spot what makes mine better by looking at the code, or just start using it. Anyways, like so many people I think my code might be useful for others.

simpleTemplate supports if-statements, variable getting and setting, foreach-loops, blocks and a simple yet effective caching mechanism. I do have a list of features I am going to add so keep an eye on sjon.blog for any updates to the code. Here it is:

Comments welcome!

MVC - nothing new there is it?

Thursday, January 5th, 2006

Reading MVC and web apps: oil and water after discussing about MVC today I think the article is pretty much right about MVC. At the office today we pretty much concluded that MVC was nothing more than a strict separation of your database, templateparser and actual engine just ‘doing’ the rest. This strict separation prevents spaghetti-code and unstructured calls between the various aspects of these elements of MVC. However, this separation should be common among web developers and there should be no need to call it MVC. It’s the same as “Don’t use registered_globals in your application”. This undefined fuzz about separation I like the comparison between where we are now, and where we were two years ago, it makes you wonder what the web will be like in 2007 :)

PS, haven’t read about CRUD yet? You should!

Protecting your property

Friday, December 30th, 2005

Zend almost convinced me that their Safeguard Suite really secured your PHP scripts. Ofcourse I know that scripts need to be interpreted and executed and are therefore never 100% safe; but I expected some form of obfuscation and rewriting to prevent this. This doesn’t seem to be the case though. I have had a look at PHP Obfuscator/Obscurer; but a good illustration of the quality of that script can be illustrated with following if-statement (line 406):

if($FunctieNaam == 'doLoad') $FunctieNaam = 'doLoad';

Also, obfuscating React took me almost an hour; which isn’t that strange when you have had a look at the sourcecode. Maybe a better example of how to obfuscate properly will be posted on this blog.

RoundCube Webmail

Monday, December 12th, 2005

Even though RoundCube is officially still in alpha phase it is a very usable webmail-client; replacing my previous SquirrelMail client, which is everything RoundCube is not. RoundCube uses AJAX combined with the Ilohamail IMAP classes, while Squirrelmail uses it’s own backend (which is faster) combined with a frontend based purely on HTML tables. Although Roundcube suffers some performance issues, it’s clean PHP code and templating system make this a very nice webmailclient with lots of potential.