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

PHP session tracking one (41)

Aug 08, 2016 am 09:23 AM
cookie setcookie

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.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to Fix Roblox 403 Forbidden Error on Google Chrome How to Fix Roblox 403 Forbidden Error on Google Chrome May 19, 2023 pm 01:49 PM

Many Windows users have recently encountered an unusual error called Roblox403 Forbidden Error while trying to access website URLs in Google Chrome browser. Even after restarting the Chrome app multiple times, they were unable to do anything. There could be several potential causes for this error, some of which we've outlined and listed below. Browsing history and other cache of Chrome and corrupted data Unstable internet connection Incorrect website URLs Extensions installed from third-party sources After considering all the above aspects, we have come up with some fixes that can help users resolve this issue. If you encounter the same problem, check out the solutions in this article. Fix 1

Where are the cookies on your computer? Where are the cookies on your computer? Dec 22, 2023 pm 03:46 PM

Cookies on your computer are stored in specific locations on your browser, depending on the browser and operating system used: 1. Google Chrome, stored in C:\Users\YourUsername\AppData\Local\Google\Chrome\User Data\Default \Cookies etc.

Where are cookies stored? Where are cookies stored? Dec 20, 2023 pm 03:07 PM

Cookies are usually stored in the cookie folder of the browser. Cookie files in the browser are usually stored in binary or SQLite format. If you open the cookie file directly, you may see some garbled or unreadable content, so it is best to use Use the cookie management interface provided by your browser to view and manage cookies.

Where are the mobile cookies? Where are the mobile cookies? Dec 22, 2023 pm 03:40 PM

Cookies on the mobile phone are stored in the browser application of the mobile device: 1. On iOS devices, Cookies are stored in Settings -> Safari -> Advanced -> Website Data of the Safari browser; 2. On Android devices, Cookies Stored in Settings -> Site settings -> Cookies of Chrome browser, etc.

How cookies work How cookies work Sep 20, 2023 pm 05:57 PM

The working principle of cookies involves the server sending cookies, the browser storing cookies, and the browser processing and storing cookies. Detailed introduction: 1. The server sends a cookie, and the server sends an HTTP response header containing the cookie to the browser. This cookie contains some information, such as the user's identity authentication, preferences, or shopping cart contents. After the browser receives this cookie, it will be stored on the user's computer; 2. The browser stores cookies, etc.

Detailed explanation of where browser cookies are stored Detailed explanation of where browser cookies are stored Jan 19, 2024 am 09:15 AM

With the popularity of the Internet, we use browsers to surf the Internet have become a way of life. In the daily use of browsers, we often encounter situations where we need to enter account passwords, such as online shopping, social networking, emails, etc. This information needs to be recorded by the browser so that it does not need to be entered again the next time you visit. This is when cookies come in handy. What are cookies? Cookie refers to a small data file sent by the server to the user's browser and stored locally. It contains user behavior of some websites.

Does clearing cookies have any impact? Does clearing cookies have any impact? Sep 20, 2023 pm 06:01 PM

The effects of clearing cookies include resetting personalization settings and preferences, affecting ad experience, and destroying login status and password remembering functions. Detailed introduction: 1. Reset personalized settings and preferences. If cookies are cleared, the shopping cart will be reset to empty and products need to be re-added. Clearing cookies will also cause the login status on social media platforms to be lost, requiring re-adding. Enter your username and password; 2. It affects the advertising experience. If cookies are cleared, the website will not be able to understand our interests and preferences, and will display irrelevant ads, etc.

What are the dangers of cookie leakage? What are the dangers of cookie leakage? Sep 20, 2023 pm 05:53 PM

The dangers of cookie leakage include theft of personal identity information, tracking of personal online behavior, and account theft. Detailed introduction: 1. Personal identity information is stolen, such as name, email address, phone number, etc. This information may be used by criminals to carry out identity theft, fraud and other illegal activities; 2. Personal online behavior is tracked and analyzed through cookies With the data in the account, criminals can learn about the user's browsing history, shopping preferences, hobbies, etc.; 3. The account is stolen, bypassing login verification, directly accessing the user's account, etc.

See all articles