Langsung ke konten utama

How to Use Session And Cookie in PHP

Storing data across pages using PHP is done with two variables in the global scope, called $_SESSION and $_COOKIE, and although accomplishing the same end goal, the both go about it in very different ways. The purpose of this article is to give a brief look into the differences between cookies and sessions, when it’s better to use one versus the other, and the pros and cons of the two.

The difference is in how each store data. Cookies store data locally in the user’s browser, while sessions store data on the webserver.
Session Basics

Sessions are simply server-side cookies each with a corresponding client side cookie that contains only a reference to its server-side counterpart. When a user visits a page, the client sends the reference code to the server, and PHP will then match that reference code to a server-side cookie and load the data in the server’s cookie into the $_SESSION superglobal.
Pros

1. Can store very large amounts of data easily.
2. Save bandwidth by passing only a reference to the session each pageload. A client-side cookie has to pass all of its data.
3. Data is stored on the web server. This makes sessions secure, because the data cannot be viewed or edited by the client.

Cons

1. Ends when the browser is closed unless you’ve configured php.ini to extend sessions’ cookie lifetime. Cannot last forever.

Cookie Basics

Cookie data is sent to the web server every page load. PHP reads and stores the value into the $_COOKIE superglobal. When a cookie is created, you can give it a lifespan. After that lifespan runs out, it will expire.
Pros

1. Can last as long as the website needs. They will still be there even if the browser is closed and reopened.
2. Useful for “remember me” logins
3. Useful for storing temporary user settings. For example, if a user is browsing a paginated list of items, sorted a certain way, the sorting setting can be stored in a cookie.

Cons

1. Stored in the users filesystem. This means that the user can tamper with it and view it.
2. Can only store a limited amount of data.
3. Must pass all data to the webserver each pageload. This takes up more bandwidth.

readmore
http://buildinternet.com/2010/07/when-to-use-_session-vs-_cookie/

Komentar

Postingan populer dari blog ini

DHINATACOM - 2 new articles

Here are the latest updates from " DHINATACOM " - 2 new articles Video Trailer dan Demo PES 2011 Editorial Agustus 2010 More Recent Articles Search DHINATACOM Video Trailer dan Demo PES 2011 Setelah informasi PES 2011 dan screenshot PES 2011 beredar di Internet, video trailer berisi gambaran tentang game PES 2011 juga sudah banyak disebarluaskan di Internet . Banyak website video streaming yang memuat trailer PES 2011 juga gameplay trailer yang menampilkan berbagai fitur baru pada PES 2011 yang dapat di nikmati oleh gamer saat memainkan PES 2011. Game yang baru akan dirilis pada tanggal 22 Oktober 2011 ini akan dirilis pada banyak konsol seperti Nintendo Wii, PlayStation 3, PlayStation 2, PlayStation Portable, Xbox 360, dan tentu saja PC. Nah, sambil menunggu tanggal rilisnya yang kira-kira masih 2 bulan lagi kita lihat dulu yuk video trailer PES 2011 PES 2011 In Depth Trailer! PES 2011 – Official Gameplay Trailer [HD] Pro Evolution Socce...