用户注册信息,管理员核对信息审核通过后,可实现注册的用户名和密码的成功登陆,利用session和cookie获取用户信息并且不能跳过登录页面直接进入主页面
1.Session
存储在服务器
可以存储任何内容
有默认过期时间:大约15分钟
相对比较安全
用法:
1.必须在php页面开始写:session_start();开启session
2.写Session: $_SESSION["uid"]=$uid;
3.读取Session:$_SESSION["uid"];
2.Cookie
存储在客户端
只能存储字符串
默认没有过期时间
用法:
1.设置Cookie:setcookie("name","value");
2.取值:$_COOKIE["name"];
在php里面写
目的:
获取用户信息
不能跳过登陆页面
zhuce.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script type="text/javascript" src="jquery-2.1.4.min.js"></script> </head> <body> <div style="background-color:#CCC; width:300px; padding-left:10px;"> <h1>注册页面</h1> <div>用户名:<input type="text" id="uid" /></div><br /> <div>密 码:<input type="text" id="pwd" /></div><br /> <div>姓 名:<input type="text" id="name" /></div><br /> <div>性 别:<input type="radio" checked="checked" name="sex" id="nan" value="true" />男 <input type="radio" name="sex" value="false" />女</div><br /> <div>生 日:<input type="text" id="birthday" /></div><br /> <div>工 号:<input type="text" id="code" /></div><br /> <div><input type="button" value="提交" id="btn" /> <input type="button" value="查看" onclick="window.open('main.php')" /></div><br /> </div> </body> <script type="text/javascript"> $(document).ready(function(e) { $("#btn").click(function(){ var uid = $("#uid").val(); var pwd = $("#pwd").val(); var name = $("#name").val(); var sex = $("#nan")[0].checked; var birthday = $("#birthday").val(); var code = $("#code").val(); $.ajax({ url:"zhucechuli.php", data:{uid:uid,pwd:pwd,name:name,sex:sex,birthday:birthday,code:code}, type:"POST", dataType:"TEXT", success: function(data){ if(data=="OK") { alert("注册成功!"); } else { alert("注册失败!"); } } }) }) }); </script> </html>
zhucechuli.php
<?php $uid=$_POST["uid"]; $pwd=$_POST["pwd"]; $name=$_POST["name"]; $sex=$_POST["sex"]; $birthday=$_POST["birthday"]; $code=$_POST["code"]; include("mydbda.php"); $db = new mydbda(); $sql="insert into users values('".$uid."','".$pwd."','".$name."',".$sex.",'".$birthday."','".$code."',false)"; $str = $db->Select($sql,"QT","mydb"); echo $str; ?>
main.php
<?php session_start(); //找session if(empty($_SESSION["uid"])) { header("Location:denglu.php");//定义不能跳转页面 } //找coolie //$_COOKIE["uid"] ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <h1>注册审核页面 </h1> <table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr> <td>用户名</td> <td>密码</td> <td>姓名</td> <td>性别</td> <td>生日</td> <td>工号</td> <td>状态</td> </tr> <?php include("mydbda.php"); $db=new mydbda(); $sql="select * from users"; $str=$db->Select($sql,"CX","mydb"); $hang=explode("|",$str); for($i=0;$i<count($hang);$i++) { $lie=explode("^",$hang[$i]); $sex=$lie[3]?"男":"女"; $zhuangtai=$lie[6]?"<input type='text' value='审核已通过' checked='checked'/>":"<a href='shenhechuli.php?uid={$lie[0]}'>审核</a>"; echo "<tr> <td>{$lie[0]}</td> <td>{$lie[1]}</td> <td>{$lie[2]}</td> <td>{$sex}</td> <td>{$lie[4]}</td> <td>{$lie[5]}</td> <td>{$zhuangtai}</td> </tr>"; } ?> </table> </body> </html>
shehechuli.php
<?php include("mydbda.php"); $uid=$_GET["uid"]; $db=new mydbda(); $sql="update users set isok=true where uid='".$uid."'"; $str=$db->Select($sql,"QT","mydb"); header("Location:main.php"); ?>
denglu.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <div style="width:300px; background-color:#CCC"> <h1>登陆页面</h1> <form action="dengluchuli.php" method="post"> <div>用户名:<input type="text" name="uid" /></div><br /> <div>密 码:<input type="text" name="pwd" /></div><br /> <div><input type="submit" value="登陆" /></div> </form></div> </body> </html>
dengluchuli.php
<?php session_start();//开启Session 写在php里 必须写在最上面 $uid = $_POST["uid"]; $pwd = $_POST["pwd"]; include("mydbda.php"); $db=new mydbda(); $sql="select count(*) from users where uid='".$uid."' and pwd='".$pwd."' and isok =true"; $str = $db->Select($sql,"CX","mydb"); if($str==1) { $_SESSION["uid"]=$uid;//存在服务器,任何页面都可以调用 //$_SESSION["name"]=array(1,2,3,4,5)session可以存储任何内容 //用cookie写 //setcookie("uid",$uid);//定义cookie 会在客户端生成cookie文件 header("Location:main.php"); } else { header("Location:denglu.php"); } ?>
<?php class mydbda { var $host = "localhost"; var $username = "root"; var $password = "123"; var $database = "mydb"; /** 功能:执行SQL语句,返回结果 参数:$sql:要执行的SQL语句 $type:SQL语句的类型,CX代表查询,QT代表其他 $data:要操作的数据库 返回值:如果是查询,返回结果集 如果是其他语句,执行成功返回OK,失败返回NO */ function Select($sql,$type,$data) { //1.造连接对象 $db = new mysqli($this->host,$this->username,$this->password,$data); //2.判断是否连接成功 if(mysqli_connect_error()) { echo "连接失败"; //退出整个程序 exit; } else { //4.执行SQL语句 $result = $db->query($sql); if($type == "CX") { $str = ""; while($row = $result->fetch_row()) { for($i=0;$i<count($row);$i++) { $str=$str.$row[$i]."^"; } $str = substr($str,0,strlen($str)-1); $str = $str."|"; } $str = substr($str,0,strlen($str)-1); return $str; } else { if($result) { return "OK"; } else { return "NO"; } } } } } ?> mydbda.php
php中文网学习专题:php session (包含图文、视频、案例)