|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
用C语言重新编写,包括可以访问数据库。他将这些程序和一些表单直译器整合起来,称为 PHP/FI。PHP/FI 可以和数据库连接,产生简单的动态网页程序。 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翻译,有不恰的地方,请qianjinok@china.com-------
不可能吃饭的时候咬了自己一下舌头就从此不吃饭了不是?放下畏惧,继续努力,咱们是来征服它的,而不是被它征服的,振奋起来吧同志。 |
|