Tutoring Online - Cookies and Sessions


Top Questions

Hi my dear friends.

Everybody knows what is cookie and session. But let me tell a truth, most of the beginners don’t know properly what is happening in cookies and sessions and what is the real use .I have taken so many Interviews but none of them given a good answer. I am very much sure you are going to get a good idea about Sessions and Cookies. Let us start our battle.:)

I am explaining only PHP syntax here. But the concept of Cookies and Sessions are same in all Server Side Languages like ASP.NET, JSP,RUBYetc.except syntax and some rules

what is Cookie ?

“A cookie is a very small piece of data passed from a Webserver to visitors browser, where it is saved to be sent back to the server whenever subsequent requests are made. When a Web server sends data to a browser to be saved as a cookie, it’s said that the server is setting a cookie” 

Cookies are stored either in the browser or in the Users hard disk (it creates a small text file, if it is a Persistent Cookies. Ohh My God! what is Persistent Cookie’s. don’t worry we will discuss later)

TIP-1

Setting Cookies in PHP First method-

 

header (); header ("Set-Cookie: W3answers=excellent; expires=$date; path=/");

header ("Set-Cookie: php=excellent; path=/; domain=$HTTP_HOST; expires=".gmstrftime ("%A, %d-%b-%Y %H:%M:%S GMT",time ()+100)); Second method-

Setcookie (); Send a cookie Both PHP4 and PHP5 supports 

Third method-Setrawcookie ();

Send a cookie without urlencoding the cookie value (supports php5 only) You can use either header or setcookie. But Security purpose PHP suggest to use setcookie 

TIP-2  

If we are passing anything before setcookie () or headers () we will be getting following Warnings. Warning: Cannot add header information headers know with the message already sent. 

You can avoid this error message using 

ob_start (); 

Two Types of Cookies 

1) Temporary Cookies

2) Persistent Cookies  

Temporary Cookies –

 these types of Cookies are stored in browser. If we close the browser Cookie deletes automatically. How can we create Temporary Cookies in php setcookie (‘w3answers’, ‘phpqa’);
?> Here w3answers is the name of the Cookie and phpqa is the value.

Wow, you have created a Temporary Cookie. Where this Cookie creates and stores. This Cookie is created by the server in your browser (remember it is not creating the file in the user’s hard disk, which is the specialty of Temporary Cookies) . 

So Temporary Cookies stores in your Browser. 

Persistent Cookies Those cookies which are storing in users hardisk is called Persistent Cookies. How can we create Persistent Cookies in php? To have a cookie expire 30 days from the time its set, you take the time() and add 60 seconds in a current time stamp. The following expire in 30 days: Setcookie (w3answers, phpqa, time() + 60*60*24*30); // sec/min * min/hr * hrs/day * days

So what is the main difference between Temporary and Persistent Cookies?

If you want to create a Temporary Cookies avoid time ( ie don’t use expiry in setcookie function) If you want to create a Persistent Cookies use expiry in the setcookie function (which will be the third parameter of setcookie) setcookie(Session Active, true, time() + 60*15); // secs*mins Notice that each time you set the cookie, the expiration value would Updated, effectively allowing the session to stay active for as long as the user keeps accessing pages that update the cookie with a new time Stamp. "In short, whenever you visit a Web site, your browser checks to see if cookies are available in its cookie file that have been sent by that server. If there are, the browser adds a note to the server in its request header say A cookie called “user_id” was set with the value” someuser” in a a more technical way, of course. If more than one cookie exists, they will all be sent back just like that". In the next part we will be learning about “Keeping Cookies within Your Domain”,”Retriving Cookie” “Deleting Cookies”, “ Cookie Myths”, “Sessions” and more.

Have a nice day

 

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
2 + 4 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.

Check It !

Tweet It

W3 Updates

Stay informed on our latest news!

Syndicate content