Home php教程 php手册 用cookies来跟踪识别用户

用cookies来跟踪识别用户

Jun 13, 2016 pm 12:42 PM
cookies ie content exist us yes Browser use user use identify track

让我们来看看保存在浏览器中的内容。如果你用的是IE5,在windows目录下有一个cookies的目录,里面有很多文本文件,文件名都是类似于wudong@15seconds[1].txt这样的,这就是浏览器用来保存值的cookies了。在以前的IE版本中,cookies的内容是可以察看的,但现在内容已经被编码了。在浏览器得到一个Web页面之前,它会先看这个页面的域名,是否在cookie中存在,如果有相比配的,浏览器会先把匹配的cookie传送到服务器,然后才接受处理服务器传送过来的页面。
  
  先举个cookies应用的例子:当我连接到Amazon.com时,浏览器在接受第一个页面之前会把它以前设置的cookies的内容传送给Amazon。然后Amazon.com对传送过来的内容加以检查,看看在数据库中有没有相关资料,在匹配之后,在为我建立一个定制的页面传送到过来。
  为cookies赋值
  
  必须在服务器传送任何内容给客户浏览器之前为Cookies赋值。要做到这一点,cookies的设置就必须放在

标签内:
    setcookie("CookieID",$USERID);
  ?>
  
  
  
  
  setcookie函数一共有六个参数,用逗号来分隔:
  
  cookie的名称,是一个字符串,例如:"CookieID"。其间不允许有冒号,逗号和空格。这个参数是必须的,而其它的所有参数都是可选的。如果只有这一个参数被给出,那么这个cookie将被删除。
  
  cookie的值,通常是一个字符串变量,例如:$USERID。也可以为它赋一个??来略过值的设置。
  
  cookie失效的时间。如果被省略(或者被赋值为零),cookie将在这个对话期(session)结束后失效。这个参数可以是一个绝对的时间,用DD-Mon-YYHH:MM:SS来表示,比如:"24-Nov-9908:26:00"。而更常用的是设置一个相对时间。这是通过time()函数或者mktime函数来实现的。比如time()+3600将使得cookie在一个小时后失效。
  
  一个路径,用来匹配cookie的。当在一个服务器上有多个同名的cookie的设置,为避免混淆,就要用到这个参数了。使用"/"路径的和省略这个参数的效果是一样的。要注意的是Netscape的cookie定义是把域名放在路径的前面的,而PHP则与之相反。
  
  服务器的域名,也是用来匹配cookie的。要注意的是:在服务器的域名前必须放上一个点(.)。例如:".friendshipcenter.com"。因为除非有两个以上的点存在,否者这个参数是不能被接受的。
  
  cookie的安全级,是一个整数。1表示这个cookie只能通过“安全”的网络来传送。0或者省略则表示任何类型的网络都可以。
  
  Cookies和变量
  
  当PHP脚本从客户浏览器提取了一个cookie后,它将自动的把它转换成一个变量。例如:一个名为CookieID的cookie将变成变量$CookieID.
  
  Cookies的内容被报存在HTTP_COOKIE_VARS数组中,你还可以通过这个数组和cookie的名称来存取指定的cookie值:
  
  print$HTTP_COOKIE_VARS[CookieID];
  
  记住每一个用户
  
  回过头在来看看上面的submitform.php3文件,它的作用是把客户的姓名添加到数据库中,现在我想为它添加一些东西。我想为每个用户都分配一个唯一的用户标志,然后把这个标志放在Cookies中,这样每当用户访问我的网站的时候,通过cookie和其中的用户标志,我就能够知道他是谁了。
  
  MySQL能够被设置成为每一个新的纪录自动的分配一个数字,这个数字从1开始,以后每次自动加1。用一行SQL语句,你就可以轻松的为数据表添加这样的一个字段,我把它叫做USERID:
  ALTERTABLEdbname
  ADDCOLUMN
  USERIDINT(11)NOTNULL
  PRIMARYKEYAUTO_INCREMENT;
  
  对这个字段我们作了一些特别的设置。首先,通过“INT(11)”定义它的类型为11位的整数;然后用“NOTNULL”关键字让这个字段的值不能为NULL;再用“PRIMARYKEY”把它设置为索引字段,这样搜索起来就会更快;最后,“AUTO_INCREMENT”定义它为自动增一的字段。
  
  当把用户的姓名插入到数据库后,就应该在他们的浏览器上设置cookie了。这时利用的就是刚才我们谈到的USERID字段的值:
  
    mysql_connect(localhost,username,password);
  mysql_select_db(dbname);
  mysql_query("INSERTINTOtablename(first_name,last_name)
  VALUES('$first_name','$last_name')
  ");
  setcookie("CookieID",
  mysql_insert_id(),
  time()+94608000,
  "/");/*三年后cookie才会失效*/
  ?>
  
  PHP函数mysql_insert_id()返回在最后一次执行了INSERT查询后,由AUTO_INCREMENT定义的字段的值。这样,只要你不清除掉浏览器的Cookies,网站就会永远“记住”你了
  
  读取cookie
  
  我们来写一个像Amazon.com所作的那样的脚本。首先,PHP脚本会先检查客户浏览器是否发送了cookie过来,如果是那样的话,用户的姓名就会被显示出来。如果没找到cookie的话,就显示一个表单,让客户登记他们的姓名,然后把他添加到数据库中,并在客户浏览其中设置好cookie。
  
  首先,先来显示cookie的内容:
    print$CookieID;
  ?>
  然后,就可以把名字显示出来了:
    mysql_connect(localhost,username,password);
  mysql_select_db(dbname);
  $selectresult=mysql_query("SELECT*FROMtablename
  WHEREUSERID='$CookieID'
  ");
  $row=mysql_fetch_array($selectresult);
  echo"欢迎你的光临",$row[first_name],"!";
  ?>
  就是这样的了。我在其中没有作判断,交给你自己来完成好了  


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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Coinbase Exchange Login Port 2025 Coinbase Exchange Login Port 2025 Mar 21, 2025 pm 05:51 PM

Coinbase Security Login Guide: How to Avoid Phishing Sites and Scams? Phishing and scams are becoming increasingly rampant, and it is crucial to securely access the Coinbase official login portal. This article provides practical guides to help users securely find and use the latest official login portal of Coinbase to protect the security of digital assets. We will cover how to identify phishing sites, and how to log in securely through official websites, mobile apps or trusted third-party platforms, and provide suggestions for enhancing account security, such as using a strong password and enabling two-factor verification. To avoid asset losses due to incorrect login, be sure to read this article carefully!

The latest registration portal for Ouyi official website The latest registration portal for Ouyi official website Mar 21, 2025 pm 05:54 PM

As the world's leading digital asset trading platform, Ouyi OKX attracts many investors with its rich trading products, strong security guarantees and convenient user experience. However, the risks of network security are becoming increasingly severe, and how to safely register the official Ouyi OKX account is crucial. This article will provide the latest registration portal for Ouyi OKX official website, and explain in detail the steps and precautions for safe registration, including how to identify the official website, set a strong password, enable two-factor verification, etc., to help you start your digital asset investment journey safely and conveniently. Please note that there are risks in digital asset investment, please make cautious decisions.

Ouyi Exchange app domestic download tutorial Ouyi Exchange app domestic download tutorial Mar 21, 2025 pm 05:42 PM

This article provides a detailed guide to safe download of Ouyi OKX App in China. Due to restrictions on domestic app stores, users are advised to download the App through the official website of Ouyi OKX, or use the QR code provided by the official website to scan and download. During the download process, be sure to verify the official website address, check the application permissions, perform a security scan after installation, and enable two-factor verification. During use, please abide by local laws and regulations, use a safe network environment, protect account security, be vigilant against fraud, and invest rationally. This article is for reference only and does not constitute investment advice. Digital asset transactions are at your own risk.

Binance Exchange app domestic download tutorial Binance Exchange app domestic download tutorial Mar 21, 2025 pm 05:45 PM

This article provides a safe and reliable Binance Exchange App download guide to help users solve the problem of downloading Binance App in the country. Due to restrictions on domestic application stores, the article recommends priority to downloading APK installation packages from Binance official website, and introduces three methods: official website download, third-party application store download, and friends sharing. At the same time, it emphasizes security precautions during the download process, such as verifying the official website address, checking application permissions, scanning with security software, etc. In addition, the article also reminds users to understand local laws and regulations, pay attention to network security, protect personal information, beware of fraud, rational investment, and secure transactions. At the end of the article, the article once again emphasized that downloading and using Binance App must comply with local laws and regulations, and at your own risk, and does not constitute any investment advice.

Log in to the latest official website of BitMEX exchange Log in to the latest official website of BitMEX exchange Mar 21, 2025 pm 06:06 PM

This article provides safe and reliable guides to help users access the latest official website of BitMEX exchange and improve transaction security. Due to regulatory and cybersecurity threats, it is crucial to identify the official BitMEX website and avoid phishing websites stealing account information and funds. The article introduces the search for official website portals through trusted cryptocurrency platforms, official social media, news media, and subscribes to official emails. It emphasizes the importance of checking domain names, using HTTPS connections, checking security certificates, and enabling two-factor verification and changing passwords regularly. Remember, cryptocurrency trading is high risk, please invest with caution.

Coinbase Exchange web version login portal Coinbase Exchange web version login portal Mar 21, 2025 pm 05:48 PM

The Coinbase exchange web version is popular for its convenience, but secure access is crucial. This article aims to guide users to log in to the official Coinbase web version safely and avoid phishing websites and hackers. We will explain in detail how to verify the official portal through search engines, trusted third-party platforms and official social media, and emphasize security measures such as checking the address bar security lock, enabling two-factor verification, avoiding public Wi-Fi, changing passwords regularly, and being alert to phishing emails to ensure the security of your digital assets. Correct access to the official Coinbase website is the first step to protecting your digital currency. This article will help you start your digital currency trading journey safely.

BitMEX Exchange's latest official website entrance BitMEX Exchange's latest official website entrance Mar 21, 2025 pm 06:03 PM

As a veteran cryptocurrency derivatives trading platform, the accuracy of its official website entrance is crucial. Due to rampant phishing websites, misent entry into fake websites can lead to account theft and loss of funds. This article aims to guide users to safely access the BitMEX official website, provide various methods such as trusted cryptocurrency information platforms (such as CoinMarketCap, CoinGecko), official social media, verification of existing addresses and official support channels, and emphasizes the use of security measures such as two-factor verification, regular password changes and use of security software to help users effectively avoid risks and ensure account security.

How to download Binance in the country How to download Binance in the country Mar 21, 2025 pm 05:33 PM

This article provides a guide to safely downloading the Binance App in China. Due to restrictions on domestic app stores, it is difficult to download directly. It is recommended to download the APK installation package through the Binance official website or scan the QR code to download the App. Be sure to carefully check the official domain name, check the application permissions, perform a secure scan after installation, and enable two-factor verification (2FA). Please be sure to understand and abide by local laws and regulations before downloading and using it. The risk of digital asset transactions is high, so please operate with caution. This article is for reference only and does not constitute investment advice. All risks are borne by the user. Keywords: Binance, Binance, Download, App, Domestic, Security, Tutorial, Digital Currency, Cryptocurrency

See all articles