Home > Backend Development > PHP Tutorial > PHP session tracking one (41)

PHP session tracking one (41)

WBOY
Release: 2016-08-08 09:23:05
Original
907 people have browsed it

PHP Session Tracking

What is session control

We need a powerful solution so that the website can track the interaction between the client and the server, save and remember each user's identity and information, so that Session control is produced.
??What is session control:
??HTTP is a stateless protocol. This protocol cannot maintain the connection between two transactions.
??When a user requests one page and then requests another page, HTTP cannot tell us that the two requests are from the same person.
??The idea of ​​session control is to be able to track a variable in the website. By tracking the variable, we can support the user and display different content and pages based on authorization and user identity.

Session tracking technology

Hidden form fields

Add hidden form fields of session tracking fields to HTML pages, but will not be displayed in client browsers

<form action = &ldquo;main.php&rdquo;method=&rdquo;POST&rdquo;><input type = &ldquo;hidden&rdquo;name=&ldquo;checkid&rdquo;value=&ldquo;ZY7K&rdquo;></form><br>在GET 和POST 方法中指定用于存储有关会话消息的名称和值。
Copy after login

URL rewriting

?? URL (Uniform Resource Location) rewriting technology adds a unique session ID to the end of the URL to identify the session.
??For example, rewrite the following URL to pass session ID=1002

原始URL:http:<span>//</span><span>www.myphp.com/load.php</span>用添加的参数重写的URL:http:<span>//</span><span> www.myphp.com/load.php? id=1002原始URL:</span><span>http://www.myphp.com/bookinfo.php</span>用添加的参数重写的URL:http:<span>//</span><span>www.myphp.com/bookinfo.php?bookid=1000</span>
Copy after login

Cookie and session

When we need the session to be tracked in a wider range and for a longer time, we need to use cookie and session. For example, downloading things from some websites requires members to log in first. What should we do if we want to know whether the customer has logged in and can log in automatically? You can know it through cookies and sessions.
??For example, in online shopping, how does the shopping cart know which products the customer has selected? Cookies and sessions can also be recorded.
??In short, cookie and session are technologies that can record customer status. Although they are different technologies, as long as cookie can do it, session can do it too!

cookie

What is a cookie:
??Cookie is a way for the server or script to maintain client information under the http protocol.
??A cookie is a cookie (a small text file) saved by the web server on the user's browser. It can contain information about the user and is often used to save usernames, passwords, personalized settings, personal preference records, etc. . When a user accesses the server, the server can set and access cookie information.
??Cookies are saved in the cookie temporary folder of the client, usually IE or Firefox browser, and can be deleted manually. Note: If there are too many cookies on the browser and exceed the range allowed by the system, the browser will automatically delete them.

How cookies work

When a customer visits a website based on PHP technology, you can use the setcookie() function in PHP to generate a cookie. After processing, the system sends the cookie to the client and saves it in C:Documents and Settings Username Cookies directory.
??Cookies are part of the HTTP headers, so the setcookie() function must be called before any content of the HTML itself is sent to the browser. This restriction is the same as the header() function (if you need to understand the head() function, please check it yourself).
??When the customer visits the website again, the browser will automatically send the cookie corresponding to the website in the C:Documents and Settings username Cookies directory to the server, and the server will automatically convert the cookie sent from the client. into a PHP variable. In PHP5, cookies sent by the client will be converted into global variables. You can read it through $_COOKIE[‘xxx’].

Define a cookie

Set cookie:
?? Syntax: boolsetcookie(stringname,[stringvalue,[intexpire,[stringpath,[stringdomain,[intsecure]]]]]);
This cookie function can have 6 attributes, There are three commonly used parameters.
??Example:
$value="the best way is by yourself";
setcookie("cookiename",$value,time()+60*60*24*7);

setcookie parameter explanation

Receive and process cookies
PHP has good support for cookies. Just like form forms, PHP will automatically receive the HTTP header from the web server and analyze it when receiving it. When receiving, use $_COOKIE["cookiename"] or $HTTP_COOKIE_VARS["cookiename"] (not recommended)

Note:
If the website has several different file directories, use cookies without paths , then this cookie can only be accessed in the path of the file where the cookie is set. If a path is specified, the path when setting will be used as the specified path to access the cookie.

Create cookie array:

First:
setcookie("CookieArray[0]", "Value 1");
setcookie("CookieArray[1]", "Value 2");
Second:
setcookie( "CookieArray['one']", "Value 1");
setcookie("CookieArray['two']", "Value 2");

Use array in setcookie()

<?<span>php
setcookie(</span><span>"</span><span>cookie[three]</span><span>"</span>, <span>"</span><span>cookiethree</span><span>"</span><span>);
setcookie(</span><span>"</span><span>cookie[two]</span><span>"</span>, <span>"</span><span>cookietwo</span><span>"</span><span>);
setcookie(</span><span>"</span><span>cookie[one]</span><span>"</span>, <span>"</span><span>cookieone</span><span>"</span><span>);
</span><span>//</span><span> 刷新页面后,显示出来</span><span>if</span> (isset($_COOKIE[<span>'</span><span>cookie</span><span>'</span><span>])) {
</span><span>foreach</span>($_COOKIE[<span>'</span><span>cookie</span><span>'</span>] <span>as</span> $name =><span> $value){
echo </span><span>"</span><span>$name : $value <br/>\n</span><span>"</span><span>;
}
}
</span>?>
Copy after login

Delete cookie

To delete an existing cookie, there are two ways:
1. Call setcookie with only the name parameter, then the cookie named this
name will be deleted from the client;
setcookie("MyCookie"); //Delete MyCookie
2. Set the cookie expiration time to time() or time()-1. Note:
It doesn’t matter how much time() is reduced, as long as it is the expiration time, then
Then this cookie is on this page After browsing it, it was deleted
 (actually it has expired).
??For example:
setcookie("MyCookie","Value",time()-1);
  //Delete MyCookie.
 Note: When a cookie is deleted, its value is still
 valid on the current page. If you want to set the cookie to expire after the browser is closed.
  Then you can directly set expiretime to 0, or do not set this value.
For example: setcookie("name","value",0).

Cookie Notes

1. There cannot be any html output before setcookie(), that is, spaces. Blank
will not work. It must be set before the content of the html file is output
?? 2. After setcookie(), you are currently in There will be no output when the page calls echo $_COOKIE["name"]. You must refresh or go to the next page to see the cookie value.
•3. No need for browsers to handle cookies differently. The client can disable cookies, and the browser will also limit the number of cookies. The maximum number of cookies that can be created by a browser is 300, and each cookie cannot exceed 4KB. The total number of cookies that can be set by each WEB site cannot exceed 20.
??4. Cookies are stored on the client side. If the user disables cookies, your cookies will naturally have no effect! Therefore, avoid over-reliance on cookies and think about solutions if cookies are disabled, just in case

The above introduces PHP session tracking one (41), including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template