Monday, February 2, 2015

anil

Need to 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…

The web has not only human visitors!

If your WordPress or PHP site has a few visitors within an hour a web server should be able to serve ALL pages to your visitors without any problems. So far so good, but what if your site get accessed by some bot? The most bad scenario is that these “unnatural” access can slow/take down your site and also all other sites hosted on the same server!

WordPress Super Cache, a required WP plugin

Even if your blog doesn’t have a lot of blog post or comments, you should install the WordPress Super Cacheplugin. This plugin works on most servers and can rescue your blog’s life! WordPress needs a lot of database queries to show a single page to your visitors. Each database connection needs some memory and will use some CPU. Using this cache plugin a “normal” viewed page doesn’t use the database anymore and your server can handle much more traffic.

Cache functions for custom PHP websites

There are many ways to cache your website, there are several cache modules available or it’s possible to create a cache version of each page using some PHP code. Which the best is for your situation depends on the application and the type of hosting you’re using.

The eAccelerator project

If you’re able to configure your web server (you need root access) you should try the eAccelerator project. It works as kind of PHP extension and is able to create a cache version of your PHP scripts. I installed and updated eAccelerator on two web servers now and I like it how it works. Before you start you should check the requirements and maybe you like to read my notes about the eAccelerator installation.

Custom PHP file caching (tutorial)

If you’re looking for a way to cache single pages from your website you should try this tutorial. The simple code snippet is able to “download” the HTML code from the selected page and stores the HTML code as a static page. The following code will check, read/write and output the cache version (check the comments inside the code).
The code above is pretty simple but not dynamic. To get this example working, you need to create a list for the cache files and for the URL’s. In the next example we use some mod_rewrite rules to match our file structure.
In our case we use a part from the URL and pass this string as a variable using the query-string. Here are some example URLs:
http://domain.com/page/great-post-about-scripts
http://domain.com/page/php-upload-tutorial
http://domain.com/page/jquery-plugin-review
Inside our .htaccess file we use this rule:
RewriteEngine on
RewriteRule ^page\/([a-z\-]*)$ /page.php?pageurl=$1 [L]
The rewrite engine will pass anything after “page/” to the page.php file as a query-string. Inside the file page.php we need some code to automate our request and generate the cache version.

Place this IF/ELSE statement below the variables $cache_file and $url. The code we’ve used inside the tutorial is just an example, you need to change the code to get it working in your situation.


About Author -

Hi, I am Anil.

Welcome to my eponymous blog! I am passionate about web programming. Here you will find a huge information on web development, web design, PHP, Python, Digital Marketing and Latest technology.

Subscribe to this Blog via Email :

Note: Only a member of this blog may post a comment.