Home > headlines > body text

PHP implements member login registration page with html plus Session and Cookie

PHP中文网
Release: 2020-07-13 16:25:23
Original
3890 people have browsed it

User registration information, after the administrator checks the information and passes the review, successful login with the registered username and password can be achieved. Session and cookie are used to obtain user information and cannot skip the login page and enter the main page directly

PHP implements member login registration page with html plus Session and Cookie

1.Session
Stored on the server
Can store any content
There is a default expiration time: about 15 minutes
Relatively safe
Usage:
1. Must start writing on the php page: session_start(); open session
2. Write Session: $_SESSION["uid"]=$uid;
3. Read Session:$_SESSION ["uid"];

2.Cookie
Stored on the client
Can only store strings
No expiration time by default
Usage:
1.Set Cookie: setcookie("name","value");
2. Value:$_COOKIE["name"];

Write in php
Purpose:
Get user information
Cannot skip the login page

PHP implements member login registration page with html plus Session and Cookie

zhuce.php

PHP implements member login registration page with html plus Session and Cookie

PHP implements member login registration page with html plus Session and Cookie##

<!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(&#39;main.php&#39;)" /></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>
Copy after login

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(&#39;".$uid."&#39;,&#39;".$pwd."&#39;,&#39;".$name."&#39;,".$sex.",&#39;".$birthday."&#39;,&#39;".$code."&#39;,false)";
$str = $db->Select($sql,"QT","mydb");
echo $str;

?>
Copy after login

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=&#39;text&#39; value=&#39;审核已通过&#39; checked=&#39;checked&#39;/>":"<a href=&#39;shenhechuli.php?uid={$lie[0]}&#39;>审核</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>
Copy after login

shehechuli.php

<?php
 include("mydbda.php");
 $uid=$_GET["uid"];
 
 $db=new mydbda();
 $sql="update users set isok=true where uid=&#39;".$uid."&#39;";
 $str=$db->Select($sql,"QT","mydb");
 header("Location:main.php");

?>
Copy after login

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>
Copy after login

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=&#39;".$uid."&#39; and pwd=&#39;".$pwd."&#39; 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");
    }    
?>
Copy after login
<?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
Copy after login

php Chinese website learning topic: php session (including pictures, texts, videos, cases)

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template