PHP登录程序

1 登录程序准备

mysql 创建表

create table users(user_id int primary key auto_increment,

userName varchar(20) not null unique,

password varchar(20) not null);

 

database.php

$databaseConnection = null;

function getConnection(){
    $hostname = "localhost";
    $database = "test";
    $userName = "root";
    $password = "";
    global $databaseConnection;
    $databaseConnection = mysqli_connect($hostname,$userName,$password) or die(mysqli_errror());
    mysqli_select_db($databaseConnection,$database) or die(mysqli_error());
    
}
function closeConnection(){
    global $databaseConnection;
    if($databaseConnection){
        mysqli_close($databaseConnection) or die(mysqli_error());
    }
}
?>

2登录程序html和PHP

login.html



用户登录



用户登录





用户名:

密码:





login_process.php

include_once("functions/database.php");
$userName = $_POST['userName'];
$password = $_POST['password'];

getConnection();
 mysqli_select_db($databaseConnection,"test");
$sql = "select * from users where userName='$userName' and password='$password';";
$result = mysqli_query($databaseConnection,$sql);
if(mysqli_num_rows($result)>0)
    echo "登录成功";
else
    echo "用户名和密码错误!";
closeConnection();
?>

你可能感兴趣的:(php)