Changes

Jump to: navigation, search

PHP and Cookies - Creating, Reading and Writing

33 bytes added, 17:33, 13 May 2009
no edit summary
Web servers are typically stateless entities. That is to say they serve up web pages without regard to who requested the page and with no knowledge of whether that person has previously requested other pages. This makes it difficult for web based applications to track whether a visitor is new to the site or whether they have visited before and have already logged into a service. Cookies were developed to provide a mechism mechanism to track state in the otherwise stateless world of the web.
Cookies essentially provide a mechanism to store small pieces of data on the computer systems of the visitors to your site. This enables you to maintain the state of a user's visit to your site so that you can track their movement through though the site, or to store information such as their user name and address after they have entered it on one page so that they don't have to keep re-entering it on different pages.
Before going too far in implenting implementing cookies on your web site it is important to keep in mind that users can disable cookie support in their browsers. You should, therefore, avoid making your site completely dependent on cookies.
Another option for maintaining state is to use PHP Sessions (see [[Understanding PHP Sessions]] for more information). Cookies and sessions differ in important ways, and which to use depends on your requirements.
Both cookies and PHP sessions allow you to store data that is accessible across different pages of your web site, but there are differences between the two approaches.
Cookies are stored on the hard drive of the visitor to your site and are, therefore, visible to other domains you may host and run. Thay They also have a long life and can be configured to persist long after the user has left your site. Cookies are limited in size and quantity (4kb each and a maximum of 20 cookies per domain).
PHP sessions, on the other hand, are stored on the web server. This means they are not visible to other web servers you may have hosting your domain. They are also not limited in size and can be used for storing secure data, since they are not transmitted to the client browser in the way that cookies are.
== The Structure of a Cookie ==
Cookies allow data to be stored in the form of a name/value pair. Both the name and the value are set at your discretion. For example you might want to write a cookie that store stores the user name in the from form ''username=JohnW''. The cookie also contains additional information such as an expiration date and a domain.
The format of a cookies cookie is as follows:
<pre>
== Creating a Cookie in PHP ==
Cookes Cookies are created in PHP using the ''setcookie()'' function. ''setcookie()'' takes a number of arguments. The first argument is the name of the cookie (the name part of the name/value pair described earlier). The seconmd second is the value part of the name/value pair. The third argument is the optional expiration date of the cookie. The fourth argument specifies the active ''path'' for the cookie. The fifth argument is the ''domain'' setting and the sixth is the security setting (0 specifies HTTP and HTTPS and 1 specifies HTTPS only).
Based on the above information we can create a cookie using the following PHP:
Note that if you specified domain and/or path arguments when you created the cookie you must also specify them when you delete the cookie.
 
<google>BUY_PHP_BOTTOM</google>

Navigation menu