侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

PHP用户指南-cookies部分

php  /  管理员 发布于 7年前   282

PHP用户指南-cookies部分

在这课教程我们将学习怎样利用 PHP 处理cookies,我将试着使事情尽可能简单地去解释cookies的一些实际应用。

什么是cookies及作用?  
cookies是由web服务器产生的并且存在客户端的一些信息。它嵌在html信息中,由服务器端指定,在客户端及服务器端间传递信息
。它通常用来:用户网页个性化,计数器,储存被浏览站点的信息等。

cookies和php
在 PHP中用cookies是相当容易的。可以使用setcookie函数设置一个cookie。cookie是 HTTP标头的一部分, 因此设置cookie功能必须在任何内容送到浏览器之前。这种限制与header()函数一样。任何从客户端传来的cookie将自动地转化成一个PHP变量。PHP取得信息头并分析, 提取cookie名并变成变量。因此,如果你设置cookie如setcookie("mycookie","wang");php将自动产生一个名为$mycookie,值为"wang"的变量.

先让我们复习一下setcookie函数语法:
setcookie(string CookieName, string CookieValue, int CookieExpireTime, path, domain, int secure);
PATH:表示web服务器上的目录,默认为被调用页面所在目录
DOMAIN:cookie可以使用的域名,默认为被调用页面的域名。这个域名必须包含两个".",所以如果你指定你的顶级域名,你必须用".mydomain.com"
SECURE:如果设为"1",表示cookie只能被用户的浏览器认为是安全的服务器所记住

应用:
对于一个需要注册的站点,将自动识别用户的身份,并发送给它信息,如果是陌生人,将告诉他请先注册。我们按下面给出的信息创建一个小型数 据库:名字(first name),姓(last name),email地址(email address),计数器(visit counter).
按下面步骤建表:

mysql> create database users;  
Query OK, 1 row affected (0.06 sec)  

mysql> use users;  
Database changed  

mysql> create table info (FirstName varchar(20), LastName varchar(40),  
email varchar(40), count varchar(3));  
Query OK, 0 rows affected (0.05 sec)

好,现在有了符合要求的表,我们可以建一个php页面对照数据库检查cookies.

########################index.php##################################
<? if (isset($Example)) { //Begin instructions for existing Cookie  
$info = explode("&", $Example);  
$FirstName=$info[0];  
$LastName=$info[1];  
$email=$info[2];  
$count=$info[3];  
$count++;  

$CookieString=$FirstName.'&'.$LastName.'&'.$email.'&'.$count;  
SetCookie ("Example",$CookieString, time()+3600); //设一新的cookie  

echo" <html>  
<title>wang example</title>  
</head>  
<body>  
<p>Hello $FirstName $LastName, this is your visit number: $count</p>  
<p>Your email address is: $email</p>  
<body>  
<html>";  

mysql_connect() or die ("Problem connecting to DataBase"); //update DB  
$query = "update info set count=$count where FirstName='$FirstName' and  
LastName='$LastName' and email='$email'";  
$result = mysql_db_query("users", $query) or die ("Problems .... ");  

} //End Existing cookie instructions  

else { //Begin inctructions for no Cookie  
echo "<html>  
<head>  
<Title>Rafi's Cookie example</title>  
</head>  
<body>  
<a href="reg.php">Click Here for Site Registration</a>  
</body>  
</html>";  
} //End No Cookie instructions  
?>

注意:如果你用的是一个远程mysql服务器或unix服务器,你应用下面语句
mysql_connect ("server","username","password") or die ("Problem connecting to DataBase");  

我们想检查是否一个被指定名字的cookie在html头部分传送,记住,php能转换可识别的cookie为相应的变量,所以我们能检查一个名为"Example" 的变量:
<? if (isset($Example)) { //Begin instructions for existing Cookie  
...  
} else {  
...  
}
如果这个cookie存在,我们将计数器加一,并打印用户信息,如果这个cookie不存在,我们建议用户先注册
如果cookie存在,我们执行下面步骤:
<? if (isset($Example)) { //Begin instructions for existing Cookie  
$info = explode("&", $Example); //split the string to variables  
$FirstName=$info[0];  
$LastName=$info[1];  
$email=$info[2];  
$count=$info[3];  
$count++;  

$CookieString=$FirstName.'&'.$LastName.'&'.$email.'&'.$count;  
SetCookie ("Example",$CookieString, time()+3600); //setting a new cookie  

echo" <html>  
<title>wang example</title>  
</head>  
<body>  
<p>Hello $FirstName $LastName, this is your visit number: $count</p>  
<p>Your email address is: $email</p>  
<body>  
<html>";  

mysql_connect() or die ("Problem connecting to DataBase"); //update DB  
$query = "update info set count=$count where FirstName='$FirstName' and  
LastName='$LastName' and email='$email'";  
$result = mysql_db_query("users", $query) or die ("Problems .... ");  

} //End Existing cookie instructions
上面的程序有3个主要部分:首先取得cookie值,用explode函数分成不同的变量,增加计数器,并设一新cookie.接着用html语句输出用户信息。最后,用新的计数器值更新数据库。
如果这个cookie不存,下面的程序将被执行:

else { //Begin inctructions for no Cookie  
echo "<html>  
<head>  
<Title>Rafi's Cookie example</title>  
</head>  
<body>  
<a href="reg.php">Click Here for Site Registration</a>  
</body>  
</html>";  
} //End No Cookie instructions  

下面reg.php简单列出到注册页面的链接
#############################reg.php#############################

   
<html>  
<head><title>Registering the Site</title>  
</head>  

<body bgcolor=#ffffff>  
<h1>Registering the site</h1>  

<form method="post" action="reg1.php">  
<table width=90% align=center>  
<tr><td>User Name:</td><td><input type=text name='FirstName' size=20  
maxlength=20></td></tr>  
<tr><td>Last Name:</td><td><input type=text name='LastName' size=40  
maxlength=40></td></tr>  
<tr><td>email addrress:</td><td><input type=text name='email' size=40  
maxlength=40></td></tr>  
<tr><td></td><td><input type=submit value="Click to Register"></td></tr>  
</table>  
</form>  
</body>  
</html>  


在所有的信息被提交后调用另一php文件分析这些信息
##############################reg1.php####################################
<?  
if ($FirstName and $LastName and $email)  
{  
mysql_connect() or die ("Problem connecting to DataBase");  
$query="select * from info where FirstName='$FirstName' and  
LastName='$LastName' and email='$email'";  
$result = mysql_db_query("users", $query);  

$r=mysql_fetch_array($result);  
$count=$r["count"];  

if (isset($count)) {  
$CookieString=$FirstName.'&'.$LastName.'&'.$email.'&'.$count;  
SetCookie ("Example",$CookieString, time()+3600);  
echo "<p>user $FirstName $LastName already exists. Using the existing  
info.</p>";  
echo "<p><a href="index.php">Back to Main Page</a>";  
} else {  
$count = '1';  
$query = "insert into info values  
('$FirstName','$LastName','$email','$count')";  
$result = mysql_db_query("users", $query);  
$CookieString=$FirstName.'&'.$LastName.'&'.$email.'&'.$count;  
SetCookie ("Example",$CookieString, time()+3600);  
echo "Thank you for registering.<br>";  
}  

} else { echo "Sorry, some information is missing. Please go back and add all  
the information"; }  
?>  
首先检查所有的信息是否按要求填写,如果没有,返回重新输入
<?  
if ($FirstName and $LastName and $email)  
{  
...  
} else { echo "Sorry, some information is missing. Please go back and add all  
the information"; }  
?>
如果所有信息填好,将执行下面:

mysql_connect() or die ("Problem connecting to DataBase");  
$query="select * from info where FirstName='$FirstName' and  
LastName='$LastName' and email='$email'";  
$result = mysql_db_query("users", $query);  

$r=mysql_fetch_array($result);  
$count=$r["count"];  

if (isset($count)) {  
$count++;  
$CookieString=$FirstName.'&'.$LastName.'&'.$email.'&'.$count;  
SetCookie ("Example",$CookieString, time()+3600);  
echo "<p>user $FirstName $LastName already exists. Using the existing  
info.</p>";  
echo "<p><a href="index.php">Back to Main Page</a>";  
} else {  
$count = '1'; //new visitor - set counter to 1.  
$query = "insert into info values  
('$FirstName','$LastName','$email','$count')";  
$result = mysql_db_query("users", $query);  
$CookieString=$FirstName.'&'.$LastName.'&'.$email.'&'.$count;  
SetCookie ("Example",$CookieString, time()+3600);  
echo "Thank you for registering.<br>";  
这段程序做了几件工作:它检查数据库是否有这样一个用户(如果没有,也就是说,这个cookie已被删除),如果有,它指定旧的信息,并用当前的信息建一新的cookie,如果同一用户没有数据库登录,新建一数据库登录,并建一新的cookie.
首先,我们从数据库中取回用户登录详细资料
mysql_connect() or die ("Problem connecting to DataBase");  
$query="select * from info where FirstName='$FirstName' and  
LastName='$LastName' and email='$email'";  
$result = mysql_db_query("users", $query);  
$r=mysql_fetch_array($result);  
$count=$r["count"];

现在检查是否有一计数器为这用户,利用isset()函数

if (isset($count)) {  
...  
} else {  
...  
}  
计数器增加并新建一cookie
$count++; //increase counter  
$CookieString=$FirstName.'&'.$LastName.'&'.$email.'&'.$count;  
SetCookie ("Example",$CookieString, time()+3600);  
echo "<p>user $FirstName $LastName already exists. Using the existing info.</p>";  
echo "<p><a href="index.php">Back to Main Page</a>";
如果没有一用户计数器,在mysql中加一记录,并设一cookie
注意:在任何时候,setcookie放在输送任何资料到浏览器之前,否则得到错误信息

#####################################################
---advance翻译,有不恰之处,请[email protected]


  • 上一条:
    PHP新手上路(十)
    下一条:
    PHP 增加了对 .ZIP 文件的读取功能
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 用Time Warden监控PHP中的代码处理时间(0个评论)
    • 在PHP中使用array_pop + yield实现读取超大型目录功能示例(0个评论)
    • Property Hooks RFC在PHP 8.4中越来越接近现实(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-11
    • 2017-12
    • 2018-01
    • 2018-02
    • 2018-03
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-09
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    • 2024-09
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客