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