PHP實現(xiàn)簡易用戶登錄系統(tǒng)
更新時間:2020年07月10日 08:33:39 作者:行云blog
這篇文章主要為大家詳細介紹了PHP實現(xiàn)簡易用戶登錄系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
PHP簡易用戶登錄系統(tǒng),供大家參考,具體內(nèi)容如下
最近剛剛看到PHP連接數(shù)據(jù)庫的實例,于是做了一個簡易的用戶系統(tǒng)
直接上代碼
連接數(shù)據(jù)庫:connect.php
<?php
$servername = "localhost";
$username = "formbd";
$password = "formbd";
$dbname = "form";
// 創(chuàng)建連接
$conn = new mysqli($servername, $username, $password, $dbname);
// 檢測連接
if ($conn->connect_error) {
die("連接失敗: " . $conn->connect_error);
}
?>
用戶注冊前端頁面:reg.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用戶注冊頁面</title>
</head>
<body>
<form action="reg.php" method="post">
<p>用戶名:<input type="text" name="name"></p>
<p>密 碼: <input type="text" name="password"></p>
<p><input type="submit" name="submit" value="注冊">
<a href="login.html" ><input type="button" name="login" value="已有賬號,返回登錄"></a>
</p>
</form>
</body>
</html>
注冊后端處理:reg.php
<?php
header("Content-Type: text/html; charset=utf8");
if(!isset($_POST['submit'])){
exit("錯誤執(zhí)行");
}//判斷是否有submit操作
$name=$_POST['name'];//post獲取表單里的name
$user_password=$_POST['password'];//post獲取表單里的password
include('connect.php');//鏈接數(shù)據(jù)庫
$q="insert into user(id,username,password) values (null,'$name','$user_password')";//向數(shù)據(jù)庫插入表單傳來的值的sql
$sql = "select * from user where username = '$name'";
if (($conn->query($sql))==$name) {
echo '用戶名已存在';
$result = $conn->query($sql);
/*echo "
<script>
setTimeout(function(){window.location.href='reg.html';},1000);
</script>
";*/
}
else {
$conn->query($q);
echo "注冊成功";
echo "
<script>
setTimeout(function(){window.location.href='login.html';},1000);
</script>
";
}
$conn->close();//關(guān)閉數(shù)據(jù)庫
?>
用戶登錄前端頁面:login.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登陸</title>
</head>
<body>
<form name="login" action="login.php" method="post">
<p>用戶名<input type=text name="name"></p>
<p>密 碼<input type=password name="password"></p>
<p><input type="submit" name="submit" value="登錄">
<a href="reg.html" ><input type="button" name="reg" value="注冊"></a>
</p>
</form>
</body>
</html>
登錄后端處理:login.php
<?PHP
header("Content-Type: text/html; charset=utf8");
if(!isset($_POST["submit"])){
exit("錯誤執(zhí)行");
}//檢測是否有submit操作
include('connect.php');//鏈接數(shù)據(jù)庫
$name = $_POST['name'];//post獲得用戶名表單值
$passowrd = $_POST['password'];//post獲得用戶密碼單值
if ($name && $passowrd){//如果用戶名和密碼都不為空
$sql = "select * from user where username = '$name' and password='$passowrd'";//檢測數(shù)據(jù)庫是否有對應(yīng)的username和password的sql
$result = $conn->query($sql);//執(zhí)行sql
$rows=$result->fetch_assoc();//返回一個數(shù)值
if($rows){//0 false 1 true
header("refresh:0;url=success.php");//如果成功跳轉(zhuǎn)至success.php頁面
exit;
}else{
echo "用戶名或密碼錯誤";
echo "
<script>
setTimeout(function(){window.location.href='login.html';},1000);
</script>
";//如果錯誤使用js 1秒后跳轉(zhuǎn)到登錄頁面重試;
}
}else{//如果用戶名或密碼有空
echo "表單填寫不完整";
echo "
<script>
setTimeout(function(){window.location.href='login.html';},1000);
</script>";
//如果錯誤使用js 1秒后跳轉(zhuǎn)到登錄頁面重試;
}
$conn->close();//關(guān)閉數(shù)據(jù)庫
?>
登錄成功后:success.php
PS:功能未完善
<?php include 'connect.php'; session_start(); //聲明變量 $username = isset($_SESSION['nmae']) ? $_SESSION['name'] : ""; ?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登陸成功</title>
</head>
<body>
歡迎光臨
<?php echo $username;?>
<?php ?>
</body>
</html>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Laravel Intervention/image圖片處理擴展包的安裝、使用與可能遇到的坑詳解
這篇文章主要給大家介紹了關(guān)于Laravel中Intervention/image圖片處理擴展包的安裝、使用與在使用可能遇到的坑的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2017-11-11
在Ubuntu 14.04上部署 PHP 環(huán)境及 WordPress
Ubuntu確實很好玩。有喜歡的命令行,簡潔的界面,不同于Window要的感覺。偶爾換換環(huán)境工作,學習Linux的思維方式,是一種不錯的做法。之前也折騰過Ubuntu,想在Linux下學習某些開發(fā)(主要還是和代碼打交道),Ubuntu當然是最好不過的選擇,并且剛發(fā)布了14.04版本2014-09-09

