Introduction to PHP Session Management cookie

Source: Internet
Author: User
Tags php session

    
     if (!isset($_COOKIE['visited'])){ //如果cookie不存在,则新建 setcookie("visited","www.phpddt.com",mktime()+86400,"/") ordie("客户端禁止cookie"); echo"欢迎首次访问本站";}else{ echo"欢迎再次访问本站";   //如果cookie存在}?>

Session management mechanism only cookie and session of the two mechanisms, then to small talk about, cookies and session application!
(1) A cookie is a small text file that holds user names, passwords, personalization settings, and so on, when a user visits a website,
Will generate such a file saved in the user's computer, the General browser will keep the cookies in their own temporary files!
It is important to note that for security reasons, cookies do not keep high-secret information such as bank account numbers!
To generate a cookie using the Setcookie () function in PHP, consider the following simple example:

 !--? php   if  (!  Isset  ( $_post  [ ' username ' ]) {//form showing form without submission  ?;    <  form   method  =  "POST"   action  =  ""  >  Please enter your name:  <  input   type  =  "text"   name  =  " Username "  value  = "!--? php echo $_cookie[' username ']?--";   <  input   type  =  "Submit"   value  =  "Commit" ;  !--
     
       form 
     ;  
      }else { if (!  Empty($_post[' username '])) {Setcookie ("username",$_post['  Username '],mktime () +86400,"/"); Echo "Your name is logged in"; } Else { echo"name cannot be empty";}} ?> 
  

You can see that the Setcookie () function mainly receives 6 parameters:
Name:cookie's name (required)
The value of the Value:cookie
Expires:cookie Expiration Time (required)
Path: "/" means that all arrays of this domain can be accessed and read
Domain:cookie valid domain name
The Secure:bool value, if true, indicates that the cookie is sent to the client only when the SSL-encrypted connection
In particular, it is important to note that the Setcookie () function cannot output any scripts before the error occurs.
Take a look at the following complete example:

So how do we clear cookies?
It's simple. The name of the cookie, set the Expires (cookie expiration time) parameter of the Setcookie () function to be less than the current time!
Such as:


    
     setcookie("username",NULL,mktime-3600,"/");?>

Original address ...

'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
  • '). Text (i)); }; $numbering. FadeIn (1700); }); });

    The above describes the PHP session management cookie Introduction, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.