homepage Welcome to WebmasterWorld Guest from 54.226.43.155
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / WebmasterWorld / Webmaster General
Forum Library, Charter, Moderators: phranque & physics

Webmaster General Forum

    
How to speed up your website
londrum




msg:3489862
 3:30 pm on Oct 28, 2007 (gmt 0)

i've got a bit of a mania at the moment for speeding up my website. i shave off every single byte i can in the hope i'll knock a hundredth of a second of the page loading time.
i know it's nuts, but i was wondering if anyone has got some tips to share about speeding up their site.
here are a few that i've picked up from people:

JAVASCRIPT

-- try and place your <script type="text/javascript">'s at the very end of the code (just before closing the body tag)

-- put as many of your scripts as you can inside an addLoadListener, instead of running them as they appear.

REDUCING PAGE SIZE

-- change all your variable names, classes and ID's to one or two letters. so instead of using class="content", use class="c"

-- if your directory name is not contributing a keyword, then change it to one letter as well. so use /s/ instead of /styles/

-- try and remove all the whitespace and line-breaks from the code, because even a space takes up a byte.

-- if you're using HTML rather than XHTML, then you can remove all the quotes (") from the code. so use <a href=page.html> instead of <a href="page.html">

-- remove any links to your favicon.ico in the header, as modern browsers will still find the icon even if you don't give a link to it.

-- gzip up all your pages before you send it to the user.

HTTP REQUESTS

-- try and cut down on the number of HTTP requests each page makes, by combining all your scripts into one, and all your CSS stylesheets into one as well. (you can put your print styles inside a normal stylesheet by wrapping it inside
@media print{
styles go here
}

-- cut down on the number of images too, by putting all your bullets and incidental graphics into a single background sprite, and then writing them to the page using CSS. (this also reduces the file size too, by removing all the <img src="blah blah" />'s from the code.

IMAGES

-- change all your gifs into pngs, because pngs tend to have lower file sizes.

 

piatkow




msg:3489879
 3:54 pm on Oct 28, 2007 (gmt 0)

A lot of this sounds like overkill but it is still better than the webmasters who think that because "eveybody" has fast broadband they can forget about any sort of optimisation at all.

benevolent001




msg:3489893
 4:05 pm on Oct 28, 2007 (gmt 0)

To add up if you are having database driven website

Check the following

Where ever possible in queries use UNIONS instead of OR statement

Avoid use of! NOT operator in queries , separate the query in > and < instead if those are numbers

All these factors does affect , but there effect may not be as predominant so that we can note

jdMorgan




msg:3489909
 4:28 pm on Oct 28, 2007 (gmt 0)

Declare proper Expires and Cache-control headers for each type of object (page, image, script, etc.) to allow network and client-side caching.

If you generate pages dynamically (using a script), but not all such pages change frequently, then consider caching the script's page output as a 'static image' and serving that until the page changes again (server-side page caching).

Jim

jetboy




msg:3489956
 6:23 pm on Oct 28, 2007 (gmt 0)

Buy a copy of Andrew B King's Speed Up Your Site, Google for Aaron Hopkins' Optimizing Page Load Time and listen to jdMorgan.

Meaningless CSS names and monolithic stylesheets are a bitch to work on, particularly after you've had some time off from a project. Have a well documented version of your code to work on, and write an app to compress things before you put it live.

Getting to know your HTTP server and database can't do any harm either. Things like killing server modules that you don't use and holding database tables in memory to reduce disk I/O all help ...

draggar




msg:3489979
 7:08 pm on Oct 28, 2007 (gmt 0)

A lot of this sounds like overkill but it is still better than the webmasters who think that because "eveybody" has fast broadband they can forget about any sort of optimisation at all.

And the ones who think all users are on a huge 22"+ monitor with a very high resolution.

Design it for 1024X768 and it'll force you to have smaller images which load up faster.

buckworks




msg:3489989
 7:26 pm on Oct 28, 2007 (gmt 0)

Be ruthless about eliminating pointless extras.

Do your users really need a doodad that tells them what day it is, or the temperature in your city?

jetboy




msg:3490012
 8:02 pm on Oct 28, 2007 (gmt 0)

change all your gifs into pngs, because pngs tend to have lower file sizes

Choose your image format on a case by case basis. For smaller images, such as graphical bullets, icons etc., GIFs often produce a smaller file size.

Watch out for gamma issues with PNGs in older Safaris and Operas, particularly if you use Photoshop.

If you understand the ways the various formats compress images you can even design in a compression friendly style; minimising hard edges in JPEGs and colour changes along the x-axis in GIFs.

piatkow




msg:3490383
 11:53 am on Oct 29, 2007 (gmt 0)

>>Design it for 1024X768 and it'll force you to have smaller images which load up faster.

Its so easy to make that 1.5 meg image off your digital camera smaller. You just adjust the dimensions in your html.

It is really scary how many "webmasters" believe that.

Frank_Rizzo




msg:3490432
 12:50 pm on Oct 29, 2007 (gmt 0)

HARDWARE

If you have your own server add a second / third disk for ancillary stuff like logs and temp locations.

There is no worse bottleneck than a site serving data whilst writing to log files on the same disk at the same time. Adding a disk for the logging stuff will a) increase speed considerably, b) reduce the chance of disk failure

LOGGING

Ideally you should also limit what is logged. If using Linux strip out some of the uncessary logs such as cups. In apache turn off logging for gifs, css etc.

RAMDRIVES / SHM

Sessions: set these to be stored in SHM rather than in files. If you are worried about RAM then store them on the ancillary drive only.

OPTIMIZE COMPILING

When compiling apps from source ensure you have optimum settings for CFLAGS based on your server architecture. Only compile the bits you need to. Run strip to reduce file sizes.

DamonHD




msg:3490490
 2:14 pm on Oct 29, 2007 (gmt 0)

Hi,

I actually make my living optimising code, at least in theory, and in the financial sector which really values the go-faster stripes not just for vanity. And I like well-engineered systems that avoid waste. Some good points to that end have been made above.

But it is also very well worth knowing when to stop and spend time on other things instead such as extra reliability/stability and/or new content and/or new security and/or new hardware. I have refused to hire people who have no sense of when to stop. There is always a balance to be had between competing needs in a real organisation.

One simple rule of thumb that I have, other than simply avoiding using my thumb as a rule because it isn't very straight (B^>), is that it may be safe to ignore any single factor that contributes less than ~10% to the overall runtime of the system, at least unless there is really nothing else at all to budge and you're desperate. Consider spending the (engineering) effort on almost any other aspect of the system.

Rgds

Damon

[edited by: DamonHD at 2:21 pm (utc) on Oct. 29, 2007]

nICEsHARE




msg:3491400
 1:15 pm on Oct 30, 2007 (gmt 0)

Good tips, man!
For me I hardly care to speed up if this could hardly boost my traffic. Let's say I used to do great optimizing with every single image designed for my site; but what it did is to distract the quality whilst I could only save a few MB.
As of today, many hostings provide very big space and bandwidth run on fast server!
Btw, I do consider optimizing while we can.

thecoalman




msg:3491832
 8:35 pm on Oct 30, 2007 (gmt 0)

Choose your image format on a case by case basis. For smaller images, such as graphical bullets, icons etc., GIFs often produce a smaller file size.

Content of the image plays a factor there as well, A screen shot for example of this page where there is little detail @1280X960 comes out to about 75k using .gif or 65k using .png. No dithering and the .png was a 256 image. The other benefit is the gif or .png is going to give you nice crisp edges and not produce any artifacts. Only place you might see problems is on grdients and that can be eliminated mostly by using dithering but will also increase the file size slightly.

In comaprison .jpg gives about 170k at a 75 quality. To achieve the same file size as the .gif I need to drop it to 15 which produces a tremnedous amount of atifacts.

Images such as people, outdoors etc of course need the benefits of the larger color pallete used in .jpg

londrum




msg:3491879
 8:58 pm on Oct 30, 2007 (gmt 0)

here's a tip for people with lousy hosts who don't give you access to the .htaccess file (like mine)

rename all of your css pages with a .php extension. so instead of styles.css, do styles.php

then put this at the very top of the page:

<?php
ob_start('ob_gzhandler');
header('Content-type: text/css;charset: UTF-8');
header('Cache-Control: must-revalidate');
header('Expires: '.gmdate('D, d M Y 23:59:59', strtotime('+1 week')).' GMT');
ob_start('compress');
function compress($buffer){
$buffer=preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!','',$buffer);
$buffer=str_replace(array("\r\n","\r","\n","\t",' ',' ',' '),'',$buffer);
$buffer=str_replace(array("}"),";}",$buffer);
$buffer=str_replace(array(";;}"),";}",$buffer);
return $buffer;}
?>

and this at the bottom:

<?php
ob_end_flush();ob_end_flush();
?>

this will crunch up all your code, getting rid of all the comments and line-breaks, before serving it to the visitor - whilst still allowing you to keep them as you write it. best of both worlds.
and it will gzip it up as well, saving a load of KBs.

you can gzip up all your javascript pages as well. stick this at the top:

<?php
ob_start('ob_gzhandler');
header('Content-type: text/javascript;charset: UTF-8');
header('Cache-Control: must-revalidate');
header('Expires: '.gmdate('D, d M Y 23:59:59', strtotime('+1 week')).' GMT');
?>

and this at the bottom:

<?php
ob_end_flush();
?>

Oimachi2




msg:3492236
 5:37 am on Oct 31, 2007 (gmt 0)

Someone mentioned to use .png...

My opinion but stay away from .png as they cause display problems in Firefox and sometimes in explorer also.

Stick to .jpg and .gif and you are 100% sure they will display properly everytime.

Habtom




msg:3492293
 7:34 am on Oct 31, 2007 (gmt 0)

rename all of your css pages with a .php extension

After you create the HTML files (however they are created), you can write a script to open up the files and remove the extra brakes, unwanted &nbsp;, and stuff like those and still maintain the extension of the files you have.

If you are creating the HTML files on the fly, you could follow the steps just before the files are created.

There is a lot more to this, but this is a good way of saving a few KB size from your files.

Habtom

adb64




msg:3492296
 7:40 am on Oct 31, 2007 (gmt 0)

@Oimachi2,

I also use png on my site and I've never experienced problems using FF. Can you go into more detail where you encounter problems? Maybe when using an alpha channel, but I assume most modern browser can cope with this now. BTW, the pngs I use on my site don't have an alpha channel.

Arjan

MatthewHSE




msg:3493945
 7:24 pm on Nov 1, 2007 (gmt 0)

-- change all your variable names, classes and ID's to one or two letters. so instead of using class="content", use class="c"

If you're *that* interested in saving bytes, you may as well use ID's instead of Classes whenever possible. Saves three bytes each time...

MatthewHSE




msg:3493969
 7:47 pm on Nov 1, 2007 (gmt 0)

<?php
ob_start('ob_gzhandler');
header('Content-type: text/css;charset: UTF-8');
header('Cache-Control: must-revalidate');
header('Expires: '.gmdate('D, d M Y 23:59:59', strtotime('+1 week')).' GMT');
ob_start('compress');
function compress($buffer){
$buffer=preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!','',$buffer);
$buffer=str_replace(array("\r\n","\r","\n","\t",' ',' ',' '),'',$buffer);
$buffer=str_replace(array("}"),";}",$buffer);
$buffer=str_replace(array(";;}"),";}",$buffer);
return $buffer;}
?>

Neat idea - however, it doesn't seem to work well if you use CSS shorthand notation because it takes out all the spaces. I haven't tested this yet, but it looks like it might work better by changing the following line from this...

$buffer=str_replace(array("\r\n","\r","\n","\t",' ',' ',' '),'',$buffer);

to this...

$buffer=str_replace(array("\r\n","\r","\n","\t"),'',$buffer);

Looks like the space is repeated three times in the original line anyway; any reason for that?

londrum




msg:3493995
 8:13 pm on Nov 1, 2007 (gmt 0)

it was supposed to be two spaces, then three spaces, four spaces etc. but i think this forum has cut them all down to one space when i posted it.

MatthewHSE




msg:3494008
 8:22 pm on Nov 1, 2007 (gmt 0)

I see. No wonder it didn't work! ;)

This derivation of your code works pretty well on HTML pages:

<?php
ob_start('ob_gzhandler');
header('Content-type: text/html;charset: UTF-8');
header('Cache-Control: must-revalidate');
header('Expires: '.gmdate('D, d M Y 23:59:59', strtotime('+1 week')).' GMT');
ob_start('compress');
function compress($buffer){
$buffer=str_replace(array("\r\n","\r","\n","\t"),'',$buffer);
$buffer=preg_replace('/<!--.*?-->/im','',$buffer);
$buffer=str_replace(array("}"),";}",$buffer);
$buffer=str_replace(array(";;}"),";}",$buffer);
return $buffer;}
?>

Only the content-type and preg_replace lines are changed, and the line to replace white space has been changed to be the first replacement made (required to accommodate HTML comments that start with a line break right after the <!-- part).

londrum




msg:3494070
 9:34 pm on Nov 1, 2007 (gmt 0)

of course you should really cache your pages, and serve the cached version, rather than run all that PHP every time someone visits the page.

here is the code that i use. i'm a bit of a layman with PHP so it can no doubt be improved a lot, but it does the job. maybe someone can give me some tips on how to make it better.

it's got a few bonuses in it though...
1) if a browser understands XHTML - and i mean proper XHTML, with an XML prologue and Content-Type:application/xhtml+xml - then it will serve each page with an XHTML DOCTYPE and all the correct headers, otherwise it will serve an HTML 4.01 DOCTYPE.
2) You can set proper expiry dates for each page (today, tomorrow, a week, a month, or never), and it will serve expiry date headers. It serves last-modified headers as well (you can either say it was modified 'today', or 'whenever', which will return the time it was last uploaded)
3) It will gzip up the pages
4) It will crunch up the source code too, removing all the line breaks and spaces etc. It will also remove the extra unneccessary tags from HTML pages (like </td> and </tr> ), which it doesn't need to validate, whilst leaving them there for XHTML pages.
5) It will cache all the pages for however long you choose, and serve the cached version so you don't have to rerun the code every time that someone visits. And it will save it twice as well - once for the HTML version, and once for the XHTML version. (it is set up to save them in the /cache/ directory, but you can change it)

it's better if you hive off the entire top section (everything above the <title> tag), and introduce it to every page in an include file. and likewise with the bottom section.
but if you do that, you'll have to remember to move the three variables at the top of the script into each and every page.

it's a lot of code and i don't want to take up loads of space so i've made the text really small. you might have to copy and paste it to read it!

[small]<?php $cachename='name-of-cached-file'; /* chose a filename for the cached file (must be unique) */[/small]
[small]$lastmodified='today'; /* when was the page last modified? choose 'today' or 'whenever' (whenever will return the time the file was last uploaded) */[/small]
[small]$cachetime='week'; /* how long should the user's browser cache the page? choose 'today', 'tomorrow', 'week', 'month' or 'never' */[/small]

[small]if(!isset($_SERVER['HTTP_ACCEPT'])){$_SERVER['HTTP_ACCEPT']='';}[/small]
[small]if(stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml"))[/small]
[small]{$code='xhtml'; /* this section will serve an XML prologue and XHTML DOCTYPE for pages that understand it */[/small]
[small]/* the figure 18000 below is the amount of time (in seconds) that the cached file is considered 'fresh'. after that it will rewritten again. */[/small]
[small]if(file_exists('cache/'.$cachename.'-xml.cache')&&time()-18000<filemtime('cache/'.$cachename.'-xml.cache'))[/small]
[small]{ob_start();ob_start('ob_gzhandler');[/small]
[small]@readfile('cache/'.$cachename.'-xml.cache');[/small]
[small]ob_end_flush();[/small]
[small]if($cachetime=='today'){header('Expires: '.gmdate('D, d M Y').' 23:59:59 GMT');}[/small]
[small]else if($cachetime=='tomorrow'){header('Expires: '.gmdate('D, d M Y 23:59:59', strtotime('+1 day')).' GMT');}[/small]
[small]else if($cachetime=='week'){header('Expires: '.gmdate('D, d M Y 23:59:59', strtotime('+1 week')).' GMT');}[/small]
[small]else if($cachetime=='month'){header('Expires: '.gmdate('D, d M Y 23:59:59', strtotime('+1 month')).' GMT');}[/small]
[small]else if($cachetime=='never'){header('Expires: Thu, 31 Dec 1999 23:59:59 GMT');header('Cache-Control: no-store, no-cache, must-revalidate');header('Cache-Control: post-check=0, pre-check=0', FALSE);header('Pragma: no-cache');}[/small]
[small]header('Content-Type: application/xhtml+xml; charset=UTF-8');header('Content-Length: ' . ob_get_length());[/small]
[small]ob_end_flush(); exit(); }[/small]
[small]ob_start();ob_start('ob_gzhandler');[/small]
[small]echo'<?xml version="1.0" encoding="UTF-8" standalone="no"?>[/small]
[small]<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/small]
[small]<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head>';[/small]
[small]}[/small]
[small]else {$code='html'; /* this section will deliver an HTML 4.01 DOCTYPE for browsers that don't understand XHTML */[/small]
[small]/* if you changed 18000 above, then change it below as well. */[/small]
[small]if(file_exists('cache/'.$cachename.'.cache')&&time()-18000<filemtime('cache/'.$cachename.'.cache'))[/small]
[small]{ob_start();ob_start('ob_gzhandler');[/small]
[small]@readfile('cache/'.$cachename.'.cache');[/small]
[small]ob_end_flush();[/small]
[small]if($cachetime=='today'){header('Expires: '.gmdate('D, d M Y').' 23:59:59 GMT');}[/small]
[small]else if($cachetime=='tomorrow'){header('Expires: '.gmdate('D, d M Y 23:59:59', strtotime('+1 day')).' GMT');}[/small]
[small]else if($cachetime=='week'){header('Expires: '.gmdate('D, d M Y 23:59:59', strtotime('+1 week')).' GMT');}[/small]
[small]else if($cachetime=='month'){header('Expires: '.gmdate('D, d M Y 23:59:59', strtotime('+1 month')).' GMT');}[/small]
[small]else if($cachetime=='never'){header('Expires: Thu, 31 Dec 1999 23:59:59 GMT');header('Cache-Control: no-store, no-cache, must-revalidate');header('Cache-Control: post-check=0, pre-check=0', FALSE);header('Pragma: no-cache');}[/small]
[small]header('Content-Type: text/html;charset=UTF-8');header('Content-Length: ' . ob_get_length());[/small]
[small]ob_end_flush();exit();}ob_start();ob_start('ob_gzhandler');[/small]
[small]echo'<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">[/small]
[small]<html><head><meta http-equiv=content-type content="text/html;charset=UTF-8">';[/small]
[small]}[/small]
[small]function stripwhitespace($bff){$pzcr=0;$pzed=strlen($bff)-1;$rst="";while($pzcr<$pzed){[/small]
[small]$t_poz_start=stripos($bff,"<textarea",$pzcr);if($t_poz_start===false){$bffstp=substr($bff,$pzcr);[/small]
[small]$temp=stripBuffer($bffstp);$rst.=$temp;$pzcr=$pzed;}else{$bffstp=substr($bff,$pzcr,$t_poz_start-$pzcr);[/small]
[small]$temp=stripBuffer($bffstp);$rst.=$temp;$t_poz_end=stripos($bff,"</textarea>",$t_poz_start);[/small]
[small]$temp=substr($bff,$t_poz_start,$t_poz_end-$t_poz_start);$rst.=$temp;$pzcr=$t_poz_end;}}return $rst;}[/small]
[small]function stripBuffer($bff){$bff=str_replace(array("\r\r\r\r","\r\r\r","\r\r","\r\n","\n\r","\n\n\n\n","\n\n\n","\n\n"),"\n",$bff);[/small]
[small]global $code;if($code=='html')[/small]
[small]{/* this section strips out tags which are not necessary for pages carrying an HTML 4.01 DOCTYPE - including </li>, </td> and </tr>*/[/small]
[small]$bff=str_replace(array(" />"),">",$bff);$bff=str_replace(array("</li>"),"",$bff);$bff=str_replace(array("</td>"),"",$bff);$bff=str_replace(array("</tr>"),"",$bff);[/small]
[small]}[/small]
[small]/* this section closes up all the tags and spaces */[/small]
[small]$bff=str_replace(array(">\r<a",">\n<a"),"><a",$bff);$bff=str_replace(array(">\r<b",">\n<b"),"><b",$bff);[/small]
[small]$bff=str_replace(array(">\r<d",">\n<d"),"><d",$bff);$bff=str_replace(array(">\r<h",">\n<h"),"><h",$bff);[/small]
[small]$bff=str_replace(array(">\r<i",">\n<i"),"><i",$bff);$bff=str_replace(array(">\r<i",">\n<i"),"><i",$bff);[/small]
[small]$bff=str_replace(array(">\r<l",">\n<l"),"><l",$bff);$bff=str_replace(array(">\r<m",">\n<m"),"><m",$bff);[/small]
[small]$bff=str_replace(array(">\r<p",">\n<p"),"><p",$bff);$bff=str_replace(array(">\r<t",">\n<t"),"><t",$bff);[/small]
[small]$bff=str_replace(array("v>\r<s","v>\n<s"),"v><s",$bff);$bff=str_replace(array("v>\r</","v>\n</"),"v></",$bff);[/small]
[small]$bff=str_replace(array("a>\r<s","a>\n<s"),"a><s",$bff);$bff=str_replace(array(">\r<c",">\n<c"),"><c",$bff);[/small]
[small]$bff=str_replace(array(">\r</t",">\n</t"),"></t",$bff);$bff=str_replace(array(">\r</u",">\n</u"),"></u",$bff);[/small]
[small]$bff=str_replace(array(">\r</d",">\n</d"),"></d",$bff);$bff=str_replace(array(">\r<!",">\n<!"),"><!",$bff);[/small]
[small]$bff=str_replace(array(">\r</h",">\n</h"),"></h",$bff);$bff=str_replace(array("\r<u","\n<u"),"<u",$bff);[/small]
[small]$bff=str_replace(array("/>\r","/>\n"),"/>",$bff);$bff=str_replace(array("]-->\r<s","]-->\n<s"),"]--><s",$bff);[/small]
[small]$bff=str_replace(array("e>\r<s","e>\n<s"),"e><s",$bff);[/small]
[small]$bff=ereg_replace(" {2,}",' ',$bff);return $bff; }[/small]
[small]ob_start("stripwhitespace");?>[/small]

[small]<title>Your Page title</title>[/small]
[small]</head>[/small]
[small]<body>[/small]
[small]<!-- page code goes here -->[/small]
[small]</body>[/small]
[small]</html>[/small]

[small]<?php ob_end_flush();[/small]
[small]if($lastmodified=='today'){header('Last-Modified: '.gmdate('D, d M Y').' 00:00:01 GMT');}[/small]
[small]else{$this_time=filemtime($_SERVER['SCRIPT_FILENAME']);$gmt_time=gmdate('D, d M Y H:i:s', $this_time).' GMT';header([code][small]"Last-Modified: ".$gmt_time);}[/small]

[small]if(stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml"))[/small]
[small]{$buffer=ob_get_contents();ob_end_flush();[/small]
[small]if($cachetime=='today'){header('Expires: '.gmdate('D, d M Y').' 23:59:59 GMT');}[/small]
[small]else if($cachetime=='tomorrow'){header('Expires: '.gmdate('D, d M Y 23:59:59', strtotime('+1 day')).' GMT');}[/small]
[small]else if($cachetime=='week'){header('Expires: '.gmdate('D, d M Y 23:59:59', strtotime('+1 week')).' GMT');}[/small]
[small]else if($cachetime=='month'){header('Expires: '.gmdate('D, d M Y 23:59:59', strtotime('+1 month')).' GMT');}[/small]
[small]else if($cachetime=='never'){header('Expires: Thu, 31 Dec 1999 23:59:59 GMT');header('Cache-Control: no-store, no-cache, must-revalidate');header('Cache-Control: post-check=0, pre-check=0', FALSE);header('Pragma: no-cache');}[/small]
[small]header('Content-Type: application/xhtml+xml; charset=UTF-8');header('Content-Length: ' . ob_get_length());[/small]
[small]ob_end_flush();[/small]
[small]$fp=@fopen('cache/'.$cachename.'-xml.cache','w');@fwrite($fp,$buffer);@fclose($fp);[/small]
[small]}[/small]
[small]else {$buffer=ob_get_contents();ob_end_flush();[/small]
[small]if($cachetime=='today'){header('Expires: '.gmdate('D, d M Y').' 23:59:59 GMT');}[/small]
[small]else if($cachetime=='tomorrow'){header('Expires: '.gmdate('D, d M Y 23:59:59', strtotime('+1 day')).' GMT');}[/small]
[small]else if($cachetime=='week'){header('Expires: '.gmdate('D, d M Y 23:59:59', strtotime('+1 week')).' GMT');}[/small]
[small]else if($cachetime=='month'){header('Expires: '.gmdate('D, d M Y 23:59:59', strtotime('+1 month')).' GMT');}[/small]
[small]else if($cachetime=='never'){header('Expires: Thu, 31 Dec 1999 23:59:59 GMT');header('Cache-Control: no-store, no-cache, must-revalidate');header('Cache-Control: post-check=0, pre-check=0', FALSE);header('Pragma: no-cache');}[/small]
[small]header('Content-Type: text/html; charset=UTF-8');header('Content-Length: ' . ob_get_length());[/small]
[small]ob_end_flush();[/small]
[small]$fp=@fopen('cache/'.$cachename.'.cache','w');@fwrite($fp,$buffer);@fclose($fp); }?>[/small]

rebelde




msg:3494083
 9:41 pm on Nov 1, 2007 (gmt 0)

If using ASP.Net use trace.write throughout the page to see where the bottleneck is in building the page, and use that to optimize.

With any other scripting language, just insert timestamps in milliseconds throughout a test page to see the same thing.

henry0




msg:3494510
 11:54 am on Nov 2, 2007 (gmt 0)

londrum,
Thanks, nice piece of code!
I will play with it today.

Back to png
I mostly agree but if your user has not updated IE
and runs IE6 then a transparency won't display
we had to redo a bunch of tranparency png based
because of that non IE6 support.

PHP_Chimp




msg:3494844
 5:55 pm on Nov 2, 2007 (gmt 0)

Very nice code. I have been mucking around with it and came up with an error for you -

The XHTML DTD needs to be -
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

you have HTML in upper case, so it fails validation.

londrum




msg:3495567
 4:35 pm on Nov 3, 2007 (gmt 0)

i've got one other piece of code that speeds up your site...
i got this off someone else, and tweaked it a little bit.

it is only really useful for people with lousy webhosts again (like mine), who won't allow them to use the .htaccess file.

browsers are supposed to cache images, but IE, in particular is rubbish at it. what quite often happens is that they will still request a fresh image even if they hold the cached version, to check it's age, before displaying the cached version anyway.
so you really need to deliver each image with proper expiry headers. that's what this script does.
and it also gives each image a Content-length header, which will enable the browser to make proper use of the HTTP keep-alive thing, for downloading more than one object at a time.

and.. it prevents hotlinking too!
the only downside is that you have to rewrite all your image urls. so instead of having this
<image src="/filename.gif" alt="" />
you will have to use something like this
<image src="/image.php?image=filename.gif" alt="" />

and one more little bonus... it can also send you an email everytime a website tries to hotlink an image, notifying you of the URL.

[small]<?php[/small]
[small]$imagedir="images567/"; /* path to your directory containing the images (should be something that can't be easily guessed) */[/small]
[small]$validprefixes=array("localhost","www.example.com"); /* list of allowed websites (do not include http:// or any trailing slashes) */[/small]
[small]$homepage="http://www.example.com/index.html"; /* page that the user will be redirected to, if they try and visit the image url directly */[/small]
[small]$email="example@example.com"; /* if you want to be notified about any hotlink attempts, then include your email address, otherwise change it to $email=""; */[/small]

[small]function isreferrerokay($referrer,$validprefixes){[/small]
[small]$validreferrer=0;$authreferrer=current($validprefixes);while($authreferrer){[/small]
[small]if(eregi("^https?://$authreferrer/",$referrer)){$validreferrer=1;break;}[/small]
[small]$authreferrer=next($validprefixes);}[/small]
[small]return $validreferrer;}[/small]
[small]$image=$_GET['i'];$referrer=getenv("HTTP_REFERER");if(isset($_GET['i'])){[/small]
[small]if(empty($referrer)¦¦isreferrerokay($referrer,$validprefixes)){[/small]
[small]$imagepath=$imagedir.$image;$imageinfo=getimagesize($imagepath);if($imageinfo[2]==1){$imagetype="gif";}[/small]
[small]elseif($imageinfo[2]==2){$imagetype="jpeg";}elseif($imageinfo[2]==3){$imagetype="png";}[/small]
[small]else{header("HTTP/1.0 404 Not Found");exit;}[/small]

[small]ob_start();[/small]
[small]header("Content-type: image/$imagetype");$this_time=filemtime($_SERVER['SCRIPT_FILENAME']);[/small]
[small]$gmt_time=gmdate('D, d M Y H:i:s', $this_time).' GMT';header("Last-Modified: ".$gmt_time);[/small]
[small]/* change the code below to +1 day, +2 days, +1 week, +6 weeks, +1 month, +1 year... or however long you want your users to cache your images */[/small]
[small]header('Expires: '.gmdate('D, d M Y 23:59:59', strtotime('+1 month')).' GMT');[/small]
[small]@readfile($imagepath);header('Content-Length: ' . ob_get_length());ob_end_flush();}[/small]
[small]else{if(isset($email)){[/small]
[small]mail($email,"Hotlink Alert","WARNING:\n\n$referrer\ntried to access\n$image\n","From: Hotlink Alerter <$email>");}[/small]
[small]header("HTTP/1.0 404 Not Found");}}[/small]
[small]else{header("Location: $homepage");}[/small]

[small]/* if you place this script in your root directory, and call it image.php, then your image links should look something like this:[/small]
[small]<image src="/image.php?image=filename.gif" alt="" /> */[/small]
[small]?>[/small]

has anyone noticed a big change in their website speed, or a big reduction in their page filesizes by using these scripts?

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / WebmasterWorld / Webmaster General
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved