Cookie and session are widely used in java or php. cookie can be regarded as client technology, while session is server technology. For example, you can use cookies to perform automatic logon for shopping carts and websites. session is more inclined to verify this one, which is more secure than cookie because the session is stored on the server and cannot be deleted or modified at will. Below is a simple example of my learning experience.
1. Use of cookies
To save the cookie, you can directly use the setCookie function on the php page to save the cookie as follows:
<?php setCookie("username","123456",time()+120);?>
The first parameter is the cookie key, the second parameter is value, and the third parameter indicates when the cookie expires in seconds, and time () indicates the current time. This Code indicates that the cookie will expire in 2 minutes.
The same is true for cookie update. For Firefox, we can see the following cookies:
Next, delete the cookie.
SetCookie ($ cookiename, ''); or setCookie ($ cookiename, NULL); both methods can delete cookies.
It is easier to obtain the cookie by using $ _ COOKIE. You can also use this predefined ultra-Global Array to Perform Batch cookie operations.
2. session usage
(1) enable session
(2) Put the object into the session
session_start(); $_SESSION["password"]="123456";
You can save sessiona in two sentences, as shown below:
To retrieve a session, you can also use $ _ SESSION. To delete a single session, you can use unset ($ _ SESSION ["password"]). If you delete all sessions, you can use session_destroy ();
The real principle of session is not so easy to understand, and it is difficult to understand deeply. If you need to understand it, you can use firebug to view http requests and responses. After the server creates a session, it returns
PHPSESID. This ID is the unique session ID. When the browser client needs to retrieve the session, it will use this unique ID to retrieve the session information from the server. If the client disables the cookie, the session cannot be shared according to the normal code. The two simplest practices are provided here.
The first is URL rewriting. First, determine whether PHPSESSID exists. If so, set session_id (SID value );
The second is to modifySession. use_Trans_ Sid: set the value to 1.
For the first PHP type, a constant named SID can be used directly, because this situation is more extreme than usual. The specific code will not be written. If necessary, you can write comments to me. I will send them directly.
Note that php sessions can also store objects. It is best to use require_once to introduce this object before use.
Finally, for the session application, I found a simple verification code on the Internet. The Code is as follows:
<?php session_start(); Header("Content-type: image/PNG"); $im = imagecreate(44,18); $back = ImageColorAllocate($im, 245,245,245); imagefill($im,0,0,$back); $vcodes = ""; srand((double)microtime()*1000000); for($i=0;$i<4;$i++){ $font = ImageColorAllocate($im, rand(100,255),rand(0,100),rand(100,255)); $authnum=rand(1,9); $vcodes.=$authnum; imagestring($im, 5, 2+$i*10, 1, $authnum, $font); } $_SESSION['VCODE'] = $vcodes; for($i=0;$i<100;$i++) { $randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255)); imagesetpixel($im, rand()%70 , rand()%30 , $randcolor); } ImagePNG($im); ImageDestroy($im);?>
You don't need to explain how to use it.