Enter your contact information.
Enter your project information.
Step 3Choose the PHP programmers you want to hire.
Compare Free PHP Quotes
Why should you cache your PHP website?
*Attention PHP Programmers* - Add A Free Listing! Get more clients.
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 Cache plugin. 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).
<?php // a function to receive an write some data into a file function get_and_write($url, $cache_file) { $string = file_get_contents($url); $f = fopen($cache_file, 'w'); fwrite ($f, $string, strlen($string)); fclose($f); return $string; } // a function that opens and and puts the data into a single var function read_content($path) { $f = fopen($path, 'r'); $buffer = '; while(!feof($f)) { $buffer .= fread($f, 2048); } fclose($f); return $buffer; } $cache_file = '/home/user/public_html/cache/cache.page.php'; $url = 'http://www.domain.com/page.php'; if (file_exists($cache_file)) { // is there a cache file? $timedif = (time() - filemtime($cache_file)); // how old is the file? if ($timedif < 3600*24) { // get a new file 24 hours $html = read_content($cache_file); // read the content from cache } else { // create a new cache file $html = get_and_write($url, $cache_file); } } else { // no file? create a cache file $html = get_and_write($url, $cache_file); } echo $html; exit; ?>
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.
if (!empty($_GET['pageurl']) && preg_match('/^[a-z-]*$/', $_GET['pageurl'])) { $cache_file = '/home/user/public_html/cache/cache-'.$_GET['pageurl'].'.php'; $url = 'http://www.domain.com/page.php?page='.$_GET['pageurl']; } else { header ("HTTP/1.1 301 Moved Permanently"); header("Location: ".$url); exit; }
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. Download the tutorial code for a quick start. If you have questions, comments or suggestions, please post them below, thanks!
Similar Posts:
- How-to show popular posts on your WordPress blog?
- Limit the number of downloads per client
- Upload in modal window and pass values with jQuery
Mon, 14 Feb 2011 14:08:24 GMT
Tags: cache, eAccelerator, PHP Scripts, review, tutorial, Wordpress,
*Attention PHP Programmers* - Add A Free Listing! Get more clients.
cache
eAccelerator
PHP Scripts
How-to show popular posts on your WordPress blog?
Yesterday I replaced my list of current PHP articles on this blog with a list of popular articles. I’m sure it’s possible in WordPress to get those list if you count the number of comments, but actually I was looking for solution based on real
WordPress categories, optimization tips and custom pages
It’s not a SEO secret, inner links to / from strong category pages will help your site to rank higher in Google. Sad enough that WordPress doesn’t provide a lot of functionality for these important sections on your blog site. This is the infor
We are a ISO 9001:2008 certified company and STPI registered. Our Services are: Web designing, Web Development, Content & Article Writing, SEO, SEM, SMM
Kolkata, West Bengal IN
website creator chennai, web development firm chennai, product development in chennai, software developer in chennai, website designer in chennai, portal developing firm chennai SEO and SEM solutions
Chennai, Tamil Nadu IN
Web site design and development. International clients are welcome.
Quilcene, Washington US
Get a website for $400.00. Please see our Facebook page for more details. http://www.facebook.com/Computer.Wonders
Dunmore, Pennsylvania US
NodeJS cURL Tutorial
NodeJS is a powerful evented Javascript platform running on top of Google’s V8 javascript e
This Week in Web – PHP Namespaces, NodeTuts, Kohana
Interview with Matthew Weier O’Phinney
Kevin Schroeder, tech evangelist at Zend, r
A Possible Future for CSS Selectors
Woolly, the CSS sheep. CSS has some great new tools like pseudo classes and pseudo elements for sel
Million Short: A Search Engine for the Very Long Tail
Want to see the web's long tail? Just cut out the top 1 million websites and search through what's l
Share Your Web Browser With Chrome’s New User Switching
The latest beta version of Google's Chrome web browser now syncs your browsing data to your Google a
Article Tags
PHP Programmers Articles
Live Help Chat Software
Web Design Quote
Web Design