Increase site speed

Discussion in 'HTML & Website Design' started by cnjai20, Jan 28, 2011.

  1. #1
    Hello all,

    i am handing a school website , its a HTML website and i need to increase the page speed for that i am applying the following codes in .htaccess file

    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/x-javascript

    Whether i am doing right way or is there any other way is there to speed my website
     
    cnjai20, Jan 28, 2011 IP
  2. interkiwiwebdevelopers

    interkiwiwebdevelopers Active Member

    Messages:
    109
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #2
    That looks like the right way, if your host allows GZIP compression that is. Am with Godaddy and they are ignoring my support tickets, as I am asking them why GZIP compression is not enabled on my site.
     
    interkiwiwebdevelopers, Jan 29, 2011 IP
  3. allthewebsites

    allthewebsites Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Use the following resources to speed up your website.
    
    http://code.google.com/speed/page-speed/docs/rules_intro.html
    14 Rules for Faster-Loading Web Sites
    http://stevesouders.com/hpws/rules.php
    
    Code (markup):
    Using image sprites, you can reduce the number of HTTP requests.
    Add Styles on the top
    Defer load Javascript, if possible.

    GZIP Compession code for your .htaccess

    
    <IfModule mod_deflate.c>
      SetOutputFilter DEFLATE
      SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|rar|zip)$ no-gzip
      <IfModule mod_headers.c>
        Header append Vary User-Agent
      </IfModule>
    </IfModule>
    
    
    Code (markup):
     
    allthewebsites, Jan 29, 2011 IP
  4. web-developer

    web-developer Member

    Messages:
    77
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    web-developer, Jan 29, 2011 IP
  5. tolas

    tolas Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You can let the server gzip encode your output for every page view, and this will save some bandwidth and time in transfer in exchange for some extra work for the php server,

    but if you want greater speed increases then you'll have to cache the gzipped output (aswell as the plaintext output).

    The code flow for my CMS which does this is kinda like this:

    * .htaccess in root of application (/app) sets parameters from pretty urls (site.com/app/photoalbum/title_mangled_for_url) to a single gateway php script that outputs the html for the site and the requested content.

    * the gateway script checks the server's disk cache if the requested page/browser combination has already been cached. for simplicity's sake you can use $_GET or the pretty url as key, together with an abbreviated browser id.
    anyways, if the gateway script detects a cached page is requested, you output the gzipped/plaintext cached content, or if it hasn't been, or if the cache has expired by now, it follows the "normal" site php code flow to output the page. you'd use ob_start() and ob_get_clean() to make sure you capture all the output, and then update the server's disk cache before sending the gzipped/plaintext output to the browser.

    * dont bother with a sql-stored cache, especially not if you use sql for your business logic; go for a disk-cache.
    If you're really desperate for more speed, put the cache on an SSD disk.

    this automatic caching system will result in bigger speed increases as the page's normal output generation does more calculations and/or sql/curl queries.

    it is not well suited for systems that output often-changing data, or have very little processing time per generated page (measure to know).
    So it's change-interval-per-page, relative to the processing time per page and the number of views per second that you get for such a page.
    Check against common sense, and measure/guess what your change interval, processing time and request interval are likely to be, even though these may vary wildly during the day/week/month.

    But suffice it to say that if you can save just 500ms per requested page and you serve only 10 times before the cache is expired, it'll be worth it.
    If those numbers go up, you'll be laughing way harder still.

    It can be time-consuming to do the accurate measuring and loadtesting of such a caching system, but you'll just have to bite that bullet if you want to create a site that's heavily used.

    I'll now share some of my (perhaps buggy!) code to do all this;

    
    
    // these sources are free for any type of use.
    // copylefted, no warranties, completely disclaimed.
    
    
    gateway script: very beginning:
    
    ob_start();
    ob_implicit_flush(0);
    
    require_once ('SITE-GLOBALLY_required_scripts');
    
    $userID = $_SESSION['mb_user']->rec['user_id'];
    $url = mbGetURLforCachingSystem();
    $bd = browser_detection('full');
    $browserID = $bd[0]; //str_replace ("'", "\'", json_encode ($bd));
    if (!MB_DEV && count ($_POST) == 0) {
      if (!mbCacheExcluded($url, $get)) {
        $cid = mbDoesCacheExistForPage ($url, $browserID, $userID);
        if ($cid) {
          mbUseCacheAsOutput ($url, $browserID, $userID);
          return false;
        }
      }
    }
    
    // print page HTML framework ("<html><head>...</head><body>")
    
    // print page content:
    switch ($_GET['someParameter']) {
    case 'xyz' : 
      require_once ('required_scripts-FOR_THIS_PAGE');
      output_content_for_THIS_PAGE();
      break;
    ...
    }
    
    .....
    
    
    gateway script: very end:
    
    mbOutputGZip(true);
    
    
    
    
    
    required functions, NO GUARANTEES OF ANY KIND HERE:
    
    function mbGetURLforCachingSystem() {
    	$url = $_SERVER["REQUEST_URI"];
    	$url = str_replace (':', '_', $url);
    	$url = str_replace ('?', '_', $url);
    	$url = str_replace ('/', '_', $url);
    	return $url;	
    }
    
    function mbDoesCacheExistForPage ($url, $browserID, $userID) {
    	$languageID = mbGetCurrentLanguageID();
    
    	$fn = $url;
    	$fn = MB_CONTENT_PATH.'cache/html/'.$browserID.'/'.$userID.'/'.$url.'.meta';
    	return file_exists($fn);
    }
    
    function mbUseCacheAsOutput ($url, $browserID, $userID) {
    	$fn = $url;
    	$fn = MB_CONTENT_PATH.'cache/html/'.$browserID.'/'.$userID.'/'.$fn;
    
    	$dbConn = adoEasyConnection();
    
    	mbAddCachingHeaders(date('r', filemtime($fn.'.html_gzip')), true);
    	$e = $_SERVER["HTTP_ACCEPT_ENCODING"];
    	if(headers_sent()){
    		$encoding = false;
    	}elseif( strpos($e, 'x-gzip') !== false ){
    		$encoding = 'x-gzip';
    	}elseif( strpos($e,'gzip') !== false ){
    		$encoding = 'gzip';
    	}else{
    		$encoding = false;
    	}
    
    	if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) == '"' . $hash . '"') {
    		// Return visit and no modifications, so do not send anything
    		header ("HTTP/1.0 304 Not Modified");
    		header ('Content-Length: 0');
    	} else if ($encoding) {
    		header('Content-Encoding: '.$encoding);
    		$gzipHeader = "\x1f\x8b\x08\x00\x00\x00\x00\x00";
    		echo $gzipHeader;
    		echo file_get_contents ($fn.'.html_gzip');
    		return true;
    	} else {
    		echo file_get_contents ($fn.'.html_plain');
    	}
    	return false;
    }
    
    function mbOutputGZip ($putInCache) {
    	$get = $_GET;
    	if (array_key_exists("PHPSESSID", $get)) array_splice ($get, "PHPSESSID", 1);
    	$get = str_replace ("'", "\'", json_encode ($get));
    	$post = str_replace ("'", "\'", json_encode ($_POST));
    	$url = mbGetURLforCachingSystem();
    	$userID = $_SESSION['mb_user']->rec['user_id'];
    	$desktopID = mbGetDesktopID();
    	$bd = browser_detection('full');
    	$browserID = $bd[0];//str_replace ("'", "\'", json_encode ($bd));
    
    	mbAddCachingHeaders(date('r',time()), false);
    
    /* alternative method to get encodings, deprecated;
    	$e = $_SERVER["HTTP_ACCEPT_ENCODING"];
    	if(headers_sent()){
    		$encoding = false;
    	}elseif( strpos($e, 'x-gzip') !== false ){
    		$encoding = 'x-gzip';
    	}elseif( strpos($e,'gzip') !== false ){
    		$encoding = 'gzip';
    	}else{
    		$encoding = false;
    	}
    */	
    
    	$encoding = null;
    	if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) {
    		$encodings = explode(',', strtolower(preg_replace("/\s+/", "", $_SERVER['HTTP_ACCEPT_ENCODING'])));
    	} else {
    		$encodings = explode(',', 'deflate');
    	}
    	if ((in_array('gzip', $encodings) || in_array('x-gzip', $encodings) || isset($_SERVER['---------------'])) && function_exists('ob_gzhandler') && !ini_get('zlib.output_compression')) {
    		$encoding = in_array('x-gzip', $encodings) ? "x-gzip" : "gzip";
    	}
    
    	$url = preg_replace ("/&PHPSESS.+$/i", "", $url);
    	$cc_html_plain = ob_get_contents();
    	$cc_html_plain_size = strlen($cc_html_plain);
    	$cc_html_gzip = null;
    	$cc_html_gzip_size = $cc_html_plain_size;
    	ob_end_clean();
    
    	if($encoding ){
    		$gzipHeader = "\x1f\x8b\x08\x00\x00\x00\x00\x00";
    		header('Content-Encoding: '.$encoding);
    
    //note: you can do without this $sizeJS business probably...
    		$sizeJS = HTML_JS_BEGIN."\r\n".
    			"var MB_SIZE_UNCOMPRESSED=$cc_html_plain_size; ".
    			"var MB_SIZE_COMPRESSED=99999; ".
    			"var MB_DESKTOP_ID=\"$desktopID\"; \r\n ".
    			HTML_JS_END;
    		
    		$cc_html_gzip_size = strlen($gzipHeader.gzencode($cc_html_plain.$sizeJS, 9, FORCE_GZIP));
    		$sizeJS = HTML_JS_BEGIN."\r\n".
    			"var MB_SIZE_UNCOMPRESSED=$cc_html_plain_size; ".
    			"var MB_SIZE_COMPRESSED=$cc_html_gzip_size; ".
    			"var MB_DESKTOP_ID=\"$desktopID\"; \r\n ".
    			HTML_JS_END;	
    
    
    		$dataPlain = str_replace ('</body>', $sizeJS.'</body>', $cc_html_plain);
    		if ($dataPlain==$cc_html_plain) $dataPlain = $cc_html_plain.$sizeJS;
    
    		$cc_html_gzip = gzencode($dataPlain, 9, FORCE_GZIP);//gzcompress($dataPlain, 9); gzCompress doesnt work for IE!
    		$cc_html_gzip_size = strlen($gzipHeader.$cc_html_gzip);
    		//$cc_html_gzip= substr($cc_html_gzip, 0, $cc_html_gzip_size);
    		echo $cc_html_gzip;
    		
    		$languageID =mbGetCurrentLanguageID();
    		$props = array();
    		$props["cc_desktop_id"] = $desktopID;
    		$props["cc_browser_id"] = $browserID;
    		$props["cc_user_id"] = $_SESSION["mb_user"]->rec["user_id"];
    		$props["cc_language_id"] = $languageID;
    		$props["cc_url"] = $url;
    		$props["cc_get"] = $get;
    		$props["cc_post"] = $post;
    		$props["cc_html_plain_size"] = $cc_html_plain_size;
    		$props["cc_html_gzip_size"] = $cc_html_gzip_size;
    		
    		//caching via PHP server's filesystem, no longer via DB
    		if ($putInCache) {
    			$dir = MB_CONTENT_PATH.'cache/html/'.$browserID.'/'.$userID;
    			$r = createDirectoryStructure($dir);
    		
    			$fn = $dir.'/'.$url;
    	
    	
    			$wfn = $fn.'.meta';
    			file_put_contents ($wfn, json_encode($props));
    			
    			$wfn = $fn.'.html_plain';
    			file_put_contents ($wfn, $cc_html_plain);
    	
    			$wfn = $fn.'.html_gzip';
    			file_put_contents ($wfn, $cc_html_gzip);
    		}
    	}else{
    		$sizeJS = HTML_JS_BEGIN."\r\n".
    			"var MB_SIZE_UNCOMPRESSED=$cc_html_plain_size; ".
    			"var MB_SIZE_COMPRESSED=$cc_html_gzip_size; ".
    			"var MB_DESKTOP_ID=\"$desktopID\"; \r\n".
    			HTML_JS_END;
    		print($cc_html_plain.$sizeJS);
    	}
    }
    
    function mbAddCachingHeaders ($timestamps, $canSend304) {
    //NOTE: it's been ages since i looked at this function, and i'm not sure it works correctly!
    
    		function doConditionalGet($timestamp) {
    		    // A PHP implementation of conditional get, see 
    		    //   http://fishbowl.pastiche.org/archives/001132.html
    		    $last_modified = substr(date('r', $timestamp), 0, -5).'GMT';
    		    $etag = '"'.md5($last_modified).'"';
    		    // Send the headers
    		    header("Last-Modified: $last_modified");
    		    header("ETag: $etag");
    		    // See if the client has provided the required headers
    		    $if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ?
    		        stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']) :
    		        false;
    		    $if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ?
    		        stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) : 
    		        false;
    		    if (!$if_modified_since && !$if_none_match) {
    		        return;
    		    }
    		    // At least one of the headers is there - check them
    		    if ($if_none_match && $if_none_match != $etag) {
    		        return; // etag is there but doesn't match
    		    }
    		    if ($if_modified_since && $if_modified_since != $last_modified) {
    		        return; // if-modified-since is there but doesn't match
    		    }
    		    // Nothing has changed since their last request - serve a 304 and exit
    		    header('HTTP/1.0 304 Not Modified');
    		    exit;
    		}
    	$eTag = '"'.md5($timestamps).'"';
    	if (MB_DEV || mbMustNotClientCacheThis()) {
    		header("Expires: ".MB_CACHE_VERSION_DATE);
    		header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
    	    header("ETag: $eTag");
    		header("Cache-Control: no-store, no-cache, must-revalidate");
    		header("Cache-Control: post-check=0, pre-check=0", false);
    		header("Cache-Control: private"); //TODO experimental, test and retest in next nonpublic release
    		header("Pragma: no-cache");	
    	} else {
    		header("Last-Modified: ".MB_CACHE_VERSION_DATE);
    		header("Expires: Thu, 15 Apr 2010 20:00:00 GMT");
    	    header("ETag: $eTag");
    		header("Cache-Control: cache, store");
    		header("Cache-Control: max-age=$expiresOffset");
    		header("Cache-Control: private"); //TODO experimental, test and retest in next nonpublic release
    		header("Pragma: ");
    
    		$last_modified = MB_CACHE_VERSION_DATE;	
    		if ($canSend304) {
    		    $if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ?
    			stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']) :
    			false;
    		    $if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ?
    			stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) : 
    			false;
    				
    
    		    if (!$if_modified_since && !$if_none_match) {
    			return;
    		    }
    		    // At least one of the headers is there - check them
    		    if ($if_none_match && $if_none_match != $eTag) {
    			return; // etag is there but doesn't match
    		    }
    		    if ($if_modified_since && $if_modified_since != $last_modified) {
    			return; // if-modified-since is there but doesn't match
    		    }
    			
    		    // Nothing has changed since their last request - serve a 304 and exit
    		    header('HTTP/1.0 304 Not Modified');
    		    die();
    		}
    
    	}
    
    }
    
    PHP:
     
    Last edited: Feb 6, 2011
    tolas, Feb 6, 2011 IP
  6. tolas

    tolas Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    
    //lib_browserDetection.php, goes with the previous post by me:
    
    <?php
    /*
    Author: Harald Hope, Website: http://techpatterns.com/
    Script Source URI: http://techpatterns.com/downloads/php_browser_detection.php
    
    
    /****************************************** 
    this is currently set to accept 11 parameters, although you can add as many as you want:
    1. safe - returns true/false, you can determine what makes the browser be safe lower down, 
    	currently it's set for ns4 and pre version 1 mozillas not being safe, plus all older browsers
    2. ie_version - tests to see what general IE it is, ie5x-6, ie4, or ieMac, returns these values.
    3. moz_version - returns array of moz version, version number (includes full version, + etc), rv number (for math
    	comparison), rv number (for full rv, including alpha and beta versions), and release date
    4. dom - returns true/false if it is a basic dom browser, ie >= 5, opera >= 5, all new mozillas, safaris, konquerors
    5. os - returns which os is being used
    6. os_number - returns windows versions, 95, 98, me, nt 4, nt 5 [windows 2000], nt 5.1 [windows xp],
    	Just added: os x detection[crude] otherwise returns false
    7. browser - returns the browser name, in shorthand: ie, ie4, ie5x, op, moz, konq, saf, ns4
    8. number - returns the browser version number, if available, otherwise returns '' [not available]
    9. full - returns this array: $browser_name, $version_number, $ie_version, $dom_browser, 
    	$safe_browser, $os, $os_number, $s_browser [the browser search string from the browser array], $type
    10. type - returns whether it's a bot or a browser
    11. math_number - returns basic version number, for math comparison, ie. 1.2rel2a becomes 1.2
    *******************************************/
    
    // main script, uses two other functions, which_os() and browser_version() as needed
    function browser_detection( $which_test, $user_agent = "" ) {
    	if ($user_agent == "") $user_agent = $_SERVER["HTTP_USER_AGENT"];
    
    	/*global $dom_browser, $safe_browser, $browser_user_agent, $os, $browser_name, $s_browser, $ie_version, 
    	$version_number, $os_number, $b_repeat, $moz_version, $moz_version_number, $moz_rv, $moz_rv_full, $moz_release;*/
    
    	static $dom_browser, $safe_browser, $browser_user_agent, $os, $browser_name, $s_browser, $ie_version, 
    	$version_number, $os_number, $b_repeat, $moz_version, $moz_version_number, $moz_rv, $moz_rv_full, $moz_release, 
    	$type, $math_version_number;
    
    	/* 
    	this makes the test only run once no matter how many times you call it
    	since all the variables are filled on the first run through, it's only a matter of returning the
    	the right ones 
    	*/
    	if ( true || !$b_repeat )
    	{
    		//initialize all variables with default values to prevent error
    		$dom_browser = false;
    		$type = 'bot';// default to bot since you never know with bots
    		$safe_browser = false;
    		$os = '';
    		$os_number = '';
    		$a_os_data = '';
    		$browser_name = '';
    		$version_number = '';
    		$math_version_number = '';
    		$ie_version = '';
    		$moz_version = '';
    		$moz_version_number = '';
    		$moz_rv = '';
    		$moz_rv_full = '';
    		$moz_release = '';
    		$b_success = false;// boolean for if browser found in main test
    
    		//make navigator user agent string lower case to make sure all versions get caught
    		// isset protects against blank user agent failure
    		$browser_user_agent = ( isset( $user_agent ) ) ? strtolower( $user_agent ) : '';
    		
    		/*
    		pack the browser type array, in this order
    		the order is important, because opera must be tested first, then omniweb [which has safari data in string],
    		same for konqueror, then safari, then gecko, since safari navigator user agent id's with 'gecko' in string.
    		note that $dom_browser is set for all  modern dom browsers, this gives you a default to use.
    
    		array[0] = id string for useragent, array[1] is if dom capable, array[2] is working name for browser, 
    		array[3] identifies navigator useragent type
    
    		Note: all browser strings are in lower case to match the strtolower output, this avoids possible detection
    		errors
    
    		Note: There are currently 5 navigator user agent types: 
    		bro - modern, css supporting browser.
    		bbro - basic browser, text only, table only, defective css implementation
    		bot - search type spider
    		dow - known download agent
    		lib - standard http libraries
    		*/
    		// known browsers, list will be updated routinely, check back now and then
    		$a_browser_types[] = array( 'opera', true, 'op', 'bro' );
    		$a_browser_types[] = array( 'omniweb', true, 'omni', 'bro' );// mac osx browser, now uses khtml engine:
    		$a_browser_types[] = array( 'msie', true, 'ie', 'bro' );
    		$a_browser_types[] = array( 'konqueror', true, 'konq', 'bro' );
    		$a_browser_types[] = array( 'safari', true, 'saf', 'bro' );
    		// covers Netscape 6-7, K-Meleon, Most linux versions, uses moz array below
    		$a_browser_types[] = array( 'gecko', true, 'moz', 'bro' );
    		$a_browser_types[] = array( 'netpositive', false, 'netp', 'bbro' );// beos browser
    		$a_browser_types[] = array( 'lynx', false, 'lynx', 'bbro' ); // command line browser
    		$a_browser_types[] = array( 'elinks ', false, 'elinks', 'bbro' ); // new version of links
    		$a_browser_types[] = array( 'elinks', false, 'elinks', 'bbro' ); // alternate id for it
    		$a_browser_types[] = array( 'links ', false, 'links', 'bbro' ); // old name for links
    		$a_browser_types[] = array( 'links', false, 'links', 'bbro' ); // alternate id for it
    		$a_browser_types[] = array( 'w3m', false, 'w3m', 'bbro' ); // open source browser, more features than lynx/links
    		$a_browser_types[] = array( 'webtv', false, 'webtv', 'bbro' );// junk ms webtv
    		$a_browser_types[] = array( 'amaya', false, 'amaya', 'bbro' );// w3c browser
    		$a_browser_types[] = array( 'dillo', false, 'dillo', 'bbro' );// linux browser, basic table support
    		$a_browser_types[] = array( 'ibrowse', false, 'ibrowse', 'bbro' );// amiga browser
    		$a_browser_types[] = array( 'icab', false, 'icab', 'bro' );// mac browser 
    		$a_browser_types[] = array( 'crazy browser', true, 'ie', 'bro' );// uses ie rendering engine
    		$a_browser_types[] = array( 'sonyericssonp800', false, 'sonyericssonp800', 'bbro' );// sony ericsson handheld
    		
    		// search engine spider bots:
    		$a_browser_types[] = array( 'googlebot', false, 'google', 'bot' );// google 
    		$a_browser_types[] = array( 'mediapartners-google', false, 'adsense', 'bot' );// google adsense
    		$a_browser_types[] = array( 'yahoo-verticalcrawler', false, 'yahoo', 'bot' );// old yahoo bot
    		$a_browser_types[] = array( 'yahoo! slurp', false, 'yahoo', 'bot' ); // new yahoo bot 
    		$a_browser_types[] = array( 'yahoo-mm', false, 'yahoomm', 'bot' ); // gets Yahoo-MMCrawler and Yahoo-MMAudVid bots
    		$a_browser_types[] = array( 'inktomi', false, 'inktomi', 'bot' ); // inktomi bot
    		$a_browser_types[] = array( 'slurp', false, 'inktomi', 'bot' ); // inktomi bot
    		$a_browser_types[] = array( 'fast-webcrawler', false, 'fast', 'bot' );// Fast AllTheWeb
    		$a_browser_types[] = array( 'msnbot', false, 'msn', 'bot' );// msn search 
    		$a_browser_types[] = array( 'ask jeeves', false, 'ask', 'bot' ); //jeeves/teoma
    		$a_browser_types[] = array( 'teoma', false, 'ask', 'bot' );//jeeves teoma
    		$a_browser_types[] = array( 'scooter', false, 'scooter', 'bot' );// altavista 
    		$a_browser_types[] = array( 'openbot', false, 'openbot', 'bot' );// openbot, from taiwan
    		$a_browser_types[] = array( 'ia_archiver', false, 'ia_archiver', 'bot' );// ia archiver
    		$a_browser_types[] = array( 'zyborg', false, 'looksmart', 'bot' );// looksmart 
    		$a_browser_types[] = array( 'almaden', false, 'ibm', 'bot' );// ibm almaden web crawler 
    		$a_browser_types[] = array( 'baiduspider', false, 'baidu', 'bot' );// Baiduspider asian search spider
    		$a_browser_types[] = array( 'psbot', false, 'psbot', 'bot' );// psbot image crawler 
    		$a_browser_types[] = array( 'gigabot', false, 'gigabot', 'bot' );// gigabot crawler 
    		$a_browser_types[] = array( 'naverbot', false, 'naverbot', 'bot' );// naverbot crawler, bad bot, block
    		$a_browser_types[] = array( 'surveybot', false, 'surveybot', 'bot' );// 
    		$a_browser_types[] = array( 'boitho.com-dc', false, 'boitho', 'bot' );//norwegian search engine 
    		$a_browser_types[] = array( 'objectssearch', false, 'objectsearch', 'bot' );// open source search engine
    		$a_browser_types[] = array( 'answerbus', false, 'answerbus', 'bot' );// http://www.answerbus.com/, web questions
    		$a_browser_types[] = array( 'sohu-search', false, 'sohu', 'bot' );// chinese media company, search component
    		$a_browser_types[] = array( 'iltrovatore-setaccio', false, 'il-set', 'bot' );
    
    		// various http utility libaries 
    		$a_browser_types[] = array( 'w3c_validator', false, 'w3c', 'lib' ); // uses libperl, make first
    		$a_browser_types[] = array( 'wdg_validator', false, 'wdg', 'lib' ); // 
    		$a_browser_types[] = array( 'libwww-perl', false, 'libwww-perl', 'lib' ); 
    		$a_browser_types[] = array( 'jakarta commons-httpclient', false, 'jakarta', 'lib' );
    		$a_browser_types[] = array( 'python-urllib', false, 'python-urllib', 'lib' ); 
    		
    		// download apps 
    		$a_browser_types[] = array( 'getright', false, 'getright', 'dow' );
    		$a_browser_types[] = array( 'wget', false, 'wget', 'dow' );// open source downloader, obeys robots.txt
    
    		// netscape 4 and earlier tests, put last so spiders don't get caught
    		$a_browser_types[] = array( 'mozilla/4.', false, 'ns', 'bbro' );
    		$a_browser_types[] = array( 'mozilla/3.', false, 'ns', 'bbro' );
    		$a_browser_types[] = array( 'mozilla/2.', false, 'ns', 'bbro' );
    		
    		//$a_browser_types[] = array( '', false ); // browser array template
    
    		/* 
    		moz types array
    		note the order, netscape6 must come before netscape, which  is how netscape 7 id's itself.
    		rv comes last in case it is plain old mozilla 
    		*/
    		$moz_types = array( 'firebird', 'phoenix', 'firefox', 'iceweasel', 'galeon', 'k-meleon', 'camino', 'epiphany', 'netscape6', 'netscape', 'multizilla', 'rv' );
    
    		/*
    		run through the browser_types array, break if you hit a match, if no match, assume old browser
    		or non dom browser, assigns false value to $b_success.
    		*/
    		for ($i = 0; $i < count($a_browser_types); $i++)
    		{
    			//unpacks browser array, assigns to variables
    			$s_browser = $a_browser_types[$i][0];// text string to id browser from array
    
    			if (stristr($browser_user_agent, $s_browser)) 
    			{
    				// it defaults to true, will become false below if needed
    				// this keeps it easier to keep track of what is safe, only 
    				//explicit false assignment will make it false.
    				$safe_browser = true;
    
    				// assign values based on match of user agent string
    				$dom_browser = $a_browser_types[$i][1];// hardcoded dom support from array
    				$browser_name = $a_browser_types[$i][2];// working name for browser
    				$type = $a_browser_types[$i][3];// sets whether bot or browser
    
    				switch ( $browser_name )
    				{
    					// this is modified quite a bit, now will return proper netscape version number
    					// check your implementation to make sure it works
    					case 'ns':
    						$safe_browser = false;
    						$version_number = browser_version( $browser_user_agent, 'mozilla' );
    						break;
    					case 'moz':
    						/*
    						note: The 'rv' test is not absolute since the rv number is very different on 
    						different versions, for example Galean doesn't use the same rv version as Mozilla, 
    						neither do later Netscapes, like 7.x. For more on this, read the full mozilla numbering 
    						conventions here:
    						http://www.mozilla.org/releases/cvstags.html
    						*/
    
    						// this will return alpha and beta version numbers, if present
    						$moz_rv_full = browser_version( $browser_user_agent, 'rv' );
    						// this slices them back off for math comparisons
    						$moz_rv = substr( $moz_rv_full, 0, 3 );
    
    						// this is to pull out specific mozilla versions, firebird, netscape etc..
    						for ( $i = 0; $i < count( $moz_types ); $i++ )
    						{
    							if ( stristr( $browser_user_agent, $moz_types[$i] ) ) 
    							{
    								$moz_version = $moz_types[$i];
    								$moz_version_number = browser_version( $browser_user_agent, $moz_version );
    								break;
    							}
    						}
    						// this is necesary to protect against false id'ed moz'es and new moz'es.
    						// this corrects for galeon, or any other moz browser without an rv number
    						if ( !$moz_rv ) 
    						{ 
    							$moz_rv = substr( $moz_version_number, 0, 3 ); 
    							$moz_rv_full = $moz_version_number; 
    							/* 
    							// you can use this instead if you are running php >= 4.2
    							$moz_rv = floatval( $moz_version_number ); 
    							$moz_rv_full = $moz_version_number;
    							*/
    						}
    						// this corrects the version name in case it went to the default 'rv' for the test
    						if ( $moz_version == 'rv' ) 
    						{
    							$moz_version = 'mozilla';
    						}
    						
    						//the moz version will be taken from the rv number, see notes above for rv problems
    						$version_number = $moz_rv;
    						// gets the actual release date, necessary if you need to do functionality tests
    						$moz_release = browser_version( $browser_user_agent, 'gecko/' );
    						/* 
    						Test for mozilla 0.9.x / netscape 6.x
    						test your javascript/CSS to see if it works in these mozilla releases, if it does, just default it to:
    						$safe_browser = true;
    						*/
    						if ( ( $moz_release < 20020400 ) || ( $moz_rv < 1 ) )
    						{
    							$safe_browser = false;
    						}
    						break;
    					case 'ie':
    						$version_number = browser_version( $browser_user_agent, $s_browser );
    						// first test for IE 5x mac, that's the most problematic IE out there
    						if ( stristr( $browser_user_agent, 'mac') )
    						{
    							$ie_version = 'mac';
    						}
    						// this assigns a general ie id to the $ie_version variable
    						elseif ( $version_number == 5 )
    						{
    							$ie_version = '5.0';
    						}
    						elseif ( $version_number == 6 )
    						{
    							$ie_version = '6.0';
    						}
    						elseif ( $version_number == 7 )
    						{
    							$ie_version = '7.0';
    						}
    						elseif ( $version_number == 8 )
    						{
    							$ie_version = '8.0';
    						}
    						elseif ( $version_number == 9 )
    						{
    							$ie_version = '9.0';
    						}
    						elseif ( ( $version_number > 3 ) && ( $version_number < 5 ) )
    						{
    							$dom_browser = false;
    							$ie_version = 'ie4';
    							// this depends on what you're using the script for, make sure this fits your needs
    							$safe_browser = true; 
    						}
    						else
    						{
    							$ie_version = 'old';
    							$dom_browser = false;
    							$safe_browser = false; 
    						}
    						break;
    					case 'op':
    						$version_number = browser_version( $browser_user_agent, $s_browser );
    						if ( $version_number < 5 )// opera 4 wasn't very useable.
    						{
    							$safe_browser = false; 
    						}
    						break;
    					case 'saf':
    						$version_number = browser_version( $browser_user_agent, $s_browser );
    						break;
    					/* 
    						Uncomment this section if you want omniweb to return the safari value
    						Omniweb uses khtml/safari rendering engine, so you can treat it like
    						safari if you want.
    					*/
    					/* 
    					case 'omni':
    						$s_browser = 'safari';
    						$browser_name = 'saf';
    						$version_number = browser_version( $browser_user_agent, 'applewebkit' );
    						break; 
    					*/
    					default:
    						$version_number = browser_version( $browser_user_agent, $s_browser );
    						break;
    				}
    				// the browser was id'ed
    				$b_success = true;
    				break;
    			}
    		}
    		
    		//assigns defaults if the browser was not found in the loop test
    		if ( !$b_success ) 
    		{
    			/*
    				this will return the first part of the browser string if the above id's failed
    				usually the first part of the browser string has the navigator useragent name/version in it.
    				This will usually correctly id the browser and the browser number if it didn't get
    				caught by the above routine.
    				If you want a '' to do a if browser == '' type test, just comment out all lines below
    				except for the last line, and uncomment the last line. If you want undefined values, 
    				the browser_name is '', you can always test for that
    			*/
    			// delete this part if you want an unknown browser returned
    			$s_browser = substr( $browser_user_agent, 0, strcspn( $browser_user_agent , '();') );
    			// this extracts just the browser name from the string
    			preg_match_all('#[^0-9][a-z]*-*\ *[a-z]*\ *[a-z]*#', $s_browser, $r );
    			$s_browser = $r[0];
    			$version_number = browser_version( $browser_user_agent, $s_browser );
    
    			// then uncomment this part
    			//$s_browser = '';//deletes the last array item in case the browser was not a match
    		}
    		// get os data, mac os x test requires browser/version information, this is a change from older scripts
    		$a_os_data = which_os( $browser_user_agent, $browser_name, $version_number );
    		$os = $a_os_data[0];// os name, abbreviated
    		$os_number = $a_os_data[1];// os number or version if available
    
    		// this ends the run through once if clause, set the boolean 
    		//to true so the function won't retest everything
    		$b_repeat = true;
    
    		// pulls out primary version number from more complex string, like 7.5a, 
    		// use this for numeric version comparison
    		$m = array();
    		if ( preg_match_all('#[0-9]*\.*[0-9]*#', $version_number, $m ) )
    		{
    			$math_version_number = $m[0]; 
    			//print_r($m);
    		}
    		
    	}
    	//$version_number = $_SERVER["REMOTE_ADDR"];
    	/*
    	This is where you return values based on what parameter you used to call the function
    	$which_test is the passed parameter in the initial browser_detection('os') for example call
    	*/
    	switch ( $which_test )
    	{
    		case 'safe':// returns true/false if your tests determine it's a safe browser
    			// you can change the tests to determine what is a safeBrowser for your scripts
    			// in this case sub rv 1 Mozillas and Netscape 4x's trigger the unsafe condition
    			return $safe_browser; 
    			break;
    		case 'ie_version': // returns ieMac or ie5x
    			return $ie_version;
    			break;
    		case 'moz_version':// returns array of all relevant moz information
    			$moz_array = array( $moz_version, $moz_version_number, $moz_rv, $moz_rv_full, $moz_release );
    			return $moz_array;
    			break;
    		case 'dom':// returns true/fale if a DOM capable browser
    			return $dom_browser;
    			break;
    		case 'os':// returns os name
    			return $os; 
    			break;
    		case 'os_number':// returns os number if windows
    			return $os_number;
    			break;
    		case 'browser':// returns browser name
    			return $browser_name; 
    			break;
    		case 'number':// returns browser number
    			return $version_number;
    			break;
    		case 'full':// returns all relevant browser information in an array
    			$full_array = array( $browser_name, $version_number, $ie_version, $dom_browser, $safe_browser, 
    				$os, $os_number, $s_browser, $type, $math_version_number );
    			return $full_array;
    			break;
    		case 'type':// returns what type, bot, browser, maybe downloader in future
    			return $type;
    			break;
    		case 'math_number':// returns numerical version number, for number comparisons
    			return $math_version_number;
    			break;
    		default:
    			break;
    	}
    }
    
    // gets which os from the browser string
    function which_os ( $browser_string, $browser_name, $version_number  )
    {
    	// initialize variables
    	$os = '';
    	$os_version = '';
    	/*
    	packs the os array
    	use this order since some navigator user agents will put 'macintosh' in the navigator user agent string
    	which would make the nt test register true
    	*/
    	$a_mac = array( 'mac68k', 'macppc' );// this is not used currently
    	// same logic, check in order to catch the os's in order, last is always default item
    	$a_unix = array( 'unixware', 'solaris', 'sunos', 'sun4', 'sun5', 'suni86', 'sun', 
    		'freebsd', 'openbsd', 'bsd' , 'irix5', 'irix6', 'irix', 'hpux9', 'hpux10', 'hpux11', 'hpux', 'hp-ux', 
    		'aix1', 'aix2', 'aix3', 'aix4', 'aix5', 'aix', 'sco', 'unixware', 'mpras', 'reliant',
    		'dec', 'sinix', 'unix' );
    	// only sometimes will you get a linux distro to id itself...
    	$a_linux = array( 'kanotix', 'ubuntu', 'mepis', 'debian', 'suse', 'redhat', 'slackware', 'mandrake', 'gentoo', 'linux' );
    	$a_linux_process = array ( 'i386', 'i586', 'i686' );// not use currently
    	// note, order of os very important in os array, you will get failed ids if changed
    	$a_os = array( 'beos', 'os2', 'amiga', 'webtv', 'mac', 'nt', 'win', $a_unix, $a_linux );
    
    	//os tester
    	for ( $i = 0; $i < count( $a_os ); $i++ )
    	{
    		//unpacks os array, assigns to variable
    		$s_os = $a_os[$i];
    
    		//assign os to global os variable, os flag true on success
    		//!stristr($browser_string, "linux" ) corrects a linux detection bug
    		if ( !is_array( $s_os ) && stristr( $browser_string, $s_os ) && !stristr( $browser_string, "linux" ) )
    		{
    			$os = $s_os;
    
    			switch ( $os )
    			{
    				case 'win':
    					if ( strstr( $browser_string, '95' ) )
    					{
    						$os_version = '95';
    						$os = "Windows 95";
    					}
    					elseif ( ( strstr( $browser_string, '9x 4.9' ) ) || ( strstr( $browser_string, 'me' ) ) )
    					{
    						$os_version = 'me';
    						$os = "Windows ME";
    					}
    					elseif ( strstr( $browser_string, '98' ) )
    					{
    						$os_version = '98';
    						$os = "Windows 98";
    					}
    					elseif ( strstr( $browser_string, '2000' ) )// windows 2000, for opera ID
    					{
    						$os_version = 5.0;
    						$os = 'Windows 2000';
    					}
    					elseif ( strstr( $browser_string, 'xp' ) )// windows 2000, for opera ID
    					{
    						$os_version = 5.1;
    						$os = 'Windows XP';
    					}
    					elseif ( strstr( $browser_string, '2003' ) )// windows server 2003, for opera ID
    					{
    						$os_version = 5.2;
    						$os = 'Windows 2003';
    					}
    					elseif ( strstr( $browser_string, 'ce' ) )// windows CE
    					{
    						$os_version = 'Windows CE';
    					}
    					break;
    				case 'nt':
    					if ( strstr( $browser_string, 'nt 5.2' ) )// windows server 2003
    					{
    						$os_version = 5.2;
    						$os = 'Windows server 2003';
    					}
    					elseif ( strstr( $browser_string, 'nt 5.1' ) || strstr( $browser_string, 'xp' ) )// windows xp
    					{
    						$os_version = 5.1;//
    						$os = 'Windows XP';
    					}
    					elseif ( strstr( $browser_string, 'nt 5' ) || strstr( $browser_string, '2000' ) )// windows 2000
    					{
    						$os_version = 5.0;
    						$os = "Windows 2000";
    					}
    					elseif ( strstr( $browser_string, 'nt 4' ) )// nt 4
    					{
    						$os_version = 4;
    						$os = "Windows NT4";
    					}
    					elseif ( strstr( $browser_string, 'nt 3' ) )// nt 4
    					{
    						$os_version = 3;
    						$os = "Windows NT3";
    					}
    					break;
    				case 'mac':
    					if ( strstr( $browser_string, 'os x' ) ) 
    					{
    						$os_version = 10;
    						$os = "Mac OS X";
    					}
    					//this is a crude test for os x, since safari, camino, ie 5.2, & moz >= rv 1.3 
    					//are only made for os x
    					elseif ( ( $browser_name == 'saf' ) || ( $browser_name == 'cam' ) || 
    						( ( $browser_name == 'moz' ) && ( $version_number >= 1.3 ) ) || 
    						( ( $browser_name == 'ie' ) && ( $version_number >= 5.2 ) ) )
    					{
    						$os_version = 10;
    						$os = "Mac OS X";
    					}
    					break;
    				default:
    					break;
    			}
    			break;
    		}
    		// check that it's an array, check it's the second to last item 
    		//in the main os array, the unix one that is
    		elseif ( is_array( $s_os ) && ( $i == ( count( $a_os ) - 2 ) ) )
    		{
    			for ($j = 0; $j < count($s_os); $j++)
    			{
    				if ( stristr( $browser_string, $s_os[$j] ) )
    				{
    					$os = 'unix'; //if the os is in the unix array, it's unix, obviously...
    					$os_version = ( $s_os[$j] != 'unix' ) ? $s_os[$j] : '';// assign sub unix version from the unix array
    					break;
    				}
    			}
    		} 
    		// check that it's an array, check it's the last item 
    		//in the main os array, the linux one that is
    		elseif ( is_array( $s_os ) && ( $i == ( count( $a_os ) - 1 ) ) )
    		{
    			for ($j = 0; $j < count($s_os); $j++)
    			{
    				if ( stristr( $browser_string, $s_os[$j] ) )
    				{
    					$os = 'lin';
    					// assign linux distro from the linux array, there's a default
    					//search for 'lin', if it's that, set version to ''
    					$os_version = ( $s_os[$j] != 'linux' ) ? $s_os[$j] : '';
    					break;
    				}
    			}
    		} 
    	}
    
    	// pack the os data array for return to main function
    	$os_data = array( $os, $os_version );
    	return $os_data;
    }
    
    PHP:
     
    tolas, Feb 6, 2011 IP
  7. tolas

    tolas Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    
    
    // function returns browser number, gecko rv number, or gecko release date
    //function browser_version( $browser_user_agent, $search_string, $substring_length )
    function browser_version( $browser_user_agent, $search_string )
    {
    	// 12 is the longest that will be required, handles release dates: 20020323; 0.8.0+
    	$substring_length = 12;
    	//initialize browser number, will return '' if not found
    	$browser_number = '';
    
    	// use the passed parameter for $search_string
    	// start the substring slice right after these moz search strings
    	// there are some cases of double msie id's, first in string and then with then number
    	$start_pos = 0;  
    	/* this test covers you for multiple occurrences of string, only with ie though
    	 with for example google bot you want the first occurance returned, since that's where the
    	numbering happens */
    	
    	for ( $i = 0; $i < 4; $i++ )
    	{
    		//start the search after the first string occurrence
    		if ( strpos( $browser_user_agent, $search_string, $start_pos ) !== false )
    		{
    			//update start position if position found
    			$start_pos = strpos( $browser_user_agent, $search_string, $start_pos ) + strlen( $search_string );
    			if ( $search_string != 'msie' )
    			{
    				break;
    			}
    		}
    		else 
    		{
    			break;
    		}
    	}
    
    	// this is just to get the release date, not other moz information
    	// also corrects for the omniweb 'v'
    	if ( $search_string != 'gecko/' ) 
    	{ 
    		if ( $search_string == 'omniweb' )
    		{
    			$start_pos += 2;// handles the v in 'omniweb/v532.xx
    		}
    		else
    		{
    			$start_pos++; 
    		}
    	}
    
    	// Initial trimming
    	$browser_number = substr( $browser_user_agent, $start_pos, $substring_length );
    
    	// Find the space, ;, or parentheses that ends the number
    	$browser_number = substr( $browser_number, 0, strcspn($browser_number, ' );') );
    
    	//make sure the returned value is actually the id number and not a string
    	// otherwise return ''
    	if ( !is_numeric( substr( $browser_number, 0, 1 ) ) )
    	{ 
    		$browser_number = ''; 
    	}
    	//$browser_number = strrpos( $browser_user_agent, $search_string );
    	return $browser_number;
    }
    ?>
    
    PHP:
     
    tolas, Feb 6, 2011 IP
  8. seafrontsteve

    seafrontsteve Peon

    Messages:
    451
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Don't ignore the obvious = keep each page as simple as possible!
    If the site is a blog then use a light theme and keep the number of widgets you use to a minimum.
    Reducing server load this way can produce much better improvements than any number of techie tweaks
     
    seafrontsteve, Feb 6, 2011 IP