Thursday, December 25, 2008

Disable browser caching in asp.net

To improve speed and response to user, some browsers cache the whole webpage. when user request for same page, instead of going to server, browsers server this page from the cache.
This approach can create problem, browser caches a secured page, which user should see only if he is login. There is a way to force the browser not to cache an ASP.NET web page and you need to add only 1 line of code to your ASP.NET web pages in order to disable browser caching. 

Add the following line on top of the Page_Load event handler and your ASP.NET page will not be cached in the users browsers: 

Response.Cache.SetCacheability(HttpCacheability.NoCache)

Caching in asp.net

One of the techniques developers commonly relied on to speed up processing of request is the use of caching. When information is cached, it stays cached either indefinitely, until some relative time, or until some absolute time. Most commonly, information is cached for a relative time frame. That is, our database information may be fairly static, updated just a few times a week. Therefore, we might want to invalidate the cache every other day, meaning every other day the cached content is rebuilt from the database.

There are a number of classes in the .NET Framework designed to aid with caching information. ASP.NET supports three types of caching for Web-based applications:
  • Page Level Caching (called Output Caching)
  • Page Fragment Caching (often called Partial-Page Output Caching)
Output Caching : 
Page level, or output caching, caches the HTML output of dynamic requests to ASP.NET Web pages. Output caching is easy to implement. By simply using the @OuputCache page directive, ASP.NET Web pages can take advantage of this powerful technique. The syntax looks like this:

<%@OutputCache Duration="60" VaryByParam="none" %>

The Duration parameter specifies how long, in seconds, the HTML output of the Web page should be held in the cache. 
The VaryByParam is a useful setting that can be used to cache different "views" of a dynamic page whose content is generated by GET or POST values. For example, you may have an ASP.NET Web page that reads in a Part number from the QueryString and displays information about a particular widget whose part number matches the QueryString Part number. Imagine for a moment that Output Caching ignored the QueryString parameters altogether (which you can do by setting VaryByParam="none"). If the first user visited the page with QueryString /ProductInfo.aspx?PartNo=4, she would see information out widget #4. The HTML for this page would be cached. The next user now visits and wished to see information on widget #8, a la /ProductInfo.aspx?PartNo=8. If VaryByParam is set to VaryByParam="none", the Output Caching engine will assume that the requests to the two pages are synonymous, and return the cached HTML for widget #4 to the person wishing to see widget #8! To solve for this problem, you can specify that the Output Caching engine should vary its caches based on the PartNo parameter by either specifying it explicitly, like VaryByParam="PartNo", or by saying to vary on all GET/POST parameters, like: VaryByParam="*".

Partial-Page Output Caching:
Partial-Page Output Caching, or page fragment caching, allows specific regions of pages to be cached. this can be done by caching the usercontrols.  This caching can be done by adding an OutputCache directive at the top of the User Control.


Tuesday, December 9, 2008

Asynchronous operations are not allowed in this context. Page starting an asynchronous operation has to have the Async attribute set to true

If you get this error when calling web service asynchronously, add the Async=”true” attribute in the page directive at the top of page.




Wednesday, November 12, 2008

Create Thunbnail images in php

Here is the code that you can use to create thumbnails in php.

This method only support jpeg, jpg and png files.

function createthumb($originalfilename,$thumbfilename,$new_w,$new_h)
{
$system=explode(".",$name);
if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);}
if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);}
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
if ($old_x > $old_y)
{
$thumb_w=$new_w;
$thumb_h=$old_y*($new_h/$old_x);
}
if ($old_x < $old_y)
{
$thumb_w=$old_x*($new_w/$old_y);
$thumb_h=$new_h;
}
if ($old_x == $old_y)
{
$thumb_w=$new_w;
$thumb_h=$new_h;
}
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
if (preg_match("/png/",$system[1]))
{
imagepng($dst_img,$filename);
} else {
imagejpeg($dst_img,$filename);
}
imagedestroy($dst_img);
imagedestroy($src_img);
}

Wednesday, September 24, 2008

Removing HTML tags from strings in Javascript

To remove html tags from a string use following code snippet:

function StripHTMLTags(strSrc)
{
strSrc.replace( /<[^<|>]+?>/gi,'' );
}

Sunday, September 21, 2008

Image Caching in Apache Webserver

Using Apache with the mod_expire module, you can define the Webserver to allow the client to “cache” images, including java script and CSS files.
This greatly improves the load-time of the website.
To enable edit the httpd.conf and define:
enable mod_expire module

    LoadModule expires_module modules/mod_expires.so

there are different way of configuring caching in apache.

Caching Files by Extension:

ExpiresActive On
   <Directory "/home/website/public_html">
Options FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
ExpiresDefault A300
<FilesMatch "\.html$">
Expires A86400
</FilesMatch>
<FilesMatch "\.(gif|jpg|png|js|css)$">
Expires A2592000
</FilesMatch>
</Directory>

Caching Files by MIME Type:

ExpiresActive On
ExpiresByType text/java script “access plus 1 day”
ExpiresByType image/gif A2592000
ExpiresByType image/jpg A2592000

Now restart apache. ExpiresDefault A300 sets the default expiry time to 300 seconds after access (A). Using M300 would set the expiry time to 300 seconds after file modification. Above setting will cache gif,jpg, png images for 259200 seconds(30 days) after access(A signifies access).

Saturday, September 6, 2008

PHP Framework

For a long time, I am using asp.net to build dynamic website. But recently for a project I need to move to php. Being a asp.net geek, I found it very difficult to work in php. But after some search I found a great php framework Prado. I was really amazed that these guys have build such a great framework. Now I can feel at home.

PRADO is a component-based and event-driven programming framework for developing Web applications in PHP 5. The sole requirement to run PRADO-based applications is a Web server supporting PHP 5.1.0 or higher. And above all "It is free!". PRADO is best suitable for creating Web applications that are highly user-interactive. It can be used to develop systems as simple as a blog system to those as complex as a content management system (CMS) or a complete e-commerce solution.

Installation of PRADO mainly involves downloading and unpacking.

  1. Go to pradosoft.com to grab the latest version of PRADO.
  2. Unpack the PRADO release file to a Web-accessible directory.
Your installation of PRADO is done and you can start to play with the demo applications included in the PRADO release via URL http://web-server-address/prado/demos/. Here we assume PRADO is unpacked to the prado subdirectory under the DocumentRoot of the Web server.

Saturday, August 23, 2008

There are many articles on the web about how to improve performance of website running on apache webserver. But I summarize here, so that you can start directly using them:
  1. Remove all 404 file references
  2. Remove duplicate javascript and css references
  3. Remove unnecessary images
  4. Minify all javascripts and css. There are many tools available to do this, but my personal favourite is YUI Compressor
  5. Enabling gzip compression. To do this you need to make some changes in apache configuration file(httpd.conf but you can also do it in .htaccess file). To enable gzip compression in apache enable module mod_deflate in httpd.conf. you can do this by removing semicolon before this entry LoadModule deflate_module modules/mod_deflate.so

    Add following entry at the end of the httpd.conf:


    SetOutputFilter DEFLATE
    SetInputFilter DEFLATE

    # Netscape 4.x has some problems...
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    # Netscape 4.06-4.08 have some more problems
    BrowserMatch ^Mozilla/4\.0[678] no-gzip
    # MSIE masquerades as Netscape, but it is fine
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

    # NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
    # the above regex won't work. You can use the following
    # workaround to get the desired effect:
    BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

    # Make sure proxies don't deliver the wrong content
    #Header append Vary User-Agent env=!dont-vary


    # The two lines above compress everything unless excluded below.
    SetEnvIfNoCase Request_URI \.(?:gif|jpeg|png|jpg)$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI \.avi$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI \.mov$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI \.mp3$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI \.mp4$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI \.rm$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI \.plist$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI \.swf$ no-gzip dont-vary
    # Below is an example where you get rid of what's above and you explicity compress.
    AddOutputFilterByType DEFLATE application/x-httpd-php application/x-httpd- fastphp application/x-httpd-eruby text/html text/javascript

    DeflateFilterNote ratio
    DeflateCompressionLevel 9


    There are still many things that should be done to improve site performance. I will write about them in my next article.

Hope this helps