Speed Up Your Website By Enabling Gzip

Enabling gzip is an incredibly easy way to speed up delivery of your website content. Just create a file in the root folder of your site named .htaccess (or edit your existing one) and copy the following lines into that file.

php_value output_buffering On
php_value output_handler ob_gzhandler
Want to check if it is working? Click here and use this Gzip test tool. If this doesn't work then your server may not allow gzip to be enabled. Some server administrators disable gzip compression to save on CPU load. If you still insist and the above lines didn't work for you try enabling the alternative, deflate. Place the following lines into an .htaccess file.
AddOutputFilterByType DEFLATE text/html text/css text/javascript
Now if that doesn't work and you really really want to enable compression, copy the following php code into the top of the PHP files you want to compress.
<?php if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start(); ?>
But if you reach this point you should really consider switching to a new server or just living without compression, heh.

Submit a Post and/or Cookie using CURL

Here is a class that will submit post variables or cookie variables using the CURL library.

The CookiePost Class

/**
 * CookiePost
 * Uses CURL to submit a post and/or cookie.
 * 
 * @author Kyle Robinson Young <kyle at kyletyoung.com>
 */
class CookiePost 
{
	var $useragent		= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)';
	var $referer		= 'http://www.google.com';
	var $timeout		= 30;
	var $maxredirs		= 4;
	var $status;
	var $post, $cookie	= array();
	
	/**
	 * SUBMIT
	 *
	 * @param str $url
	 * @return bool | str
	 */
	function submit($url=null)
	{
		if (empty($url)) return false;
		
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:'));
		curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout);
		curl_setopt($curl, CURLOPT_MAXREDIRS, $this->maxredirs);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
		
		// IF COOKIES
		if ($this->getVars('cookie', '; '))
		{
			curl_setopt($curl, CURLOPT_COOKIE, $this->getVars('cookie'));
		} // cookie
		
		// IF POST
		if ($this->getVars('post'))
		{
			curl_setopt($curl, CURLOPT_POST, true);
			curl_setopt($curl, CURLOPT_POSTFIELDS, $this->getVars('post'));
		} // post
		
		curl_setopt($curl, CURLOPT_USERAGENT, $this->useragent);
		curl_setopt($curl, CURLOPT_REFERER, $this->referer);
		
		$html = curl_exec($curl);
		$this->status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
		curl_close($curl);
		
		return $html;
	} // submit
	
	/**
	 * GET VARS
	 * Returns class vars from array to str
	 *
	 * @param str $name
	 * @return false | str
	 */
	function getVars($name=null, $ender='&')
	{
		if (empty($this->$name)) return false;
		if (!is_array($this->$name)) return $this->$name;
		$vars = '';
		foreach ($this->$name as $key => $val)
		{
			if (empty($key)) continue;
			$vars .= $key.'='.$val.$ender;
		} // foreach
		$this->$name = substr($vars, 0, -1);
		return $this->$name;
	} // getVars
	
} // CookiePost

Using The Code

$cookiePost = new CookiePost();
$cookiePost->cookie = array(
	'user'		=> 'kyle',
	'passwd'	=> '1234',
	'cookie'	=> '1'
);
$cookiePost->post = array(
	'postVar1'	=> 'testing'
);
echo $cookiePost->submit('http://www.example.com/test.php');

First create a CookiePost object and then specify any cookies and/or post vars we want to submit using an associative array. Finally just give the submit method a URL to submit to. It will return the HTML and set the status variable to the returned HTTP status code. Enjoy.

Quick clean URLs using mod_rewrite

When building a layout based website which URL looks better?

  1. http://www.example.com/?page=home.html
  2. http://www.example.com/home
Number 2 of course and why not do it when clean URLs are surprisingly simple to accomplish? Requirements: Apache, PHP and mod_rewrite enabled. Copy the following snippet into a file named .htaccess then put this file in the same folder as your default index.php file.
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
This little snippet checks to make sure you have mod_rewrite enabled, turns on the RewriteEngine, checks to make sure the requested url is not an existing folder or file, then sends the url requested to your index.php file. Here is an sample index.php file:
<html>
<body>
<div id="header"></div>
<div id="content"><?php
$page = empty($_REQUEST['url']) ? 'home.html' : $_REQUEST['url'].'.html';
if (!file_exists($page)) $page = 'error404.html';
include $page;
?></div>
<div id="footer"></div>
</body>
</html>
This script checks if the $_REQUEST var url is empty. If its empty then it sets the default page to home.html otherwise it appends .html to end of the requested url. If the page file does not exist it will try to load a file called error404.html. Simple right?

Code Snippet: Valid Email

Need to validate an email address? Here is a little regex I put into a php function to do just that:

function validEmail($email) {
    if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) return true;
    else return false;
} // validEmail