国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

PHP用戶指南-cookies部分

2024-05-04 23:00:54
字體:
來源:轉載
供稿:網友
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]
,歡迎訪問網頁設計愛好者web開發。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 高青县| 盐山县| 平邑县| 汕尾市| 南安市| 梁河县| 西昌市| 通城县| 融水| 图木舒克市| 九江县| 卓尼县| 关岭| 芷江| 礼泉县| 积石山| 卓尼县| 牡丹江市| 孝昌县| 房产| 砚山县| 句容市| 嘉黎县| 万安县| 波密县| 佛教| 庄河市| 民权县| 贵阳市| 白朗县| 上饶市| 榆树市| 大连市| 高雄县| 曲水县| 新泰市| 夹江县| 高平市| 姜堰市| 龙海市| 安吉县|