Selasa, 31 Juli 2012

Cookies and Session in the PHP






Cookies are a mechanism for storing data variables to a specific time on the client side (browser). Data on the cookies was the first time will be sent to the server, which is then stored in a web browser. These cookies have the ability to store and recall data on the client hard disk.
Example 1:
<? Php
$ Status = 0;
if (setcookie ("testCookies", "ChocoChip"));
   $ Status = 1;

if ($ status == 1) {
   echo "Ok Cookie Support";
   echo 'own cookies ". $ HTTP_COOKIE_VARS [" testCookies "];
Else {}
   echo "Cookie Support Failed";
}
?>



Display:
Ok Cookie Support
Own cookies ChocoChip
Analysis:
If the browser supports cookies the cookies with testCookies variable will be filled with ChocoChip, and filled with 1 status, if the browser can not fill the cookies, the browser does not support cookies automatically, the status will not change, that is still 0, and that comes out is the display support cookies failed.

Making Cookies
In making a complete cookies, how to write it as follows:

<? Php
setcookie (string name, string value, int expire, string path, string domain, int secure);
?>


Noteworthy is the location of the writing of this cookie, which is before the header or before as well. For calling cookies, which are called variable is $ HTTP_COOKIE_VARS ["cookie name"]. This variable is the variable's default browser.
Session almost sam with cookies, the session will be lost when the browser is closed. Every website visitor will be given a session identifier, called the session_id.
Making Session
To create a session, the function used is session_start (). Note the location of the saved session. Use php_info function (). Find session.save_path variable. This directory is used to hold the session that we make. Example 2:

<? Php session_start ();?>
<html>
<body>
<? Php print "Session <p> you is:". Session_id (). "</ P>";?>
</ Body>
</ Html>

Running Session with Automatic

To be able to run the session automatically, php.ini file to be changed. The part that changed is:

session.auto_start = 1 / / change from 0 to 1

Furthermore, the session will be run automatically at startup.

Tidak ada komentar:

Posting Komentar