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
wang example
hello $firstname $lastname, this is your visit number: $count
your email address is: $email
;
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
rafi's cookie example
click here for site registration
;
} //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
wang example
hello $firstname $lastname, this is your visit number: $count
your email address is: $email
;
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
rafi's cookie example
click here for site registration
;
} //end no cookie instructions
下面reg.php简单列出到注册页面的链接
#############################reg.php#############################
registering the site
registering the site
user name:
last name:
email addrress:
在所有的信息被提交后调用另一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 user $firstname $lastname already exists. using the existing
info.
;
echo back to main page;
} 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.
;
}
} 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
user $firstname $lastname already exists. using the existing
info.
;
echo back to main page;
} 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.
;
这段程序做了几件工作:它检查数据库是否有这样一个用户(如果没有,也就是说,这个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
user $firstname $lastname already exists. using the existing info.
;
echo back to main page;
如果没有一用户计数器,在mysql中加一记录,并设一cookie
注意:在任何时候,setcookie放在输送任何资料到浏览器之前,否则得到错误信息
以上就介绍了php使用指南-cookies部分,包括了方面的内容,希望对php教程有兴趣的朋友有所帮助。