厦门服务器租用>>php实现会员登陆注册页有html加Session和Cookie

php实现会员登陆注册页有html加Session和Cookie

发布时间:2022/8/18 17:27:00    来源: 纵横数据

用户注册信息,管理员核对信息审核通过后,可实现注册的用户名和密码的成功登陆,利用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


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<!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>
<p style="background-color:#CCC; width:300px; padding-left:10px;">
<h1>注册页面</h1>
<p>用户名:<input type="text" id="uid" /></p><br />
<p>密&nbsp;&nbsp;码:<input type="text" id="pwd" /></p><br />
<p>姓&nbsp;&nbsp;名:<input type="text" id="name" /></p><br />
<p>性&nbsp;&nbsp;别:<input type="radio" checked="checked" name="sex" id="nan" value="true" />男&nbsp;<input type="radio" name="sex" value="false" />女</p><br />
<p>生&nbsp;&nbsp;日:<input type="text" id="birthday" /></p><br />
<p>工&nbsp;&nbsp;号:<input type="text" id="code" /></p><br />
<p><input type="button" value="提交" id="btn" />&nbsp;&nbsp;&nbsp;<input type="button" value="查看" onclick="window.open('main.php')" /></p><br />
</p>
</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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?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

1
2
3
4
5
6
7
8
9
10
<?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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!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>
<p style="width:300px; background-color:#CCC">
<h1>登陆页面</h1>
 
<form action="dengluchuli.php" method="post">
<p>用户名:<input type="text" name="uid" /></p><br />
 
<p>密&nbsp;&nbsp;码:<input type="text" name="pwd" /></p><br />
<p><input type="submit" value="登陆"  /></p>
</form></p>
</body>
</html>

dengluchuli.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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");
    }   
?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?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,