Tuesday, February 15, 2011

Fast Websites using Cache, a PHP Tutorial

[Image]Why should you cache your PHP website?

Most web servers are able to handle “normal” traffic and there are plenty of websites which doesn’t have so much traffic. So maybe you ask yourself: Why should you cache your PHP powered website? The apache web server is able to serve many, many files at the same time, but all these files need to be static. A PHP script is parsed by the web server and next the generated HTML data is send to the client (web browser). While this happens the server need to use much more memory than by sending a file to a web client. Imagine what happens if you run/parse a page which is build with WordPress…

Monday, February 14, 2011

.htaccess files

In several web servers (most commonly Apache), .htaccess (hypertext access) is the default name of a directory-level configuration file that allows for decentralized management of web server configuration. The .htaccess file is placed inside the web tree, and is able to override a subset of the server's global configuration; the extent of this subset is defined by the web server administrator. The original purpose of .htaccess was to allow per-directory access control (e.g. requiring a password to access the content), hence the name. Nowadays .htaccess can override many other configuration settings, mostly related to content control, e.g. content type and character set, CGI handlers, etc.

Friday, February 11, 2011

PHP Preg_Replace Example

http://www.daniweb.com/forums/customavatars/avatar168201_4.gifThe following is a simple example of the PHP function preg_replace(). It's also a short tutorial on regex (regular expressions) using Perl since this is the regex patterns the preg_replace function utilizes.

Basic Regular Expression Syntax

http://coder.awas.vn/upload/642f9def82f94d18958d16963547405b.jpgIn computing, a regular expression, also referred to as regex or regexp, provides a concise and flexible means for matching strings of text, such as particular characters, words, or patterns of characters. A regular expression is written in a formal language that can be interpreted by a regular expression processor, a program that either serves as a parser generator or examines text and identifies parts that match the provided specification.

php dir list and sort by date

Even though the php dir function works straight from the box, it won't be very much help when you'll start thinking of sorting a file list by modification date of the file. here is a piece of code and a few functions that do the trick
function comparar($a, $b)
{
return strnatcasecmp($b["1"], $a["1"]); //reversed to order from newest to oldest, change to ($a["1"], $b["1"]) to do oldest to newest
}
$default_dir = "."; //current directory
$dirlist = array(); //make new array
$dir = dir($default_dir);
while($file = $dir->read()) //read directory
{
if(0 != strpos($file, '.demo')) //if filename has .demo in it continue
{
$lastmodified = filemtime($file); //find filetime in milliseconds
$dirlist[] = array($file, $lastmodified); //put these into array
}
}
$dir->close();
usort($dirlist, "comparar"); //send array to usort
$arraynum = sizeof($dirlist); //how many entities the array contains
$i = 0;
while($i < $arraynum) { $curList = $dirlist[$i]; //set current array to access $curFile = $curList[0]; //access that array's first array $curTime = $curList[1]; //access that array's second array $curDate = date("m/d/y", $curTime); //convert time into something readable echo " $curFile $curDate";
$i = $i + 1; //next $i
}