Posts Tagged ‘session’

How to Delete Cookies?

Tuesday, July 29th, 2008

One thing you may have noticed if you have started writing Javascript to use cookies is that there is no actual delete command that can be used to delete a cookie after you have created it. This doesn’t mean that you can’t delete cookies, it just means that you need to understand how cookies work in order to be able to control when the system will delete them for you.

When you create a session cookie it will continue to exist for as long as the browser remains open and will be deleted as soon as the browser is closed. This is because session cookies are actually retained in memory by the browser and are never actually stored anywhere. You do not specify an expiry date when creating a session cookie.

If you want a cookie to last for a longer (or shorter) time than you get wioth a session cookie you need to create a first party cookie instead. With a first party cookie the cookie is actually stored in a file on your visitor’s hard drive. You specify an expiry date/time when creating a first party cookie that defines how long the cookie is to be retained on the hard drive. The cookie isn’t necessarily deleted when that date/time is reached but cookies that have passed their expiry date/’time are ignored and so as far as the browser is concerned they don’t exist.

So how does this help us if we decide that we need to delete a cookie befre the date/time that it is set to expire? Well the solution is quite simple, we change the expiry date of the cookie so that it will be considered to have already expired. Rather than having to remember what to do each time, let’s just create a small function for deleting whichever cookie that we want.

function del_cookie(name) {
document.cookie = name +
‘=; expires=Thu, 01-Jan-70 00:00:01 GMT;’;
}

Now all we need to do is to call this del_cookie() function passing it the name of whatever cookie it is that we wish to delete. The function will update the expiry date on the cookie to one long in the past so that the cookie will be considered to be expired and will be ignored by the browser exactly the same as if it didn’t exist.

Why choose that particular date for setting the expiry date to delete the cookie? Well it just happens that all of the date processing within Javascript sees the 1st January 1970 as its starting date and actually records all date/times internally as the number of milliseconds from midnight on that day. Using that date therefore is effectively equivalent to setting the expiry to zero which means that the cookie will be deleted even if your visitor has the date on their computer set incorrectly. Were we to choose a more recent date it would be possible (although very unlikely) that one of our visitors might have their computer date set incorrectly to one earlier than the exipry that we chose and so our attempt to delete the cookie wouldn’t then work for them.

PHP:How to disable PHP magic quotes effect

Monday, June 30th, 2008

set_magic_quotes_runtime(FALSE);
if (get_magic_quotes_gpc()) {
/*
All these global variables are slash-encoded by default,
because    magic_quotes_gpc is set by default!
(And magic_quotes_gpc affects more than just $_GET, $_POST, and $_COOKIE)
*/
$_SERVER = stripslashes_array($_SERVER);
$_GET = stripslashes_array($_GET);
$_POST = stripslashes_array($_POST);
$_COOKIE = stripslashes_array($_COOKIE);
$_FILES = stripslashes_array($_FILES);
$_ENV = stripslashes_array($_ENV);
$_REQUEST = stripslashes_array($_REQUEST);
$HTTP_SERVER_VARS = stripslashes_array($HTTP_SERVER_VARS);
$HTTP_GET_VARS = stripslashes_array($HTTP_GET_VARS);
$HTTP_POST_VARS = stripslashes_array($HTTP_POST_VARS);
$HTTP_COOKIE_VARS = stripslashes_array($HTTP_COOKIE_VARS);
$HTTP_POST_FILES = stripslashes_array($HTTP_POST_FILES);
$HTTP_ENV_VARS = stripslashes_array($HTTP_ENV_VARS);
if (isset($_SESSION)) {    #These are unconfirmed (?)
$_SESSION = stripslashes_array($_SESSION, ”);
$HTTP_SESSION_VARS = stripslashes_array($HTTP_SESSION_VARS, ”);
}
/*
The $GLOBALS array is also slash-encoded, but when all the above are
changed, $GLOBALS is updated to reflect those changes.  (Therefore
$GLOBALS should never be modified directly).  $GLOBALS also contains
infinite recursion, so it’s dangerous…
*/
}
function stripslashes_array($data) {
if (is_array($data)){
foreach ($data as $key => $value){
$data[$key] = stripslashes_array($value);
}
return $data;
}else{
return stripslashes($data);
}
}

PHP: Prevent PHP script Caching

Monday, June 30th, 2008

// prevent caching
header(”Expires: Sat, 01 Jan 2000 00:00:00 GMT”);
header(”Last-Modified: “.gmdate(”D, d M Y H:i:s”).” GMT”);
header(”Cache-Control: post-check=0, pre-check=0″,false);
session_cache_limiter();
session_start();

PHP:This tip/code snippet describes a simple php session example

Monday, June 30th, 2008

<?php
// Start the session
session_start();

// Init the counter
if( !isset( $_SESSION[ 'counter' ] ) )
{
$_SESSION[ 'counter' ] = 0;
}

$_SESSION[ 'counter' ]++;
// if the counter exceeds 50 reset it
if ( $_SESSION[ 'counter' ] > 50 )
{
$_SESSION[ 'counter' ] = 0;
}

// Output session ID and counter
printf(”Our session ID is: %s<br>”, session_id());
print(”The counter value is: ” . $_SESSION[ 'counter' ] );

?>

Common Errors in PHP

Saturday, June 28th, 2008

‘Warning: Cannot modify header information - headers already sent by (output started at filename line no __) ‘

If you run a PHP document get the above error message, it means that an error has occured in your php document. This takes place if there are any executable statements like echo before the set cookie or session variable. Try to find the statement and remove it and then run the same program.

Basic Seo Tips for Web Designer

Monday, June 23rd, 2008

After designing a layout for a web site, we obviously start converting it into HTML during this session as a web designer we mainly concentrate on cross browser compatibility but we also need to concentrate on some basic seo things like…

1. Using Correct Doctype.

2. Using Meta Content and Meta Keywords.

3. Using Alt tags for Images.

4. Using Valid HTML and CSS code.

5. Make your web site load fast.

1. Why to Use Doctype and what does correct doctype means?

Document type defination(DTD) is also know as Doctype, doctype informs the browser how to interpret the content of the page. If the the doctype is not declared, the browser assumes you don’t know how to code, and goes into quirks mode. If you know what you are doing and include a correct HTML doctype, your page will be rendered in standards mode.

Some Recommended DTDs to use in your Web document.

Use correct doctype, for example your using strict doctype and you use used align in in div tag or in p tag then doctype you used is not correct because align attribute is deprecated in strict doctype.

2. Using Meta Content and Meta Keywords.

Use proper keywords and content relate to your web site, that helps search engine to crawl your web site.

3. Using Alt tags for Images.

Specify alt tag for image and that alt tag should be a keyword related to web site, because images you have placed on site can be seen by visitors but not by search engines, so alt tag help search engines to read, with the help of alt tags search engines display the image results.

4. Using Valid HTML and CSS code.

Validate your html and css code and make your code w3c standard.

validate your html code here

http://validator.w3.org

validate your css code here

http://jigsaw.w3.org/css-validator

5. Make your web site load fast.

Don’t mess up your site with heavy images that makes your site heavy and slow, try to optimize images that reduces weight and helps to load fast.

Slow Opening Of File Dialogs

Wednesday, June 18th, 2008

An intermittent slowdown opening file dialogs or Windows Explorer listings is often caused by a mapped network drive. You can select Map Network Drive from Windows Explorer’s Tools menu to make a drive or a folder on a remote PC appear as a simple drive letter on your computer. This will let you access that drive or folder as if it were a local drive. After a restart, Windows reestablishes the connection when it’s needed—for example, when you open the drop-down box that displays a tree containing My Computer and all drives below it. This initial connection can sometimes be slow the first time in a session. If the remote PC is not accessible, each attempt to display the folder tree may be slowed. Most likely the reason that some of your file-open dialogs are slow and some aren’t lies in their initial display mode.

If the inconvenience of this slowdown outweighs the convenience of having a mapped network drive, simply click on Tools in Windows Explorer’s menu and select Disconnect Network Drive. Select the drive to disconnect and click on OK.