5 Month 21 reply to session control session COOKIE

Source: Internet
Author: User

Wikipedia HTTP popularity ( non-state sex

Hypertext Transfer Protocol (HTTP, hypertext Transfer Protocol) is one of the most widely used network protocols on the Internet. All WWW documents must comply with this standard. HTTP was originally designed to provide a way to publish and receive HTML pages.

A: the definition and characteristics of session

Defined:

Session, in a computer, especially in a network application, is called a "conversation." In computer terminology, a session is an interval of time between an end user communicating with an interactive system, usually the time elapsed between registration and logging out of the system, and, if necessary, a certain amount of operating space.

The specific session in the Web refers to the amount of time that a user has spent browsing a website, from entering the Web site to the browser closing. So from the definition above we can see that the session is actually a specific time concept.

It is important to note that the concept of a session needs to include specific clients, specific server-side, and non-disruptive operating times. A session where the user and the C server are connected when the session is established with the B user and the C server are two different sessions. (excerpt from Baidu Encyclopedia)

Characteristics:

1. Stored on the server side (servers),

2 save one copy for each person,

3 can store any type of data,

4 default Expiration Time 15 minutes

Service overhead is increased, but security is high

Usage and Precautions

Whenever you use the session, you need to open it at the top of PHP to use: session_start ();

eg

<?php

Session_Start ();//Open session

$_session["UID"] = "Zhangsan";//write SESSION

echo $_session["UID"];//get SESSION data

?>

Example in code: jk.php

<? PHP Session_Start (); // open session to write at the top of the PHP code $_session ["UID"] = "Zhang San"; // Write Session Echo $_session ["UID"];? ><a href= "jk1.php" > Jump </a>

Jump page: jk1.php

<? PHP Session_Start (); Echo $_session ["UID"]; // no error when the browser page is closed,?>

Implementation of the jump can get to the first page to deposit the Zhangsan, but the direct point to open the second time will be error, but as long as there is a browser page open and then go to browse the second will be taken

case Application of Session

1. Login Screen Display requirements:

Login page:

 Public "-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
View Code

Login Processing page:

<?PHPSession_Start();include(".. /dbda.php ");$db=NewDbda ();$uid=$_post["UID"];$pwd=$_post["PWD"];$sql= "SELECT count (*) from the Users where Uid = ' {$uid} ' and Pwd = ' {$pwd}‘";$r=$db->strquery ($sql);//Login Successful return 1if($r==1){    $_session["UID"] =$uid; Header("location:main3.php");}Else{    Header("location:login3.php");}
View Code

Main Page: Mainly to return the user's information: Pay special attention to security, so will add judgment

<! DOCTYPE html Public"-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >PHPSession_Start();//Direct Output Although you can see, but there will be a direct login to the main interface to see the information//To add a judgmentif(Empty($_session["UID"])){    Header("location:login3.php"); }Echo $_session["UID"];?></body>View Code

2. Add a shopping cart case

Show fruit All information: showlist4.php

<! DOCTYPE html Public"-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >PHPinclude(".. /dbda.php ");$db=NewDbda ();$sql= "SELECT * FROM Fruit";$attr=$db->query ($sql);foreach($attr  as $v){    Echo"<tr> <td>{$v[1]}</td> <td>{$v[2]}</td> <td>{$v[3]}</td> <td>{$v[4]}</td> <td><a href= ' addgwc4.php?code={$v[0]} ' > Add to Cart </a></td> </tr>"; }       ></table><a href= "gouwuche4.php" > View shopping Cart </a></body>View Code

Shopping cart Details Table of contents: gouwuche4.php

<! DOCTYPE html Public"-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >PHPSession_Start();include(".. /dbda.php ");$db=NewDbda ();$attr=$_session["SG"];foreach($attr  as $v){    $sql= "Select Name,price from fruit where Ids = ' {$v[0]} ' "; $arr=$db->query ($sql); Echo"<tr> <td>{$arr[0][0]}</td> <td>{$arr[0][1]}</td> <td>{$v[1]}</td> </tr>";}?></table></body>View Code

Add Cart Handling: addgwc4.php

<?PHPSession_Start();$code=$_get["Code"];//If this is the first click, there is no value in the session to pass the value of the array directly .if(Empty($_session["SG"])){    $attr=Array(Array($code, 1)); $_session["sg"] =$attr; }Else{    //Nth times Click N!=1    $attr=$_session["SG"];//remove the values from the session to the array//But also the case, when the fruit is present or the fruit does not exist//to determine whether the fruit exists, the method of writing    if(Iscunzai ($code))//Click the same    {        foreach($attr  as $k=$v)        {            if($v[0]==$code)//determine the fruit code $v[0] is equal to pass the code            {                $attr[$k][1] =$v[1]+1;//$v [1] is its number            }            }                $_session["sg"] =$attr; }    Else    {        $arr=Array($code, 1); Array_push($attr,$arr); $_session["sg"] =$attr; }    }functionIscunzai ($c)//What you need is a code value to judge.{    $attr=$_session["SG"]; $b=false;//defining variables of type bool    foreach($attr  as $v)    {        $b=$b||In_array($c,$v); }        return $b;}Header("location:showlist4.php");
View Code

Operation Result:

Second: the definition and characteristics of cookies

Defined:

A "Cookie" is a small amount of information that is sent by a Web server to be stored on a Web browser so that the next time the unique visitor returns to the Web server, this information can be read back from the browser. This is useful for the browser to remember specific information about the visitor, such as the location of the last visit, the time spent, or the user's preferences (such as style sheets). A Cookie is a text file stored in a browser directory that is stored in RAM when the browser is running. Once you exit from this website or Web server, cookies can also be stored on your computer's hard drive. All cookies that are terminated when a visitor ends their browser conversation.

Features:

1 stored on the client,

2 save one copy for each person,

3 can only store strings,

4 The default never expires

Therefore, relative security is lower relative to the session

Usage: No data is viewed on the current page

<? Php

Setcookie ("UID", "Zhangsan");//Set Cookies

echo $_cookie["UID"];//currently delays the value of the time, the first refresh failed, the latter several times is good

?>

Instance operations in code:

<? PHP // Setcookie ("UID", "Zhangsan");//Set   COOKIE//echo $_cookie["UID"];//will delay the value of the time, the first refresh failed, the latter several times is good  Setcookie("UID", "Zhangsan"); // other pages can be taken to ? ><a href= "jk1.php" > Jump </a>

Jump page:

<? PHP Echo $_cookie ["UID"]; // no error when the browser page is closed,?>

5 Month 21 back to session control session COOKIE

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.