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 .... ");
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
在所有的資訊被提交後調用另一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);
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);
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放在輸送任何資料到瀏覽器之前,否則得到錯誤資訊