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放在输送任何资料到浏览器之前,否则得到错误信息