Archive for the ‘PHP’ Category.

Compress HTML before sending to the browser

Hany function ob_gzhandler that help facilitate sending gz-encoded data to web browsers that support compressed web pages.

This is the way I do it, checking the extension it’s loaded and that it’s possible to start the encoding:

if (extension_loaded('zlib')) {//check extension is loaded
    if(!ob_start("ob_gzhandler")) ob_start();//start HTML compression, if not normal buffer input mode
}

Detect file extension in PHP

Super easy trick to find a file extension:

$file="some_file.jpg";
$ext=end(explode(".", $file);

Other ways:

Substring:

$ext = substr($file, strrpos($file, '.') + 1);

Using path info:

$info = pathinfo($file);
$ext=$info['extenstion'];

Function to check if visitor is a bot

This function will check whether the visitor is a search engine robot

function is_bot(){
	$botlist = array("Teoma", "alexa", "froogle", "Gigabot", "inktomi",
	"looksmart", "URL_Spider_SQL", "Firefly", "NationalDirectory",
	"Ask Jeeves", "TECNOSEEK", "InfoSeek", "WebFindBot", "girafabot",
	"crawler", "www.galaxy.com", "Googlebot", "Scooter", "Slurp",
	"msnbot", "appie", "FAST", "WebBug", "Spade", "ZyBorg", "rabaz",
	"Baiduspider", "Feedfetcher-Google", "TechnoratiSnoop", "Rankivabot",
	"Mediapartners-Google", "Sogou web spider", "WebAlta Crawler","TweetmemeBot",
	"Butterfly","Twitturls","Me.dium","Twiceler");
 
	foreach($botlist as $bot){
		if(strpos($_SERVER['HTTP_USER_AGENT'],$bot)!==false)
		return true;	// Is a bot
	}
 
	return false;	// Not a bot
}

There’s any other better way?

Ping your sitemap.xml to Google

Not to long a go we explained how to generate a sitemap.xml with PHP.

Well, there was something missing and I think really important, to ping Google about the changes in the sitemap.

Simplest way of doing this:

file_get_contents('http://www.google.com/webmasters/sitemaps/ping?sitemap=http://yoursite.com/sitemap.xml');

Remember to register your site at Google webmaster tools, since if you don’t will not work.

Check requirements for PHP web application

Imagine that you need to have an installation form for your web app.

Of course you will need to ask many things, but before you ask, what about been sure the client match all the software requirements?

Check the PHP version:

$phpversion = substr(PHP_VERSION, 0, 6);
if($phpversion >= 5.2) {
       echo "Right PHP version"; 
}
else{
	echo "No, you can't continue with the installation";
	exit;
}

In this example we require 5.2 at least to continue.

Check extension loaded in PHP:

if (!extension_loaded('curl')){
    echo 'Not found, please proceed to install';
    exit;
}
else echo 'Found';

In this example we check that CURL it’s loaded.

Checking apache module installed:

if (in_array('mod_rewrite',apache_get_modules())) echo 'Found';
else echo 'Not found';

In this example we check that mod_rewrite it’s loaded.

Checking folder permissions:

 if(is_writable('/images')) { 
	echo 'OK - Writable'; 
} 
elseif(!file_exists('/images')) { 
	echo 'File Not Found';
} 
else {
	echo 'Unwritable (check permissions, chmod 755 should fix this)';
	exit;
}

Checking mysql connection:

if (!mysql_connect($_POST["DB_HOST"],$_POST["DB_USER"],$_POST["DB_PASS"])){
	$msg=mysql_error();
	echo 'Mysql error:' .$msg;
	exit;
}
else echo "connected!";
Follow me