Cookies and session usage in PHP are described in detail

Source: Internet
Author: User
Tags php session setcookie
Very often, we need to track the activities of the visitors throughout the site, automatic or semi-automated identification of their identity (that is, often said site landing and other functions), at this time, we usually use cookies and session to track and judge

1. Introduction and difference between cookies and session

In a very long time, we need to track the activities of the visitors throughout the site, automatic or semi-automated identification of their identity (that is, often said site landing and other functions), this time, we often use cookies and session to track and judge.

Difference

Session information is stored on the server side, but the session ID is stored in the client cookie, of course, the PHP session storage method is diversified, so that even if the cookie is disabled to track

Cookies are completely kept on the client side such as: IE Firefox will no longer be used when the client prohibits cookies

2. Configuration and application of cookies

Setcookie (string name, string value, int expire,stringpath, string domain, int secure);
Where name is the cookie variable name identifier, you will be able to refer to the cookie variable in PHP as if it were the same as the normal variable name. Value is the initial value of the cookie variable, and expire indicates the time of the cookie variable, path is the associated route to the cookie variable, and domain is the Web site that represents the cookie variable, and secure needs to be valid for secure transmission of HTTPS.

Setcookie ("Cookie", "Cookievalue", Time () +3600, "/forum", ". php100.com", 1);
Receiving and processing cookies
PHP's support for the reception and processing of cookies is very good, is completely automatic, as is the principle of the form variable, especially simple.
For example, setting up a cookie,php named Mycookier will automatically parse it from the HTTP header received by the Web server and form a variable like the normal variable, named $ MyCookie, which is the value of the cookie. arrays also apply. Another way is to reference the PHP global variable $http_cookie_vars array.
Examples are as follows: (assuming that they were set up in the previous page and still valid)

Echo $MyCookie;
echo $CookieArray [0];
echo $_cookie["MyCookie"];
echo $HTTP _cookie_vars["MyCookie"];
Delete Cookies

There are two ways to delete an already existing cookie:

1, Setcookie ("Cookie", "");
2, Setcookie ("Cookie", "Value", Time () -1/time ());

Restrictions on the use of cookies

1, the HTML file must be set before the content output;
2. Different browsers have inconsistent handling of cookies and sometimes have incorrect results.
3, the limit is on the client. A browser can create a maximum of 30 cookies, and each cannot exceed 4KB, and each Web site can set a total of no more than 20 cookies.

3. Session Configuration and application

Copy the code code as follows:


Session_Start (); Initializes the session. Need to be in the header of the file
$_session[name]=value; Configure Seeeion
Echo $_session[name]; Use session
Isset ($_session[name]); Judge
Unset ($_session[name]); Delete
Session_destroy ();//Consume all session

Note: Session_register (), session_unregister,session_is_registered is no longer used under PHP5

Examples of cookies usage

if ($_get[' out ')
{//For cancelling cookies
Setcookie (' id ', ' ");
Setcookie (' Pass ', ' ");
echo "<script>location.href= ' login.php ' </script>";//Because cookies are not in time, they only take effect when you refresh again, so the page automatically refreshes when you log off.
}

if ($_post[' name ']&&$_post[' password ')//If the variable username and password exist, set the cookie below
{//For setting cookies
Setcookie (' id ', $_post[' name '],time () +3600);
Setcookie (' Pass ', $_post[' password '],time () +3600);
echo "<script>location.href= ' login.php ' </script>"; Make cookies effective in time
}
if ($_cookie[' id ']&&$_cookie[' pass ')
{//cookies is used to display cookies after successful setting
echo "Login Successful! <br/> User name: ". $_cookie[' id ']." <br/> password: ". $_cookie[' pass ');
echo "<br/>";
echo "<a href= ' login.php?out=out ' > Logout cookies</a>"; In double quotation marks, if there is another quotation mark, you need to use single quotation marks.
}

?>
<form action= "" method= "POST" >
User id:
<input type= "text" name= "name"/><br/><br/>
Password:
<input type= "password" name= "password"/><br/><br/>
<input type= "Submit" Name= "Submit" >
</form>

Session Usage Example

<?php//session Usage Instance session_start ();//start session, must be placed in the first sentence, otherwise it will be wrong. if ($_get[' out ']) {         unset ($_session[' id '));    unset ($_session[' Pass ');} if ($_post[' name ']&&$_post[' password ')} {      //used to set SESSION    $_session[' id ']=$_post[' name '];    $_session[' pass ']=$_post[' password ';} if ($_session[' id ']&&$_session[' pass ') {    echo "login successful! <br/> User id: ". $_session[' id ']." <br/> User password: ". $_session[' Pass ');    echo "<br/>";    echo "<a href= ' login.php?out=out ' > Logout session</a>";}? >

<form action= "login.php" method= "POST" >
User id:
<input type= "text" name= "name"/><br/><br/>
Password:
<input type= "password" name= "password"/><br/><br/>
<input type= "Submit" Name= "Submit" >
</form>

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.