4.part4-2day php cookie和session

A, cookie

What 1.cookie that?

A cookie is a mechanism in the client browser to store data and use it to track and identify users, in simple terms, a cookie file is temporarily stored on the user's hard drive and then the WEB server, and then read a web browser.

The cookie can not store large volumes of data and sensitive unencrypted data, otherwise it will bring security problems.

2. How to create, read, delete cookie

setcookie(name, value, expire, path, domin);

? < PHP
     // create the cookie 
    $ The expire = Time () + 60 * 60 * 30 * 24-; // valid for one month 
    setcookie ( 'the User', 'tom', $ The expire ); 
 
    // read the cookie 
    echo  $ _COOKIE [ 'the User' ]; 
 
    // delete the cookie settings cookie is valid for the last time to 
    $ delExpire = time () - 1 ;
     setcookie ( 'the User', 'tom', $ delExpire );
 >?

3.cookie life cycle

If you do not set the expiration time, close the browser cookie will disappear

If you set the expiration time, it will go away over time.

4. To then temporarily stored cookie data array or the like is necessary to use the serialization and deserialization

<? PHP
     the serialize ();   // a sequence of stored values 
    to unserialize (); // deserialization value 
?>

二、session

1. What is a session?

session session is stored on the server side, relatively safe, can be used to store a relatively small amount of information and data storage time is not long. In practical application session with the login information is stored, so you only need to visit once, you can access other pages

When you start the session session, generates a random and unique session_id, file name is the session, session_id this time there will be the server's memory. When Close this id will be automatically canceled, re-visit this page, and will generate a random unique id again

session will be stored in a cookie default cookie named PHPSESSION, a value corresponding session_id, tmp local xampp's will add another sess_ corresponding file called session_id, which is the information we store, so the use of session when the client can not be disabled cookie

2. Create, read, delete the session, the premise is you must start the session

? < PHP
     // start session 
    session_star (); 
 
    // Create a session 
    $ _SESSION [ 'name'] = 'Tom' ; 
 
    // set the session time 
    $ The expire = Time () * 60; // . 1 minutes failed 
    the setcookie ( 'PHPSESSION ',' session_id ', $ The expire );
     // read session 
    echo  $ _SESSION [' name ' ]; 
 
    // delete a certain session 
    the unset ( $ _SESSION [' name ' ]); 
     
    // completely delete session, session reset will lose all session data has been stored 
    session_destroy ();
 ?>

 

Guess you like

Origin www.cnblogs.com/ldwtry/p/12185580.html