vin.ingine.nl
a blog by Vincent van Ingen
6 Aug 2009
 

So you want to open a website with PHP with fopen() or file_get_contents()? But it's taken to long is not available? Well I got this problem with reading my Twitter account that I want to show on the right. Today Twitter has a lot of downtimes so if you check my website, my website taking a long time to load!

UPDATE: Twitter had a DDOS attack that causes the downtime. Well, it's good for testing the code ;)

The only thing you need to do is change the default_socket_timeout ini value like so:

 
// set timeout for 5 seconds
ini_set( 'default_socket_timeout', 5 );

I store all my community feeds into a cache file. So if there's a downtime, it will show the old one stored in the cache. And you don't need to reload the content per every page click. So it's nice to have it stored in cache for like 6 minutes.

 
function getContents( $url )
{
	// set the timeout for 3 seconds
	ini_set( 'default_socket_timeout', 3 );
 
	// store the cachefile into $file
	$file = md5( $url );
	$old = true;
 
	if( $f = fopen( "cache/$file", 'r' ) ) 
	{
		$stat = fstat( $f );
 
		// check if the cache file is NOT older then 360 seconds
		if( time() - 360 < $stat['mtime'] ) {
			$old = false;
		}
 
		fclose( $f );
	}
 
	// if NOT old, return the cache file
	if( !$old ) {
		return file_get_contents( "cache/$file" );
	}
 
	// ...else get the new content, if timeout, return the cache file
	if( !$con = file_get_contents( $url ) ) {
		return file_get_contents( "cache/$file" );
	}
 
	// store the new content in the cache file
	if( $f = fopen( "cache/$file", 'w' ) )
	{
		fwrite( $f, $con );
		fclose( $f );
	}
 
	// ...and return the new content
	return $con;
}
 
if( $con = getContents( 'http://some.web.page' ) ) {
	// Woohoo! Do something with it...
}
 
Tags: Development, PHP. No comments yet.
 
 
 
 
 
5 Aug 2009
 

If you build a website in these days. You must be very alerted by the spambots that are crawling trough your site. When I build my blog, I was very aware of this and made some simpel yet effective code to protect some of my html by using Javascript.

I see lots of website just doing mymail [at] yourwebsite [dot] com. This will proberply help for some of the spambots. But definitely not all! If I was a spambot, I would also looking for the [at]'s and [dot]'s, and also the {at}'s and {dot}'s like I do.

Here is the snippet:

 
function protect( $s )
{
	$s = str_replace( "\n", ' ', str_replace( "\r", ' ', $s ) );
 
	$a = array();
	$p = 0;
	$l = strlen( $s );
 
	while( $p < $l )
	{
		$n = rand( 1, 4 );
		$a[] = substr( $s, $p, $n );
		$p += $n;
	}
 
	asort( $a );
 
	$j = 'var a = new Array();';
 
	foreach( $a as $k => &$v ) {
		$j .= "a[$k] = '" . addslashes( $v ) . "';";
	}
 
	$j .= 'document.write( a.join("") );';
 
	return '<script language="javascript">' . $j . '</script>';
}

You can just do the following to write the protected code.

 
echo protect( 'Help! Protect me from these evil spambots! Please!' );
echo protect( '<a href="mailto:insert.mail@address.here">e-mail me</a>' );

What it does is. It cuts the inputted string into random length en put them into a array. Sort the array to make ik not linear. And let Javascript join it into a string and document.write() it. This time Javascript writing it and your code would not be seen as plain text in the html source code.

 
Tags: Development, PHP. Comments (2).