PHP教程

登录注册代码php

本文主要是介绍登录注册代码php,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

connect.php

<?php
    $server = "localhost";
    $db_username = "root";
    $db_passwd = "123456";
    $dbname = "test";
    //连接数据库
    $db_link =  mysqli_connect($server,$db_username,$db_passwd,$dbname);

    //连接失败
    if(!$db_link){
        die("Can't connect to MySQL Server.Errorcode:" . mysqli_connect_error());
    }
     //设置数据库编码
    mysqli_query($db_link,"SET NAMES 'utf8'"); 
    
?>

login.html

<html>
    <!--https://www.jb51.net/article/172542.htm-->
    <head>
    <title>后台登录</title>
    <!--解决html中文在浏览器中显示乱码-->
    <meta http-equiv="Content-Type"content="text/html;charset=utf-8">
    </head>
    <body>
    <h1>请输入你的账号密码进行登录</h1>
        <form action="login.php" method="post">
            <fieldset>
                <legend><h2>用户登录</h2></legend>
                <ul>
                <li>
                    <label>用户名:</label>
                    <input type="text" name="username">
                </li>
                <li>
                    <label>密码:</label>
                    <input type="password" name="passwd">
                </li>
                <li>
                    <label> </label>
                    <input type="checkbox" name="remember" value="yes"> 七天内自动登录
                </li>
                <li>
                    <label> </label>
                    <input type="submit" name="login" value="登录"> <input type="reset" name="reset" value="重置"> </td> </tr> 
                </li>
                <li>
                    <label> <label>
                        <td colspan="2" align="center"> 还没有账号,快去<a href="register.php" rel="external nofollow" >注册</a>吧</td>                </li>
                </ul>
            </fieldset>
        </form>
    </body>
</html>

register.php

<html>
<head><title>用户注册</title>
    <meta name="content-type"; charset="UTF-8">
</head>
<body>
    <h1>用户注册</h1>
    <form action="register.php" method="post">
        <table border="0">
            <tr><td>用户名:</td>
                <td><input type="text" id="id_name" name="username" required="required"></td>
            </tr>
            <tr><td>密码:</td>
                <td><input type="passwd" id="passwd" name="passwd" required="required"></td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <input type="submit" id="register" name="register" value="注册">
                    <input type="reset" id="rester" name="rester" value="重置">
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    如果已有账号,快去<a href="login.html" rel="external nofollow" rel="external nofollow"> 登录</a>吧!
                </td>
            </tr>
        </table>

    </form>
</body>
</html>



<?php
require('dbconnect.php');

header('Content-type:text/html;charset=utf-8');

$username = $_POST['username'];
$passwd = $_POST['passwd'];


$sql = "select username from user where username = '$username'";
$result = mysqli_query($db_link,$sql);
$row = mysqli_fetch_assoc($result);
//判断用户名是否存在
if($username == $row['username']){
    
    echo "<script>alert('用户名.$username.已经存在!请重新注册')</script>";

}else{
    //用户名不存在 将注册的账号密码加入数据库
    $sql_insert = "insert into user(username,passwd) values('$username','$passwd')";
    mysqli_query($db_link,$sql_insert);
    header('refrsh:3;url=login.html');
    echo "<script>alert('$username.用户注册成功,请点击登录去登录!')</script>";
}


?>

login.php

<?php

require("dbconnect.php");

header('Content-Type:text/html;charset=utf-8');
//开启session()
session_start();

//处理用户登录信息
if(isset($_POST['login'])){
//接收用户的登录信息
$username=$_POST['username'];
$passwd = $_POST['passwd'];

//判断提交的登录信息
if(($username == '') || ($passwd == '')){
    //若为空则视为未填写返回login.html页面
    header('refresh:3;url=login.html');
    echo "用户名或密码不能为空,3秒后将返回登录页面,请重新输入!";
    exit;
}else{
    //若不为空则判断和账号密码是否存在于数据库中
    $sql = "select username,passwd from user where username = '$username'"; //构造查询账号密码语句
    $result = mysqli_query($db_link,$sql); //查询字符串
    $row = mysqli_fetch_assoc($result);  //从结果中取一行为关联数组
        if(($username != $row['username']) || ($passwd != $row['passwd'])){
        //账号密码错误的情况下 同样和空处理一样
        header('refresh:3;url=login.html');
        echo "用户名或密码错误,3庙后将放回登录页面,请重新输入!";
        exit;
    }elseif($passwd==$row['passwd']){
        #用户名和密码都正确,将用户名和密码信息存到Session中
        $_SESSION['username'] = $username;
        $_SESSION['islogin'] = 1;
        
        echo "登录成功!三秒后跳转到后台管理页面";
        
        if($_POST['remember'] == "yes"){
            #如果勾选七天内自动登录 将其保存到cookie中 并设置保存7天
            setcookie('username',$username,time()+7*24*60*60);
            setcookie('code',md5($username.md5($passwd)),time()+7*24*60*60);
            #print_r($_COOKIE);
        }else{
            #没有勾选删除cookie
            setcookie('username','',time()-999999);
            setcookie('code','',time()-999999);
        }
        header('refresh:3;url=index.php');
    }
}   

}
?>

index.php

<?php
header('Content-type:text/html; charset=utf-8');
//开启session
session_start();

//先判断cookie是否有用户信息
if(isset($_COOKIE['username'])){
    $_SESSION['username'] = $_COOKIE['username'];
    $_SESSION['islogin'] = 1;
    #print_r($_SESSION);
}
if(isset($_SESSION['islogin'])){
    //如果已经登陆
    echo "你好".$_SESSION['username']." ,欢迎来到个人中心!<br>";
    echo "<a href='logout.php'>注销</a>";
}else{
    //如果没有登录
    echo "你还没有登录,请<a href = 'login.html'>登录</a>";
}
?>



这篇关于登录注册代码php的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!