PHP connects to the database to realize the function of user registration and login

Time:2022-5-3

PHP connects to the database to realize the function of user registration and login

)

The code is as follows

Database connection, login page, registration function, login function, background page, logout function.

dbconnect.php

<?php
    $server = "localhost";
    $db_username = "root";
    $db_passwd = "123456";
    $dbname = "test";
    //Connect database
    $db_link =  mysqli_connect($server,$db_username,$db_passwd,$dbname);

    //Connection failed
    if(!$db_link){
        die("Can't connect to MySQL Server.Errorcode:" . mysqli_connect_error());
    }
     //Set database code
    mysqli_query($db_link,"SET NAMES 'utf8'"); 
    
?>

login.html

<html>
    <!--https://www.jb51.net/article/172542.htm-->
    <head>
    < title > background login < / Title >
    <!-- Solve the problem of displaying garbled code in HTML Chinese browser -- >
    <meta http-equiv="Content-Type"content="text/html;charset=utf-8">
    </head>
    <body>
    <h1>Please enter your account and password to log in</h1>
        <form action="login.php" method="post">
            <fieldset>
                < legend > < H2 > user login < / H2 > < / legend >
                <ul>
                <li>
                    < label > User Name: < / label >
                    <input type="text" name="username">
                </li>
                <li>
                    < label > password: < / label >
                    <input type="password" name="passwd">
                </li>
                <li>
                    <label> </label>
                    < input type = "checkbox" name = "remember" value = "yes" > log in automatically within seven days
                </li>
                <li>
                    <label> </label>
                    < input type = "submit" name = "login" value = "login" > < input type = "reset" name = "reset" value = "reset" > < / td > < / TR > 
                </li>
                <li>
                    <label> <label>
                        < TD colSpan = "2" align = "center" > you don't have an account yet. Go to < a href = "register. PHP" rel = "external nofollow" > register < / a ></li>
                </ul>
            </fieldset>
        </form>
    </body>
</html>

register.php

<html>
< head > < title > user registration < / Title >
    <meta name="content-type"; charset="UTF-8">
</head>
<body>
    <h1>User registration</h1>
    <form action="register.php" method="post">
        <table border="0">
            < tr > < td > User Name:</td>
                <td><input type="text" id="id_name" name="username" required="required"></td>
            </tr>
            < tr > < td > password:</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 = "register" >
                    < input type = "reset" id = "rester" name = "rester" value = "reset" >
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    If you already have an account, go to < a href = "login. HTML" rel = "external nofollow" rel = "external nofollow" > log in to < / 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);
//Determine whether the user name exists
if($username == $row['username']){
    
    Echo "< script > alert ('user name. $username. Already exists! Please re register ') < / script >";

}else{
    //The user name does not exist. Add the registered account password to the database
    $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. User registered successfully, please click login to login!') < / script >";
}


?>

login.php

<?php

require("dbconnect.php");

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

//Process user login information
if(isset($_POST['login'])){
//Receive user login information
$username=$_POST['username'];
$passwd = $_POST['passwd'];

//Determine the login information submitted
if(($username == '') || ($passwd == '')){
    //If it is blank, it will be regarded as not filled in and return login HTML page
    header('refresh:3;url=login.html');
    Echo "the user name or password cannot be empty. You will return to the login page in 3 seconds. Please re-enter!";
    exit;
}else{
    //If it is not empty, judge whether the account and password exist in the database
    $sql = "select username,passwd from user where username = '$username'"; // Construct query account password statement
    $result = mysqli_ query($db_link,$sql); // Query string
    $row = mysqli_ fetch_ assoc($result);  // Get a row associative array from the result
        if(($username != $row['username']) || ($passwd != $row['passwd'])){
        //When the account and password are wrong, it is the same as empty processing
        header('refresh:3;url=login.html');
        Echo "the user name or password is wrong. It will be put back to the login page after 3 days, please re-enter!";
        exit;
    }elseif($passwd==$row['passwd']){
        #The user name and password are correct. Save the user name and password information in the session
        $_SESSION['username'] = $username;
        $_SESSION['islogin'] = 1;
        
        Echo "login succeeded! Jump to the background management page after three seconds";
        
        if($_POST['remember'] == "yes"){
            #If checked, automatically log in within seven days, save it to the cookie and set to save it for seven days
            setcookie('username',$username,time()+7*24*60*60);
            setcookie('code',md5($username.md5($passwd)),time()+7*24*60*60);
            #print_r($_COOKIE);
        }else{
            #Delete cookie is not checked
            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');
//Open session
session_start();

//First determine whether the cookie has user information
if(isset($_COOKIE['username'])){
    $_SESSION['username'] = $_COOKIE['username'];
    $_SESSION['islogin'] = 1;
    #print_r($_SESSION);
}
if(isset($_SESSION['islogin'])){
    //If you have logged in
    Echo "hello"$_ SESSION['username']. ", welcome to the personal center! < br >";
    Echo "< a href ='logout. PHP '> logout < / a >";
}else{
    //If you are not logged in
    Echo "you haven't logged in yet, please < a href = 'login. HTML' > log in < / a >";
}
?>

logout.php

<?php
header('Contet-type:text/html; charset=utf-8');
//Operation after logout
session_start();
//Clear session
$username = $_ SESSION['username']; // For the following prompt information
$_SESSION = array();
session_destroy();

//Clear cookies
setcookie('username','', time()-99);
setcookie('code','', time()-99);

//Prompt information
Echo "look forward to logging in next time,"$ username.'< br>';
Echo "< a href ='login. HTML '> login again < / a >";
?>