Introduction to Php's session and basic use of cookies

Source: Internet
Author: User
Tags session id php session setcookie
<span id="Label3"></p>Introduction to Cookies and session<p><p>Most of the 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 the site landing and other functions), we often use cookies and session to track and Judge.</p></p><p><p>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 can be tracked.</p></p><p><p>A cookie is a mechanism for storing data on a remote browser to track and identify Users. Cookies are completely kept on the client side such as: IE Firefox will no longer be available when the client prohibits Cookies.</p></p>The configuration and application of cookies<pre class="brush: php;"><pre class="brush: php;"></pre></pre><p><p>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.</p></p><p><p>Like what:</p></p><pre class="brush: php;"><pre class="brush: php;">Setcookie ("Cookie", "cookievalue", time () +3600, "/librarys", ". nowamagic.net", 1);</pre></pre><p><p>1. Receiving and processing cookies</p></p><p><p>Php's support for the reception and processing of cookies is very good, is completely automatic, as is the principle of the form variable, very simple.</p></p><p><p>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.</p></p><p><p>Examples are as follows: (assuming that they were set up in the previous page and still Valid)</p></p><pre class="brush: php;"><pre class="brush: php;"></pre></pre><p><p>2. Delete Cookies</p></p><p><p>There are two ways to delete an already existing cookie:</p></p> <ul> <ul> <li>Setcookie ("Cookie", "");</li> <li>Setcookie ("Cookie", "value", time () -1/time ());</li> </ul> </ul><p><p>3. Restrictions on the use of cookies</p></p> <ol> <ol> <li>Must be set before the content output of the HTML file;</li> <li>Different browsers have inconsistent handling of cookies and sometimes have incorrect results.</li> <li>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.</li> </ol> </ol>The configuration and application of Session<pre class="brush: php;"><pre class="brush: php;">Session_Start ();//initialize SESSION. $_session[name]=value;//configuration seeeionecho $_session[name];//using Sessionisset in the header of the file ($_ Session[name]);//judgment unset ($_session[name]);//delete Session_destroy ();//consume all SESSION</pre></pre><p><p>Note: session_register (), session_unregister,session_is_registered is no longer used under php5.</p></p><p><p>Examples of Cookies usage:</p></p><pre class="brush: php;">If ($_get[' out ')} {//used to unregister cookies Setcookie (' ID ', ""); Setcookie (' pass ', ' "); echo "<script>location.href= ' login.php ' </script>"; Because cookies are not effective in time, they only take effect when you refresh them again, so the page automatically refreshes when you log off. }if ($_post[' name ']&&$_post[' password ')//if variable username and password exist, set cookies{//for setting cookies Setcookie (' ID ', $_post [' name '],time () +3600); Setcookie (' pass ', $_post[' password '],time () +3600); echo "<script>location.href= ' login.php ' </script>"; Allow cookies to be in effect}if ($_cookie[' ID ']&&$_cookie[' pass ') {//cookies settings are successful and used to display cookies 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 ></pre><p><p>Session Usage Example:</p></p><pre class="brush: php;"><pre class="brush: php;"><?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></pre></pre><p><p>Introduction to Php's session and basic use of cookies</p></p></span>

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.