实现效果:
用户注册信息,管理员核对信息审核通过后,可实现注册的用户名和密码的成功登陆,利用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"];
二,登录注册界面设计
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 > <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="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"]; 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> </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 > <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
1 <?php 2 session_start();//开启Session 写在php里 必须写在最上面 3 4 $uid = $_POST["uid"]; 5 $pwd = $_POST["pwd"]; 6 include("mydbda.php"); 7 $db=new mydbda(); 8 9 $sql="select count(*) from users where uid='".$uid."' and pwd='".$pwd."' and isok =true"; 10 11 $str = $db->Select($sql,"CX","mydb"); 12 if($str==1) 13 { 14 $_SESSION["uid"]=$uid;//存在服务器,任何页面都可以调用 15 //$_SESSION["name"]=array(1,2,3,4,5)session可以存储任何内容 16 //用cookie写 17 //setcookie("uid",$uid);//定义cookie 会在客户端生成cookie文件 18 19 header("Location:main.php"); 20 } 21 else 22 { 23 header("Location:denglu.php"); 24 } 25 26 27 ?>
三,PHP连接数据库测试
1 <?php 2 //设置字符集 3 header('Content-Type: text/html; charset=utf8'); 4 //连接数据库 5 $link = mysql_connect("localhost","root","123456"); 6 if(!$link){ 7 echo '数据库连接失败...'; 8 exit(-1); 9 }else{ 10 echo "数据库连接成功..."; 11 } 12 //选择一个数据库作为默认数据库 13 mysql_select_db('php_sjk'); 14 //执行sql插入语句 15 $sql = "INSERT INTO zh VALUES (NULL,'王杰','123456')"; 16 $cr = mysql_query($sql); 17 //判断是否插入成功 18 if(!$cr){ 19 echo "数据插入失败。"; 20 }else{ 21 echo "数据插入成功。"; 22 } 23 //执行sql查询语句 24 $sql = "select * from zh"; 25 $cr = mysql_query($sql); 26 //得到返回的结果集,并循环输出 27 echo "数据展示:"; 28 while($a = mysql_fetch_row($cr)){ 29 echo "{$a[0]}——{$a[1]}——{$a[2]}"; 30 } 31 //关闭数据库连接 32 mysql_close($link); 33 ?>
四,计算器设计
1 <div class="main"> 2 <form name="form"> 3 <input class="textView" name="textView" > 4 </form> 5 <table> 6 <tr> 7 <td><input type="button" class="button" value="C" onclick="Mclean()"></td> 8 <td><input type="button" class="button" value="<" onclick="back()"></td> 9 <td><input type="button" class="button" value="/" onclick="insert('/')"></td> 10 <td><input type="button" class="button" value="x" onclick="insert('*')"></td> 11 </tr> 12 <tr> 13 <td><input type="button" class="button" value="7" onclick="insert(7)"></td> 14 <td><input type="button" class="button" value="8" onclick="insert(8)"></td> 15 <td><input type="button" class="button" value="9" onclick="insert(9)"></td> 16 <td><input type="button" class="button" value="-" onclick="insert('-')"></td> 17 </tr> 18 <tr> 19 <td><input type="button" class="button" value="4" onclick="insert(4)"></td> 20 <td><input type="button" class="button" value="5" onclick="insert(5)"></td> 21 <td><input type="button" class="button" value="6" onclick="insert(6)"></td> 22 <td><input type="button" class="button" value="+" onclick="insert('+')"></td> 23 24 </tr> 25 <tr> 26 <td><input type="button" class="button" value="1" onclick="insert(1)"></td> 27 <td><input type="button" class="button" value="2" onclick="insert(2)"></td> 28 <td><input type="button" class="button" value="3" onclick="insert(3)"></td> 29 <td rowspan="2"><input style="height: 107px" type="button" class="button" value="=" onclick="equal()"></td> 30 31 </tr> 32 <tr> 33 <td colspan="2"><input style="width: 107px" type="button" class="button" value="0" onclick="insert(0)"></td> 34 <td><input type="button" class="button" value="." onclick="insert('.')"></td> 35 </tr> 36 37 38 </table> 39 <tr> 40 <td><input class="button1" value=" 开方计算" onclick="sqrt()"></td> 41 </tr>