Home PHP Ajax Regex Interviews Contact us    

PHP


Interview Questions



Cookies

Sometimes it becomes necessary to track certain user details like (No. Of Visits, names, last visit, etc). The client machine stores such information and sends it to the web server whenever there is a request. Cookies data are sent along with the HTTP headers. You can look at this URL to know more about how they work.


Setting a cookie

setcookie("name", "cookievalue", time()+60, "path",domainname,secure);


name - The name of the cookie.

cookievalue - The value of the cookie.

time()+60 - The time the cookie expires.

path - The path on the server in which the cookie will be available on.

secure - Indicates that the cookie should only be transmitted over a secure HTTPS connection. When set to TRUE, the cookie will only be set if a secure connection exists. The default is FALSE.(0 and 1)


All the arguments except the name argument are optional. If only the name argument is present


Common Errors

Warning: Cannot modify header information - headers already sent by....
Always ensure there are no white spaces or HTML tags before the cookie function. When you start with a blank line in your PHP file there is a possibility of getting this error.
Example

This example will allow you to save user name and password on the client PC as cookie and retrieve them when needed. There are totally three “.php” files used and let me give a short introduction about what they do.

Index.php

This page initially checks whether the cookie has been created or not. If the cookie is created it displays the name and password stored in it.

Login.php

This page is showed when the cookie isn’t created. The user has to select the checkbox if he needs his details to be remembered.

Logout.php

This page deletes the cookie that has been created.

How secure is storing password using cookies

Generally we store the cookies with the username followed by the password. Now we can use any algorithm to encrypt the password before we store then to make it secured. Now we will have the user name and encrypted password stored in the cookie, which again can be played around. A good practice would be to avoid the storing of user name and using a unique ID generated. This is a overhead which we have to compromise to make thinks more secure.



Feed Back of this Topic
 
Name :
Email :
Topic :
Comments :

Ajax


Regular Expression


 
Copyright © 2007 123developers.com
Contact us | Disclaimer