PHP用戶向?qū)?cookies局部
發(fā)表時(shí)間:2024-02-12 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]PHP用戶指南-cookies部分 在這課教程我們將學(xué)習(xí)怎樣利用 PHP 處理cookies,我將試著使事情盡可能簡(jiǎn)單地去解釋cookies的一些實(shí)際應(yīng)用。 什么是cookies及作用? cookies是由web服務(wù)器產(chǎn)生的并且存在客戶端的一些信息。它嵌在html信息中,由服務(wù)器端指定,在客戶端...
PHP用戶指南-cookies部分
在這課教程我們將學(xué)習(xí)怎樣利用 PHP 處理cookies,我將試著使事情盡可能簡(jiǎn)單地去解釋cookies的一些實(shí)際應(yīng)用。
什么是cookies及作用?
cookies是由web服務(wù)器產(chǎn)生的并且存在客戶端的一些信息。它嵌在html信息中,由服務(wù)器端指定,在客戶端及服務(wù)器端間傳遞信息
。它通常用來(lái):用戶網(wǎng)頁(yè)個(gè)性化,計(jì)數(shù)器,儲(chǔ)存被瀏覽站點(diǎn)的信息等。
cookies和php
在 PHP中用cookies是相當(dāng)容易的?梢允褂胹etcookie函數(shù)設(shè)置一個(gè)cookie。cookie是 HTTP標(biāo)頭的一部分, 因此設(shè)置cookie功能必須在任何內(nèi)容送到瀏覽器之前。這種限制與header()函數(shù)一樣。任何從客戶端傳來(lái)的cookie將自動(dòng)地轉(zhuǎn)化成一個(gè)PHP變量。PHP取得信息頭并分析, 提取cookie名并變成變量。因此,如果你設(shè)置cookie如setcookie("mycookie","wang");php將自動(dòng)產(chǎn)生一個(gè)名為$mycookie,值為"wang"的變量.
先讓我們復(fù)習(xí)一下setcookie函數(shù)語(yǔ)法:
setcookie(string CookieName, string CookieValue, int CookieExpireTime, path, domain, int secure);
PATH:表示web服務(wù)器上的目錄,默認(rèn)為被調(diào)用頁(yè)面所在目錄
DOMAIN:cookie可以使用的域名,默認(rèn)為被調(diào)用頁(yè)面的域名。這個(gè)域名必須包含兩個(gè)".",所以如果你指定你的頂級(jí)域名,你必須用".mydomain.com"
SECURE:如果設(shè)為"1",表示cookie只能被用戶的瀏覽器認(rèn)為是安全的服務(wù)器所記住
應(yīng)用:
對(duì)于一個(gè)需要注冊(cè)的站點(diǎn),將自動(dòng)識(shí)別用戶的身份,并發(fā)送給它信息,如果是陌生人,將告訴他請(qǐng)先注冊(cè)。我們按下面給出的信息創(chuàng)建一個(gè)小型數(shù) 據(jù)庫(kù):名字(first name),姓(last name),email地址(email address),計(jì)數(shù)器(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)
好,現(xiàn)在有了符合要求的表,我們可以建一個(gè)php頁(yè)面對(duì)照數(shù)據(jù)庫(kù)檢查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); //設(shè)一新的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
?>
注意:如果你用的是一個(gè)遠(yuǎn)程mysql服務(wù)器或unix服務(wù)器,你應(yīng)用下面語(yǔ)句
mysql_connect ("server","username","password") or die ("Problem connecting to DataBase");
我們想檢查是否一個(gè)被指定名字的cookie在html頭部分傳送,記住,php能轉(zhuǎn)換可識(shí)別的cookie為相應(yīng)的變量,所以我們能檢查一個(gè)名為"Example" 的變量:
<? if (isset($Example)) { //Begin instructions for existing Cookie
...
} else {
...
}
如果這個(gè)cookie存在,我們將計(jì)數(shù)器加一,并打印用戶信息,如果這個(gè)cookie不存在,我們建議用戶先注冊(cè)
如果cookie存在,我們執(zhí)行下面步驟:
<? 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個(gè)主要部分:首先取得cookie值,用explode函數(shù)分成不同的變量,增加計(jì)數(shù)器,并設(shè)一新cookie.接著用html語(yǔ)句輸出用戶信息。最后,用新的計(jì)數(shù)器值更新數(shù)據(jù)庫(kù)。
如果這個(gè)cookie不存,下面的程序?qū)⒈粓?zhí)行:
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簡(jiǎn)單列出到注冊(cè)頁(yè)面的鏈接
#############################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>
在所有的信息被提交后調(diào)用另一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"; }
?>
首先檢查所有的信息是否按要求填寫(xiě),如果沒(méi)有,返回重新輸入
<?
if ($FirstName and $LastName and $email)
{
...
} else { echo "Sorry, some information is missing. Please go back and add all
the information"; }
?>
如果所有信息填好,將執(zhí)行下面:
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>";
這段程序做了幾件工作:它檢查數(shù)據(jù)庫(kù)是否有這樣一個(gè)用戶(如果沒(méi)有,也就是說(shuō),這個(gè)cookie已被刪除),如果有,它指定舊的信息,并用當(dāng)前的信息建一新的cookie,如果同一用戶沒(méi)有數(shù)據(jù)庫(kù)登錄,新建一數(shù)據(jù)庫(kù)登錄,并建一新的cookie.
首先,我們從數(shù)據(jù)庫(kù)中取回用戶登錄詳細(xì)資料
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"];
現(xiàn)在檢查是否有一計(jì)數(shù)器為這用戶,利用isset()函數(shù)
if (isset($count)) {
...
} else {
...
}
計(jì)數(shù)器增加并新建一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>";
如果沒(méi)有一用戶計(jì)數(shù)器,在mysql中加一記錄,并設(shè)一cookie
注意:在任何時(shí)候,setcookie放在輸送任何資料到瀏覽器之前,否則得到錯(cuò)誤信息
#####################################################
---advance翻譯,有不恰之處,請(qǐng)qianjinok@china.com-------