<html> <head> <style> @page { size 8.5in 11in; margin: 2cm } div.page { page-break-after: always } </style> </head> <body> <div class="page">Page 1 Here</div> <div class="page">Page 2 Here</div> </body> </html>
This website is dedicated to common, day to day programming and database related Frequently Asked Questions. Popular Programming and Database Articles under one blog
Friday, December 30, 2011
HTML Printing & Page Breaks
Thursday, December 22, 2011
Optimize Your Site With GZIP Compression
In Apache, enabling output compression is fairly straightforward. Add the following to your .htaccess file:
If you can't change your .htaccess file, you can use PHP to return compressed content. Give your HTML file a .php extension and add this code to the top:
In PHP:
<?php if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start(); ?>
We check the "Accept-encoding" header and return a gzipped version of the file (otherwise the regular version). This is almost like building your own webserver (what fun!). But really, try to use Apache to compress your output if you can help it. You don't want to monkey with your files.
Reference: http://betterexplained.com/articles/how-to-optimize-your-site-with-gzip-compression/
# compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
# Or, compress certain file types by extension:
<files *.html>
SetOutputFilter DEFLATE
</files>
Apache actually has two compression options:- mod_deflate is easier to set up and is standard.
- mod_gzip seems more powerful: you can pre-compress content.
If you can't change your .htaccess file, you can use PHP to return compressed content. Give your HTML file a .php extension and add this code to the top:
In PHP:
<?php if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start(); ?>
We check the "Accept-encoding" header and return a gzipped version of the file (otherwise the regular version). This is almost like building your own webserver (what fun!). But really, try to use Apache to compress your output if you can help it. You don't want to monkey with your files.
Verify Your Compression
Once you've configured your server, check to make sure you're actually serving up compressed content.- Online: Use the online gzip test to check whether your page is compressed.
- In your browser: Use Web Developer Toolbar > Information > View Document Size (like I did for Yahoo, above) to see whether the page is compressed.
- View the headers: Use Live HTTP Headers to examine the response. Look for a line that says "Content-encoding: gzip".
Reference: http://betterexplained.com/articles/how-to-optimize-your-site-with-gzip-compression/
Creating image thumbnails using PHP
Sometimes restricting images size is required. Following function is very useful to create images of certain sizes on the fly
function createThumbs($source, $dest, $requiredWidth, $requiredHeight, $fileType='')
{
$size = getimagesize($source);
$givenWidth = $size[0];
$givenHeight = $size[1];
if ($givenWidth > 800)
{
$requiredRatio = $requiredWidth / $requiredHeight;
$middleWidth = ceil($givenHeight * $requiredRatio);
if($middleWidth>$givenWidth){
$requiredRatio = $requiredHeight / $requiredWidth;
$middleHeight = ceil($givenWidth * $requiredRatio);
$middleWidth = $givenWidth;
$y = ceil(($givenHeight - $middleHeight)/2);
}
else{
$middleHeight = $givenHeight;
$middleWidth = ceil($givenHeight * $requiredRatio);
$x = ceil(($givenWidth - $middleWidth)/2);
}
$new_im = imagecreatetruecolor($requiredWidth,$requiredHeight);
$extention = strtolower(substr($source, strlen($source)-3, strlen($source)));
if ($extention == "jpg" || $extention == "jpeg") { $im = imagecreatefromjpeg($source); }
elseif ($extention == "gif") { $im = imagecreatefromgif($source); }
elseif ($extention == "png") { $im = imagecreatefrompng($source); }
else
{
//echo("ERROR: Unknown image source file format");
return;
}
imagecopyresampled($new_im,$im,0,0,$x,$y,$requiredWidth,$requiredHeight,$middleWidth,$middleHeight);
imagejpeg($new_im,$dest,85);
}
}
function createThumbs($source, $dest, $requiredWidth, $requiredHeight, $fileType='')
{
$size = getimagesize($source);
$givenWidth = $size[0];
$givenHeight = $size[1];
if ($givenWidth > 800)
{
$requiredRatio = $requiredWidth / $requiredHeight;
$middleWidth = ceil($givenHeight * $requiredRatio);
if($middleWidth>$givenWidth){
$requiredRatio = $requiredHeight / $requiredWidth;
$middleHeight = ceil($givenWidth * $requiredRatio);
$middleWidth = $givenWidth;
$y = ceil(($givenHeight - $middleHeight)/2);
}
else{
$middleHeight = $givenHeight;
$middleWidth = ceil($givenHeight * $requiredRatio);
$x = ceil(($givenWidth - $middleWidth)/2);
}
$new_im = imagecreatetruecolor($requiredWidth,$requiredHeight);
$extention = strtolower(substr($source, strlen($source)-3, strlen($source)));
if ($extention == "jpg" || $extention == "jpeg") { $im = imagecreatefromjpeg($source); }
elseif ($extention == "gif") { $im = imagecreatefromgif($source); }
elseif ($extention == "png") { $im = imagecreatefrompng($source); }
else
{
//echo("ERROR: Unknown image source file format");
return;
}
imagecopyresampled($new_im,$im,0,0,$x,$y,$requiredWidth,$requiredHeight,$middleWidth,$middleHeight);
imagejpeg($new_im,$dest,85);
}
}
Thursday, November 3, 2011
Redirect to www .htaccess
Create a .htaccess file with the below code, it will ensure that all requests coming in to domain.com will get redirected to www.domain.com
The .htaccess file needs to be placed in the root directory of your old website (i.e the same directory where your index file is placed)
The .htaccess file needs to be placed in the root directory of your old website (i.e the same directory where your index file is placed)
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
Please REPLACE domain.com and www.newdomain.com with your actual domain name.
Note* This .htaccess method of redirection works ONLY on Linux servers having the Apache Mod-Rewrite moduled enabled.
Tuesday, October 25, 2011
Why ASP.NET appears to be faster than Classic ASP?
Pages with the .aspx extension use compiled ASP.NET (based on Microsoft's .NET Framework), which makes them faster and more robust than server-side scripting in ASP, which is interpreted at run-time; however, ASP.NET pages may still include some ASP scripting
Reference: http://en.wikipedia.org/wiki/Active_Server_Pages
Reference: http://en.wikipedia.org/wiki/Active_Server_Pages
Classic ASP Built-in Objects
ASP 2.0 provided six built-in objects: Application, ASPError, Request, Response, Server, and Session.
Reference: http://en.wikipedia.org/wiki/Active_Server_Pages
Session
, for example, represents a cookie-based session that maintains the state of variables from page to page. The Active Scripting engine's support of the Component Object Model (COM) enables ASP Websites to access functionality in compiled libraries such as DLLs.Reference: http://en.wikipedia.org/wiki/Active_Server_Pages
Implementing Cookieless Session in ASP.NET
Cookies are basically text data which a web site may store of the user's machine. Cookies are not considered as safe medium to store data as they could be dangerous in some scenario. Also there might be the case that user has cookies turned off on his machine or the browser doesn't supports the cookies. Our application might get failed if it is depended on cookies support on client side. Most of the time session id is stored at client side in cookies. Therefore we won't be able to retrieve session's data in the case when cookies are not enable.
ASP.NET support cookieless execution of the application when the client doens't have cookies support. When to chose cookieless, the session id is transferred via the request url. Each and every request of the application page contains the session id embedded in its url. So the web application need not to request the session from the cookies.
To set Cookieless session in an ASP.NET application set following value in web.config file
When you have cookieless session then the url may look like this
Issue
Session IDs can be hijacked which is a security issue if above mentioned approach is used
Reference: http://www.dailycoding.com/Posts/implementing_cookieless_session_aspnet.aspx
ASP.NET Cookieless Support
ASP.NET support cookieless execution of the application when the client doens't have cookies support. When to chose cookieless, the session id is transferred via the request url. Each and every request of the application page contains the session id embedded in its url. So the web application need not to request the session from the cookies.
To set Cookieless session in an ASP.NET application set following value in web.config file
<sessionstate cookieless="true" />
When you have cookieless session then the url may look like this
http://www.dailycoding.com/Posts/(_entv9gVODTzHuenph6KAlK07..)/test.aspx
Issue
Session IDs can be hijacked which is a security issue if above mentioned approach is used
Reference: http://www.dailycoding.com/Posts/implementing_cookieless_session_aspnet.aspx
Tuesday, September 13, 2011
How to find trigger assosiated with a table ?
select 'alter table '+quotename(s.name)+'.'+quotename(object_name(o.parent_obj))+ ' disable trigger '+quotename(o.name)
from sys.sql_modules m
join sys.sysobjects o on m.object_id=o.id
join sys.schemas s on s.schema_id = o.uid
where o.type='tr'
and definition like '%TABLENAME%'
Friday, August 19, 2011
Difference between varchar(max) and varchar(8000)
Varchar(8000) stores a maximum of 8000 characters
Varchar(max) stores a maximum of 2 147 483 647 characters
Works in SQL Server 2005 or above
Varchar(max) stores a maximum of 2 147 483 647 characters
Works in SQL Server 2005 or above
Thursday, July 21, 2011
Zip Files or Folder On The Fly Using PHP
Zip Files
<?php
include_once
'CreateZipFile.inc.php'
;
$createZip
=
new
CreateZipFile;
$createZip
->addDirectory(
'/'
);
$filecontent1
=
file_get_contents
(
'fileone.txt'
);
$filecontent2
=
file_get_contents
(
'filetwo.txt'
);
$createZip
->addFile(
$filecontent1
,
'/fileone.txt'
);
$createZip
->addFile(
$filecontent2
,
'/filetwo.txt'
);
$zipFileName
=
'myzip.zip'
;
$handle
=
fopen
(
$zipFileName
,
'wb'
);
$out
= fwrite(
$handle
,
$createZip
->getZippedFile());
fclose(
$handle
);
$createZip
->forceDownload(
$zipFileName
);
@unlink(
$zipFileName
);
?>
Zip Folder
<?php
include_once
'CreateZipFile.inc.php'
;
$createZip
=
new
CreateZipFile;
$createZip
->zipDirectory(
'/testdir'
,
''
);
$zipFileName
=
'myzip.zip'
;
$handle
=
fopen
(
$zipFileName
,
'wb'
);
$out
= fwrite(
$handle
,
$createZip
->getZippedFile());
fclose(
$handle
);
$createZip
->forceDownload(
$zipFileName
);
@unlink(
$zipFileName
);
?>
Tuesday, July 5, 2011
Displaying iFrame content in a DIV
if your images are outside of your iFrame, of course your dropdown menu will not show up since you are trying to tell the dropdown menu to go out of its windows. iframe is a different window inside your html, therefore any overflow of the window will never go out, elements inside the iframe is not affected with the z-index.
what you could do is, if your http://website.com/dropdown.html has the dropdown content only, you can call it by jQuery
<div id="myDropDown">
</div>
<script type="text/javascript">
$(document).ready(function() {
$("#myDropDown").load("http://website.com/dropdown.html");
});
</script>
this will append the content of your dropdown.html into the div
what you could do is, if your http://website.com/dropdown.html has the dropdown content only, you can call it by jQuery
<div id="myDropDown">
</div>
<script type="text/javascript">
$(document).ready(function() {
$("#myDropDown").load("http://website.com/dropdown.html");
});
</script>
this will append the content of your dropdown.html into the div
Saturday, March 12, 2011
PHP function for checking http in a URL
function check_url($url)
{
if(!strstr($url, "http://"))
{
$url = "http://".$url;
}
return $url;
}
{
if(!strstr($url, "http://"))
{
$url = "http://".$url;
}
return $url;
}
Monday, February 7, 2011
Check website for http 404
function is_404($url) {
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
/* Get the HTML or whatever is linked in $url. */
$response = curl_exec($handle);
/* Check for 404 (file not found). */
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
curl_close($handle);
/* If the document has loaded successfully without any redirection or error */
if ($httpCode >= 200 && $httpCode < 300) {
return false;
} else {
return true;
}
}
Subscribe to:
Posts (Atom)