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放在輸送任何資料到瀏覽器之前,否則得到錯誤信息