日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

PHP使用指南,PHP使用指南-cookies部分

發(fā)布時間:2025/3/15 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PHP使用指南,PHP使用指南-cookies部分 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

php使用指南-cookies部分

在這課教程我們將學(xué)習(xí)怎樣利用 PHP 處理cookies,我將試著使事情盡可能簡單地去解釋cookies的一些實際應(yīng)用。

什么是cookies及作用?

cookies是由web服務(wù)器產(chǎn)生的并且存在客戶端的一些信息。它嵌在html信息中,由服務(wù)器端指定,在客戶端及服務(wù)器端間傳遞信息

。它通常用來:用戶網(wǎng)頁個性化,計數(shù)器,儲存被瀏覽站點的信息等。

cookies和php

在 PHP中用cookies是相當(dāng)容易的。可以使用setcookie函數(shù)設(shè)置一個cookie。cookie是 HTTP標頭的一部分, 因此設(shè)置cookie功能必須在任何內(nèi)容送到瀏覽器之前。這種限制與header()函數(shù)一樣。任何從客戶端傳來的cookie將自動地轉(zhuǎn)化成一個PHP變量。PHP取得信息頭并分析, 提取cookie名并變成變量。因此,如果你設(shè)置cookie如setcookie("mycookie","wang");php將自動產(chǎn)生一個名為$mycookie,值為"wang"的變量.

先讓我們復(fù)習(xí)一下setcookie函數(shù)語法:

setcookie(string CookieName, string CookieValue, int CookieExpireTime, path, domain, int secure);

PATH:表示web服務(wù)器上的目錄,默認為被調(diào)用頁面所在目錄

DOMAIN:cookie可以使用的域名,默認為被調(diào)用頁面的域名。這個域名必須包含兩個".",所以如果你指定你的頂級域名,你必須用".mydomain.com"

SECURE:如果設(shè)為"1",表示cookie只能被用戶的瀏覽器認為是安全的服務(wù)器所記住

應(yīng)用:

對于一個需要注冊的站點,將自動識別用戶的身份,并發(fā)送給它信息,如果是陌生人,將告訴他請先注冊。我們按下面給出的信息創(chuàng)建一個小型數(shù) 據(jù)庫:名字(first name),姓(last name),email地址(email address),計數(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)在有了符合要求的表,我們可以建一個php頁面對照數(shù)據(jù)庫檢查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"

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服務(wù)器或unix服務(wù)器,你應(yīng)用下面語句

mysql_connect ("server","username","passWord") or die ("Problem connecting to DataBase");

我們想檢查是否一個被指定名字的cookie在html頭部分傳送,記住,php能轉(zhuǎn)換可識別的cookie為相應(yīng)的變量,所以我們能檢查一個名為"Example" 的變量:

if (isset($Example)) { //Begin instructions for existing Cookie

...

} else {

...

}

如果這個cookie存在,我們將計數(shù)器加一,并打印用戶信息,如果這個cookie不存在,我們建議用戶先注冊

如果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"

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函數(shù)分成不同的變量,增加計數(shù)器,并設(shè)一新cookie.接著用html語句輸出用戶信息。最后,用新的計數(shù)器值更新數(shù)據(jù)庫。

如果這個cookie不存,下面的程序?qū)⒈粓?zhí)行:

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:maxlength=20>

Last Name:maxlength=40>

email addrress:maxlength=40>

在所有的信息被提交后調(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 "

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"; }

?>

如果所有信息填好,將執(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 "

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.

";

這段程序做了幾件工作:它檢查數(shù)據(jù)庫是否有這樣一個用戶(如果沒有,也就是說,這個cookie已被刪除),如果有,它指定舊的信息,并用當(dāng)前的信息建一新的cookie,如果同一用戶沒有數(shù)據(jù)庫登錄,新建一數(shù)據(jù)庫登錄,并建一新的cookie.

首先,我們從數(shù)據(jù)庫中取回用戶登錄詳細資料

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)在檢查是否有一計數(shù)器為這用戶,利用isset()函數(shù)

if (isset($count)) {

...

} else {

...

}

計數(shù)器增加并新建一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";

如果沒有一用戶計數(shù)器,在mysql中加一記錄,并設(shè)一cookie

注意:在任何時候,setcookie放在輸送任何資料到瀏覽器之前,否則得到錯誤信息

以上就介紹了PHP使用指南-cookies部分,包括了方面的內(nèi)容,希望對PHP教程有興趣的朋友有所幫助。

本文原創(chuàng)發(fā)布php中文網(wǎng),轉(zhuǎn)載請注明出處,感謝您的尊重!

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的PHP使用指南,PHP使用指南-cookies部分的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。