ウェブサイトを構築する目的は、機能の完全性です。機能の 1 つは、ユーザーのログイン認証の実装です。以下に、
PHP Cookie ログイン検証コードの例を詳しく紹介します:
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span> ?php </span></span></li><li><span>@mysql_connect("localhost", "root","1981427")<br /> //选择数据库之前需要先连接数据库服务器 </span></li><li class="alt"><span>or die("数据库服务器连接失败"); </span></li><li><span>@mysql_select_db("test") //选择数据库mydb </span></li><li class="alt"><span>or die("数据库不存在或不可用"); </span></li><li><span>//获取用户输入 </span></li><li class="alt"><span>$</span><span class="attribute">username</span><span> = $_POST['username']; </span></li><li><span>$</span><span class="attribute">passcode</span><span> = $_POST['passcode']; </span></li><li class="alt"><span>$</span><span class="attribute">cookie</span><span> = $_POST['cookie']; </span></li><li><span>//执行SQL语句 </span></li><li class="alt"><span>$</span><span class="attribute">query</span><span> = @mysql_query("select username, <br />userflag from users " </span></li><li><span>."where </span><span class="attribute">username</span><span> = </span><span class="attribute-value">'$username'</span><span> and <br /></span><span class="attribute">passcode</span><span> = </span><span class="attribute-value">'$passcode'</span><span>") </span></li><li class="alt"><span>or die("SQL语句执行失败"); </span></li><li><span>//判断用户是否存在,密码是否正确 </span></li><li class="alt"><span>if($</span><span class="attribute">row</span><span> = </span><span class="attribute-value">mysql_fetch_array</span><span>($query)) </span></li><li><span>{ </span></li><li class="alt"><span>if($row['userflag'] == 1 or $row['userflag'] == 0) <br />//判断用户权限信息是否有效 </span></li><li><span>{ </span></li><li class="alt"><span>switch($cookie) //根据用户的选择设置cookie保存时间 </span></li><li><span>{ </span></li><li class="alt"><span>case 0: //保存Cookie为浏览器进程 </span></li><li><span>setcookie("username", $row['username']); </span></li><li class="alt"><span>break; </span></li><li><span>case 1: //保存1天 </span></li><li class="alt"><span>setcookie("username", $row['username'],<br /> time()+24*60*60); </span></li><li><span>break; </span></li><li class="alt"><span>case 2: //保存30天 </span></li><li><span>setcookie("username", $row['username'],<br /> time()+30*24*60*60); </span></li><li class="alt"><span>break; </span></li><li><span>case 3: //保存365天 </span></li><li class="alt"><span>setcookie("username", $row['username'], <br />time()+365*24*60*60); </span></li><li><span>break; </span></li><li class="alt"><span>} </span></li><li><span>header("location: main.php"); <br />//自动跳转到main.php </span></li><li class="alt"><span>} </span></li><li><span>else </span></li><li class="alt"><span>{ </span></li><li><span>echo "用户权限信息不正确"; </span></li><li class="alt"><span>} </span></li><li><span>} </span></li><li class="alt"><span>else </span></li><li><span>{ </span></li><li class="alt"><span>echo "用户名或密码错误"; </span></li><li><span>} </span></li><li class="alt"><span class="tag">?><span> </span> </li> </ol>PHP Cookie ログイン認証コード例: <span><pre class="brush:php;toolbar:false"><ol class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span> ?php </span></span></li><li><span>session_start(); </span></li><li class="alt"><span>if(isset($_COOKIE['username'])) </span></li><li><span>{ </span></li><li class="alt"><span>@mysql_connect("localhost", "root","1981427") </span></li><li class="alt"><span>//选择数据库之前需要先连接数据库服务器 </span></li><li><span>or die("数据库服务器连接失败"); </span></li><li class="alt"><span>@mysql_select_db("test") //选择数据库mydb </span></li><li><span>or die("数据库不存在或不可用"); </span></li><li class="alt"><span>//获取Session </span></li><li><span>$</span><span class="attribute">username</span><span> = $_COOKIE['username']; </span></li><li class="alt"><span>//执行SQL语句获得userflag的值 </span></li><li><span>$</span><span class="attribute">query</span><span> = @mysql_query("select <br />userflag from users " </span></li><li class="alt"><span>."where </span><span class="attribute">username</span><span> = </span><span class="attribute-value">'$username'</span><span>") </span></li><li><span>or die("SQL语句执行失败"); </span></li><li class="alt"><span>$</span><span class="attribute">row</span><span> = </span><span class="attribute-value">mysql_fetch_array</span><span>($query); </span></li><li><span>//获得用户权限信息 </span></li><li class="alt"><span>$</span><span class="attribute">flag</span><span> = $row['userflag']; </span></li><li><span>//根据userflag的值输出不同的欢迎信息 </span></li><li class="alt"><span>if($</span><span class="attribute">flag</span><span> == 1) </span></li><li><span>echo "欢迎管理员".$_SESSION['username']<br />."登录系统"; </span></li><li class="alt"><span>if($</span><span class="attribute">flag</span><span> == 0) </span></li><li><span>echo "欢迎用户".$_SESSION['username']<br />."登录系统"; </span></li><li class="alt"><span>echo "</span><span class="tag"><</span><span> </span><span class="tag-name">a</span><span> </span><span class="attribute">href</span><span>=</span><span class="attribute-value">"logout.php"</span><span> <br /></span><span class="attribute">href</span><span>=</span><span class="attribute-value">"logout.php"</span><span class="tag">></span><span>注销</span><span class="tag"><</span><span> /a</span><span class="tag">></span><span>"; </span></span></li> <li><span>} </span></li> <li class="alt"><span>else </span></li> <li><span>{ </span></li> <li class="alt"><span>echo "您没有权限访问本页面"; </span></li> <li><span>} </span></li> <li class="alt"> <span class="tag">?></span><span> </span> </li> <li> <span class="tag"><</span><span> ?php </span></li><li class="alt"><span>setcookie("username"); </span></li><li><span>echo "注销成功"; </span></li><li class="alt"><span class="tag">?></span><span> </span> </li> </ol>上記はタイムリーな PHP Cookie ログイン認証の実装方法です。