PHP, Cookie and Session

In very many times, we need to track visitors activities throughout the site, for their identity for automatic or semi-automatic identification (which is usually often said that the landing site like functionality), this time, we often use Cookie and Session to tracking and judgment.

 

Session information is stored in the server side, but the session id is stored in the Client cookie, of course, session storage method php are diversified, so that even if the disabled cookie can still track Cookie completely held in the client, such as: IE firefox When the client will no longer use is prohibited cookie

 

 

2 , the configuration and application Cookie

Setcookie(string name , string value, int expire,string path, string domain, int secure);

 

Where name is the cookie variable name identification, you will be using php could he cited as an ordinary variable names to use the same cookie variable. value is the initial value of the cookie variable, expire represents the effective time of the cookie variable; path for the cookie path-related variables; domain represents the site cookie variables; secure when you need to effectively secure transmission of https.

 

 

, the configuration and application Cookie

 

Receiving and processing   Cookie

  PHP support for the reception and processing of Cookie very well, is fully automatic, with the same principles FORM variables, particularly simple.

Such as setting of a Cookie named MyCookier, PHP will automatically receive the HTTP server from WEB analyze it out in advance, and the formation of a common variable with the same variable named $ myCookie, Cookie is the value of this variable. The same applies array. Another option is a reference to the global variable $ HTTP_COOKIE_VARS PHP array.

Examples are as follows :( assume these are set after a previous page, and still valid)

 

echo $MyCookie;

echo $CookieArray[0];

echo $_COOKIE["MyCookie"];

echo $HTTP_COOKIE_VARS["MyCookie"];

 

Delete   Cookie

 

To delete an existing Cookie, there are two options:

 

1 、 SetCookie("Cookie", "");

2 、 SetCookie("Cookie", "value" , time()-1 / time() );

 

Use   Cookie restrictions

 

1, must be set before the contents of the exported HTML file;

2, different browsers handle Cookie inconsistent, and sometimes erroneous results may occur.

3, the limit is at the client. A number of Cookie browser can create up to 30, and each can not exceed 4KB, the total number of each Cookie WEB site can not be set more than 20.  

 

 

 

, configuration and application of the Session

session_start (); // initialize the session to be in the file header.

$_SESSION [name ]=value;  //  配置Seeeion

echo $_SESSION[ name ];    // 使用 session

isset ($ _SESSION [name]); // Analyzing

unset ($ _SESSION [name]); // delete

session_destroy (); // consume all session

 

Note: session_register (), session_unregister, session_is_registered no longer used in php5

 

  

 

Guess you like

Origin www.cnblogs.com/rxbook/p/11389284.html