Bandwith saving and fast response times with etag

Recently i found a Post about Etags and thought this will be usefull for bandwith saving and faster response times in my scripts.

Just use this nice function:

function deliver_content($content)
{
	$etag = '"' . md5($content) . '"';
	if (!isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
		$_SERVER['HTTP_IF_NONE_MATCH'] = '';
	}
	header('Cache-Control: must-revalidate, proxy-revalidate');
	header('ETag: ' . $etag);
	if ($_SERVER['HTTP_IF_NONE_MATCH'] == $etag) {
		header('HTTP/1.0 304 Not Modified');
	} else {
		echo $content;
	}
}

So all your content of this site will be cached in the browser cache, until the etag differs. I just used an md5 of the content, but you could use also other strings.

Find links in a website without REGEX parsing

Everybody has needed it before... Website parsing for specific tags.
Naturally everbody try to solve this via REGEX. But how can we do that smarter? The solution is the domDocument class in PHP. This can be used to parse all Elements from HTML Code. In this Example i have used links.

Lets explain this via code:

 
// a site to search for links
$link = 'http://www.php.net';
// get the html code via file_get
$html = file_get_contents($link);
$ret = array();
// create a new dom object
$dom = new domDocument;
 
// load the HTML in the dom class, suppress parsing errors with @
@$dom->loadHTML($html);
 
// get all anchors from the html code
$anchor = $dom->getElementsByTagName('a');
 
/*** get the url from the links ***/
foreach ($anchor as $tag)
{
	// in $tag->getAttribute('href') is the url from the anchor
	// $tag->childNodes->item(0)->nodeValue is the value inside <a>value</a>
	echo $tag->childNodes->item(0)->nodeValue
	echo  ' -> '. $tag->getAttribute('href') . PHP_EOL;
}
 

PHP error handler

Nothing is more disturbing in an web application than beeing interupted by an error message. So what should i do?

Make you own error handler for your php application and include it at the top of every script.

Here is the code:

 'ERROR',
		E_WARNING =&gt; 'WARNING',
		E_PARSE =&gt; 'PARSING ERROR',
		E_NOTICE =&gt; 'NOTICE',
		E_CORE_ERROR =&gt; 'CORE ERROR',
		E_CORE_WARNING =&gt; 'CORE WARNING',
		E_COMPILE_ERROR =&gt; 'COMPILE ERROR',
		E_COMPILE_WARNING =&gt; 'COMPILE WARNING',
		E_USER_ERROR =&gt; 'USER ERROR',
		E_USER_WARNING =&gt; 'USER WARNING',
		E_USER_NOTICE =&gt; 'USER NOTICE',
		E_STRICT =&gt; 'STRICT NOTICE',
		E_RECOVERABLE_ERROR =&gt; 'RECOVERABLE ERROR'
		);
 
	// create error message
	if (array_key_exists($errno, $errorType)) {
		$err = $errorType[$errno];
	} else {
		$err = 'CAUGHT EXCEPTION';
	}
 
	$mail = "Error Report\n--------------\n\n";
	$mail .= 'called Script: ' . $_SERVER['PHP_SELF'] . "\n";
	if (isset($_SERVER['HTTP_REFERER'])) {
		$mail .= 'from Script: ';
		$mail .= $_SERVER['HTTP_REFERER'];
	}
 
	$mail .= "\n";
	$mail .= '$_REQUEST:' . "\n";
	$mail .= print_r($_REQUEST, true);
	$mail .= '_FILES:' . "\n";
	$mail .= print_r($_FILES, true);
	$mail .= "\n" . 'Error Type: ' . $err;
        $mail .= " \n----------\n[$errno] $errstr\n\nFatal error in line $errline of file $errfile" . "\n";
 
	$mail .= backtrace();
 
	if (DEBUG) {
		// output the error on the site
		echo nl2br($mail);
	} else {
		// sent the output to the syslog
		// syslog(LOG_INFO, $mail);
 
		// or via mail
		mail(ERROR_EMAIL, 'Error ' . $_SERVER['PHP_SELF'] . '-' . $errline . '', $mail);
	}
 
	switch ($errno) {
		case E_ERROR:
		case E_USER_ERROR:
		case E_COMPILE_ERROR:
		case E_CORE_ERROR:
		case E_RECOVERABLE_ERROR:
			// exit the script or display a message when the error is critical
			exit();
	}
}
 
// set the error handler to our new handler
set_error_handler('myErrorHandler');
 
?&gt;

With this little script you can choose what to do if an error occurs. Perhaps send an Mail to Bugtracker or to your work email. Or if you just in the development display it on the site.

If you have questions feel free to ask in the comments.