There’s a major security update available now for Geeklog. If you’re hosting a Geeklog site then it’s time to upgrade. As they say themselves, This is the first major security issue with Geeklog that has been found in a long time..
Tag Archives: PHP
Caching Smarty Database Templates
The Smarty manual explains how to use Templates from other sources such as databases. It doesn’t however give an example of caching templates from these sources. I ran into a spot of trouble when I tried to be clever about checking the freshness of a db based template.
I didn’t want to interogate the db on every request just to check if a template is stale or not. Why have caching when you have to hit the db anyway? The db_get_timestamp() function has to be modified to check the freshness in some other way then.
I used a 0-byte file in a web-server writeable directory. Unfortunately it didn’t work. The is_cached() function would return false and I’d assign Smarty variables to prepare a new version of the web page, but when I called the display() function it displayed the last cached version!
It took me a while to figure out that both is_cached() and display() call the db_get_timestamp() function. As the file I used to flag a refresh was deleted on the first run I had to retain the timestamp somehow. That’s where the global $filetimestamp array comes in.
- is_cached() checks the freshness of the cache and returns false.
- I prepare the new Smarty variables.
- The page is displayed using the Smarty display() function
- Use
touch scratch/index.tplto update the template cache.
function db_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj)
{
global $filetimestamp;
if( is_file( 'scratch/' . $tpl_name ) == false )
{
if( $filetimestamp[ $tpl_name ] != '' )
{
$tpl_timestamp = $filetimestamp[ $tpl_name ];
}
else
{
$tpl_timestamp = mktime (0,0,0,date("m")-1,date("d"), date("Y"));
}
return true;
}
else
{
$tpl_timestamp = filemtime( 'scratch/' . $tpl_name );
$filetimestamp[ $tpl_name ] = $tpl_timestamp;
unlink( 'scratch/' . $tpl_name );
}
return true;
}
if( $smarty->is_cached( 'db:index.tpl' ) == false )
{
print "regenerating cache!<br>";
$smarty->assign( 'date', time() );
}
$smarty->display("db:index.tpl");
Bug in PHP 4.3.2?
I don’t think this is much of a bug really.
String indexes of arrays should always be quoted and when they appear as part of a larger string it’s faster to seperate the strings with the dot operator. See my comment on Keith’s post above for more.
Industrial Strength MVC
php|architect posted a sample article from their latest issue. It’s a tutorial introduction to MVC. You need to have a PDF viewer to read it though, fire up xpdf and you’ll be fine!
mod_security, PHP globals, etc
I thought PHP Traveller had finished up but no, he’s still around, and pluggin his mod_security Apache module. Looks good, but I’m now 3,000 miles from our Apache dev server at work so I don’t feel like experimenting with modules as much. 🙁
PHP – oo stuff
BDKR has a rant here about OO in PHP. I agree with some of what he says. There’s lots of bad code and design out there (I can’t promise to be perfect either!) but that permeates functional and procedural programming too.
I happen to like working with objects and structuring code in that way. It was a hell of a thing to get used to in college though, Before that I had done a lot of work coding in ASM on the C64. Actually having structures to work with was a real boon!
On a related topic, John mentioned a ObjectView, a PDF magazine about OO for developers. May take a look at it later.
PHP 4.3.2 is out
A new release of PHP is out today. It looks like a required update with a HUGE amount of other bug fixes!
Update! I upgraded 2 servers. One with Apache 1.3.27, php_accelerator went fine, but the other with mod_ssl, mod_gzip and php_accelerator refused to work. Every request resulted in a seg fault. I compiled PHP with mm support for mod_ssl (is that necessary?) but haven’t got time to track down where the crash occurs. (I tried disabling both mod_gzip and php_accelerator with no luck)
I’ll give it another go later.
PHP: Process Control Functions
I had quite forgotten that PHP can fork a process just like in C! This might be a useful thing to know for doing background tasks, but to be honest the extra complexity required will probably put me off doing it. That’s where this Thread class comes in handy..
<?php
include ("threadClass.php");class testThread extends Thread {
function testThread($name) {
$this->Thread($name); // calls the parent constructor and assign its name
}function run() {
while(true) {
sleep(1);
print time() ."-" . $this->getName() . " said ok...\n"; // every second we're going to print this line...
}
}
}
// Main program. Bring up two instances of the same class (testThread).
// They runs concurrently. It's a multi-thread app with a few lines of code!!!$test1 = new testThread ("Thread-1");
$test2 = new testThread ("Thread-2");
$test1->start();
$test2->start();print "This is the main process. Press [CTRL-CANC] to terminate.\n";
while(true) {sleep(1);}?>
Introduction to Design Patterns Using PHP
This is a good introduction which assumes you haven’t a notion what design patterns are. It also helpfully states,
… like everything else we use in software development / engineering. OO and Design Patterns have its place… [They’re] Not the Only Tool..
(via phppatterns)
More Benchmarks: FastCGI is faster than ISAPI/CGI on IIS
John conducted benchmarks the other day and found that IIS/WinXP can be faster than Apache /Linux. His initial results show a huge difference, but later he found the difference was only 10%.
What difference would running a PHP accelerator make to the test? That may very well swing the results back in favour. In my own tests, adding a PHP accelerator makes an undeniable difference in speed tests. The acccelerator makers would have you believe the speed up is 10 times, but it’s at least 5-6 times.
How stable is IIS these days? I’ve heard it’s much better than it was, but is it more stable than Apache (1.3 or 2?) on Windows?
Maybe we’re missing an important point here though. Many managers want to use Microsoft in their shops. If they have IIS, this is one very good way of running PHP in that environment without being penalised by cgi performance. Bravo John for doing the tests, I’m certain your site will popup in a Google search when some MSCE is desperately trying to figure out what technology to use! We’re get them to use PHP first, and then when they’re comfortable with this open source lark, move their Win32 servers to Linux or some other Free Unix! *grin*
