日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > php >内容正文

php

php实现用户登录、注册以及修改功能(附加源码~)

發(fā)布時間:2023/12/18 php 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php实现用户登录、注册以及修改功能(附加源码~) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本代碼實現(xiàn)了php+Mysql數(shù)據(jù)庫 用戶的注冊和登錄功能,希望對大家有幫助,后附加源碼,注釋詳細(xì)~

first要貼上表結(jié)構(gòu)~

登錄頁面

前臺登錄頁面denglu.html


代碼

<!DOCTYPE html> <html> <head><meta charset="utf-8"><title>登錄頁面</title> <script type="text/javascript">//檢查用戶名是否填寫function checkName(){var name = document.getElementById("add_name").value;var spanNode = document.getElementById("user_name");if (name != "") {spanNode.innerHTML = "已填".fontcolor("green");return true;}else{spanNode.innerHTML = "不能為空".fontcolor("red");return false;}}//檢查密碼是否填寫function checkPassword(){var password = document.getElementById("add_password").value;var spanNode = document.getElementById("user_password");if (password != "") {spanNode.innerHTML = "已填".fontcolor("green");return true;}else{spanNode.innerHTML = "請輸入密碼".fontcolor("red");return false;}}//點擊登錄時進(jìn)行檢查function checkForm(){var name = checkName();var password = checkPassword();if (name && password) {return true;}else{return false;}} </script> </head> <style type="text/css">.wrapper{text-align: center;width:1000px;margin:20px auto;}h2{background-color:#7CCD7C;margin:0px;text-align:center;}.add{margin:20px auto;}.add labal{text-align: center;background-color: #FFB6C1;color: #fff;margin:20px auto;}.btn { background-color: #008CBA;;border-radius:8px;color: white;padding: 6px 93px;text-align: center;text-decoration: none;display: inline-block;font-size: 14px;}.error {color: #FF0000;} </style> <body> <div class="wrapper"> <h2>用戶管理系統(tǒng)</h2> <div class="add"> <labal>登錄系統(tǒng)</labal> </div><div class="add"><form method="post" action="denglu.php" onsubmit="return checkForm()"><labal>用戶名:</labal> <input type="text" name="add_name" id="add_name" placeholder="請輸入用戶名"><span id="user_name" class="error">*</span><br><br><labal>密碼:</labal>&nbsp&nbsp&nbsp&nbsp<input type="password" name="add_password" id="add_password"placeholder="請輸入密碼"><span id="user_password" class="error">*</span><br><br><input class="btn" type="submit" value="登錄"><br></form><a href="zhuce.html">立即注冊</a></div> </div> </body> </html>

后臺處理頁面代碼denglu.php

代碼

<?php require("./MysqlData.php");$user_name = $_POST['add_name']; $user_password = $_POST['add_password']; $password = md5($user_password); $data = new MysqlData; $sql = $data->select_user($user_name,$password); $res = $data->sqlRun($sql); //若賬號密碼錯誤 if ($res->num_rows == 0) {//釋放$res占用的內(nèi)存mysqli_free_result($res);//關(guān)閉數(shù)據(jù)庫連接$data->closeConn();//提示輸入正確的賬號echo "<script type='text/javascript'>";echo "alert('賬號密碼錯誤,請重新輸入');";echo "history.back();";echo "</script>"; } //若賬號密碼正確 else{//獲取id字段$id = mysqli_fetch_object($res)->id;//釋放$res占用的內(nèi)存mysqli_free_result($res);//關(guān)閉數(shù)據(jù)庫連接$data->closeConn();//將用戶數(shù)據(jù)加入cookiessetcookie("id",$id);setcookie("password","TRUE");header("Location:main.php"); }$data->closeConn(); ?>

登錄跳轉(zhuǎn)頁面main.php


代碼

<?php require("./MysqlData.php"); /*檢查cookie中的password變量是否不等于true,是的話表示尚未登錄網(wǎng)站,就重定向到denglu.html頁面*/ $id = $_COOKIE["id"]; $password = $_COOKIE["password"]; if ($password != "TRUE") {header("location:denglu.html");exit(); } //創(chuàng)建對象 $data = new MysqlData; $sql = "SELECT * FROM users WHERE id = ".$id.";"; $row = $data->getRow($sql); // echo $sql; $data->closeConn(); ?> <!DOCTYPE html> <html> <head><meta charset="utf-8"><title>登錄成功頁面</title> </head><style type="text/css">.wrapper{text-align: center;width:1000px;margin:20px auto;}h2{background-color:#7CCD7C;margin:0px;text-align:center;}.my{margin:20px auto;}.my labal{text-align: center;background-color: #FFB6C1;color: #fff;margin:20px auto;}</style> <body> <div class="wrapper"> <h2>用戶管理系統(tǒng)</h2> <div class="my"> <labal>我的資料</labal> </div> <labal>用戶名:</labal><input type="text" value="<?php echo $row['name'] ?>" readonly="readonly"><br><br> <labal>性別:</labal> <input type="radio"value="0" <?php if ($row['sex'] == "0") echo "checked"?>><input type="radio"value="1" <?php if ($row['sex'] == "1") echo "checked"?>><br><br> <labal>年齡:</labal><input type="text" value="<?php echo $row['age'] ?>" readonly="readonly"><br><br> <labal>郵箱:</labal><input type="text" value="<?php echo $row['email'] ?>" readonly="readonly"><br><br> <a href="modify.php">修改我的資料</a> </div></body> </html>

修改資料頁面modify.php


代碼

<?php /*檢查cookie中的password變量是否不等于true,是的話表示尚未登錄網(wǎng)站,就重定向到denglu.html頁面*/ $id = $_COOKIE["id"]; $password = $_COOKIE["password"]; if ($password != "TRUE") {header("location:denglu.html");exit(); } else{require("./MysqlData.php");$id = $_COOKIE["id"];//連接數(shù)據(jù)庫$data = new MysqlData;//執(zhí)行sql查詢我的資料$sql = "SELECT * FROM users WHERE id = ".$id.";";$row = $data->getRow($sql);//關(guān)閉數(shù)據(jù)庫$data->closeConn(); } ?><!DOCTYPE html> <html> <head><meta charset="utf-8"><title></title> <script type="text/javascript">//檢查姓名function checkName(){var inputNode = document.getElementById("edit_name");var spanNode = document.getElementById("user_name");var nameLength = document.getElementById("edit_name").value.length;//獲取輸入框的內(nèi)容var content = inputNode.value;// 檢測名字是否只包含字母跟空格var reg = /^[a-zA-Z ]*$/;if(content==""){spanNode.innerHTML = "不能為空".fontcolor("red");return false;}if (nameLength > 15) {spanNode.innerHTML = "姓名長度過長".fontcolor("red");return false;}if (reg.test(content)){spanNode.innerHTML = "正確".fontcolor("green");return true;}else{spanNode.innerHTML = "只允許字母跟空格".fontcolor("red");return false;}}//檢查密碼function checkPassword(){var password = document.getElementById("edit_password");var passwordLength = document.getElementById("edit_password").value.length;var content = password.value;var spanNode = document.getElementById("user_password");if (passwordLength > 30) {spanNode.innerHTML = "密碼過長".fontcolor("red");return false;}if (content != "") {spanNode.innerHTML = "已填".fontcolor("green");return true;}else{spanNode.innerHTML = "密碼不能為空".fontcolor("red");return false;}}//檢查再次輸入的密碼function checkUpassword(){var password = document.getElementById("edit_password").value;var upassword = document.getElementById("upassword").value;var spanNode = document.getElementById("uupassword");if (upassword != password) {spanNode.innerHTML = "密碼不一致".fontcolor("red");return false;}if (upassword != "") {spanNode.innerHTML = "已填".fontcolor("green");return true;}else{spanNode.innerHTML = "請再次輸入密碼".fontcolor("red");return false;}}//檢查性別function checkSex(){// var inputNode = document.getElementById("add_sex");var spanNode = document.getElementById("user_sex");// var content = inputNode.value;//檢查不能為空if (!document.getElementById("man").checked && !document.getElementById("women").checked) {spanNode.innerHTML = "必填".fontcolor("red");return false;}else{spanNode.innerHTML = "已填".fontcolor("green");return true;}}//檢查年齡function checkAge(){var age = document.getElementById("edit_age").value;var spanNode = document.getElementById("user_age");//檢查年齡是否在1-120內(nèi)var reg = /^(?:[1-9][0-9]?|1[01][0-9]|120)$/;if (reg.test(age)) {spanNode.innerHTML = "已填".fontcolor("green");return true;}else{spanNode.innerHTML = "年齡不合法".fontcolor("red");return false;}}//檢查郵箱function checkEmail(){var email = document.getElementById("edit_email").value;var spanNode = document.getElementById("user_email");//驗證郵箱的正則var reg = /^[a-z0-9]\w+@[a-z0-9]+(\.[a-z]{2,3}){1,2}$/i;if(email==""){spanNode.innerHTML = "不能為空".fontcolor("red");return false;}if (reg.test(email)){spanNode.innerHTML = "正確".fontcolor("green");return true;}else{spanNode.innerHTML = "郵箱格式不正確".fontcolor("red");return false;}}//提交form表單時進(jìn)行檢查function checkForm(){var add_name = checkName();var add_sex = checkSex();var add_age = checkAge();var add_email = checkEmail();var add_password = checkPassword();var upassword = checkUpassword();if (add_name && add_sex && add_age && add_email && add_password && upassword) {return true;}else{return false;}}</script> </head> <style type="text/css">.wrapper{text-align: center;width:1000px;margin:20px auto;}h2{background-color:#7CCD7C;margin:0px;text-align:center;}.update{margin:20px auto;}.update labal{text-align: center;background-color: #FFB6C1;color: #fff;margin:20px auto;}.btn { background-color: #008CBA;;border-radius:8px;color: white;padding: 10px 20px;text-align: center;text-decoration: none;display: inline-block;font-size: 14px;}.error {color: #FF0000;} </style> <body> <div class="wrapper"><h2>用戶管理系統(tǒng)</h2><div class="update"><labal>修改我的資料</labal></div> <div><form method="post" action="update.php" align="center" onsubmit="return checkForm()">ID:&nbsp&nbsp&nbsp<input type="text" name="id" value="<?php echo $row['id'] ?>" readonly="readonly"><br><br><labal>用戶名:</labal><input type="text" id="edit_name" name="edit_name" value="<?php echo $row['name'] ?>"><span id="user_name" class="error">*</span><br><br><labal>密碼:</labal>&nbsp&nbsp&nbsp&nbsp<input type="password" name="edit_password" id="edit_password" placeholder="請設(shè)置密碼"><span id="user_password" class="error">*</span><br><br><labal>確認(rèn)密碼:</labal><input type="password" name="upassword" id="upassword" placeholder="請再次輸入密碼"><span id="uupassword" class="error">*</span><br><br><labal>性別:</labal><input type="radio" id="man" name="sex" value="0"<?php if(isset($row['sex']) && $row['sex']=="0") echo "checked";?>><input type="radio" id="women" name="sex" value="1"<?php if(isset($row['sex']) && $row['sex']=="1") echo "checked";?>><span id="user_sex" class="error">*</span><br><br><labal>年齡:</labal><input type="text" id="edit_age" name="edit_age" value="<?php echo $row['age'] ?>"><span id="user_age"></span><br><br><labal>郵箱:</labal><input type="text" id="edit_email" name="edit_email" value="<?php echo $row['email'] ?>"><span id="user_email" class="error">*</span><br><br><input class="btn" type="submit" name="submit" value="確認(rèn)修改"></form> </body> </html>

后臺處理update.php

代碼

<?php /*檢查cookie中的password變量是否不等于true,是的話表示尚未登錄網(wǎng)站,就重定向到denglu.html頁面*/ $id = $_COOKIE["id"]; $password = $_COOKIE["password"]; if ($password != "TRUE") {header("location:denglu.html");exit(); }else{require("./MysqlData.php");$u_id = $_POST['id'];$name = $_POST['edit_name'];$u_password = $_POST['edit_password'];$sex = $_POST['sex'];$age = $_POST['edit_age'];$email = $_POST['edit_email'];//創(chuàng)建對象,連接數(shù)據(jù)庫$data = new MysqlData;//執(zhí)行更新sql語句$sql = "UPDATE users SET name = '".$name."',password = '".$u_password."',sex = ".$sex.",age = ".$age.",email = '".$email."' WHERE id = ".$u_id.";";$res = $data->sqlRun($sql);$data->closeConn();header("Location:main.php"); }

注冊頁面

前臺頁面zhuce.html



代碼

<!DOCTYPE html> <html> <head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>注冊新用戶</title><script type="text/javascript">//檢查姓名function checkName(){var inputNode = document.getElementById("add_name");var spanNode = document.getElementById("user_name");var nameLength = document.getElementById("add_name").value.length;//獲取輸入框的內(nèi)容var content = inputNode.value;// 檢測名字是否只包含字母跟空格var reg = /^[a-zA-Z ]*$/;if(content==""){spanNode.innerHTML = "不能為空".fontcolor("red");return false;}if (nameLength > 15) {spanNode.innerHTML = "姓名長度過長".fontcolor("red");return false;}if (reg.test(content)){spanNode.innerHTML = "正確".fontcolor("green");return true;}else{spanNode.innerHTML = "只允許字母跟空格".fontcolor("red");return false;}}//檢查密碼function checkPassword(){var password = document.getElementById("add_password");var passwordLength = document.getElementById("add_password").value.length;var content = password.value;var spanNode = document.getElementById("user_password");if (passwordLength > 30) {spanNode.innerHTML = "密碼過長".fontcolor("red");return false;}if (content != "") {spanNode.innerHTML = "已填".fontcolor("green");return true;}else{spanNode.innerHTML = "密碼不能為空".fontcolor("red");return false;}}//檢查再次輸入的密碼function checkUpassword(){var password = document.getElementById("add_password").value;var upassword = document.getElementById("upassword").value;var spanNode = document.getElementById("uupassword");if (upassword != password) {spanNode.innerHTML = "密碼不一致".fontcolor("red");return false;}if (upassword != "") {spanNode.innerHTML = "已填".fontcolor("green");return true;}else{spanNode.innerHTML = "請再次輸入密碼".fontcolor("red");return false;}}//檢查性別function checkSex(){// var inputNode = document.getElementById("add_sex");var spanNode = document.getElementById("user_sex");// var content = inputNode.value;//檢查不能為空if (!document.getElementById("man").checked && !document.getElementById("women").checked) {spanNode.innerHTML = "必填".fontcolor("red");return false;}else{spanNode.innerHTML = "已填".fontcolor("green");return true;}}//檢查年齡function checkAge(){var age = document.getElementById("add_age").value;var spanNode = document.getElementById("user_age");//檢查年齡是否在1-120內(nèi)var reg = /^(?:[1-9][0-9]?|1[01][0-9]|120)$/;if (reg.test(age)) {spanNode.innerHTML = "已填".fontcolor("green");return true;}else{spanNode.innerHTML = "年齡不合法".fontcolor("red");return false;}}//檢查郵箱function checkEmail(){var email = document.getElementById("add_email").value;var spanNode = document.getElementById("user_email");//驗證郵箱的正則var reg = /^[a-z0-9]\w+@[a-z0-9]+(\.[a-z]{2,3}){1,2}$/i;if(email==""){spanNode.innerHTML = "不能為空".fontcolor("red");return false;}if (reg.test(email)){spanNode.innerHTML = "正確".fontcolor("green");return true;}else{spanNode.innerHTML = "郵箱格式不正確".fontcolor("red");return false;}}//提交form表單時進(jìn)行檢查function checkForm(){var add_name = checkName();var add_sex = checkSex();var add_age = checkAge();var add_email = checkEmail();var add_password = checkPassword();var upassword = checkUpassword();if (add_name && add_sex && add_age && add_email && add_password && upassword) {return true;}else{return false;}}</script> </head> <style type="text/css">.wrapper{text-align: center;width:1000px;margin:20px auto;}h2{background-color:#7CCD7C;margin:0px;text-align:center;}.add{margin:20px auto;}.add labal{text-align: center;background-color: #FFB6C1;color: #fff;margin:20px auto;}.btn { background-color: #008CBA;;border-radius:8px;color: white;padding: 6px 93px;text-align: center;text-decoration: none;display: inline-block;font-size: 14px;}.error {color: #FF0000;} </style> <body> <div class="wrapper"> <h2>用戶管理系統(tǒng)</h2> <div class="add"> <labal>注冊新用戶</labal> </div><div><form method="post" action="zhuce.php" onsubmit="return checkForm()"><labal>用戶名:</labal> <input type="text" name="add_name" id="add_name" placeholder="請設(shè)置用戶名"><span id="user_name" class="error">*</span><br><br><labal>密碼:</labal>&nbsp&nbsp&nbsp&nbsp<input type="password" name="add_password" id="add_password" placeholder="請設(shè)置密碼"><span id="user_password" class="error">*</span><br><br><labal>確認(rèn)密碼:</labal><input type="password" name="upassword" id="upassword" placeholder="請再次輸入密碼"><span id="uupassword" class="error">*</span><br><br><labal>性別:</labal><input type="radio" name="add_sex" id="man" value="0"><input type="radio" name="add_sex" id="women" value="1"><span id="user_sex" class="error">*</span><br><br> <labal>年齡:</labal><input type="text" name="add_age" id="add_age"placeholder="請輸入年齡"><span id="user_age"></span><br><br><labal>郵箱:</labal><input type="text" name="add_email" id="add_email" placeholder="請輸入郵箱"><span id="user_email" class="error">*</span><br><br><input class="btn" type="submit" value="注冊"></form></div> </div> </body> </html>

后臺處理zhuce.php


代碼

<?php require("./MysqlData.php");$name = $_POST["add_name"]; $password = $_POST["add_password"]; $upassword = $_POST["upassword"]; $sex = $_POST["add_sex"]; $age = $_POST["add_age"]; $email = $_POST["add_email"];//md5加密 $user_password = md5($password); $zhuce_password = md5($upassword);$data = new MysqlData; $sele_sql = "SELECT * FROM users WHERE name = '".$name."';"; $res = $data->sqlRun($sele_sql); // echo $sele_sql; //若賬號被使用 if ($res->num_rows != 0) {//釋放res內(nèi)存mysqli_free_result($res);//提示用戶已經(jīng)有人使用echo "<script type='text/javascript'>";echo "alert('此賬號已經(jīng)有人使用');";echo "history.back();";echo "</script>"; }//若賬號沒人使用 else{//釋放$res占用的內(nèi)存mysqli_free_result($res);//$zhuce_sql = $data->zhuceData($name,$user_password,$sex,$age,$email);$res = $data->sqlRun($zhuce_sql); } //關(guān)閉數(shù)據(jù)庫連接 $data->closeConn(); ?> <!DOCTYPE html> <html> <head><meta charset="utf-8"><title>注冊成功頁面</title> </head><style type="text/css">.wrapper{text-align: center;width:1000px;margin:20px auto;}h2{background-color:#7CCD7C;margin:0px;text-align:center;}.my{margin:20px auto;}.my labal{text-align: center;background-color: #FFB6C1;color: #fff;margin:20px auto;}</style> <body> <div class="wrapper"> <h2>用戶管理系統(tǒng)</h2> <div class="my"> <labal>我的注冊信息</labal> </div> <labal>用戶名:</labal><input type="text" value="<?php echo $name ?>" readonly="readonly"><br><br> <labal>性別:</labal> <input type="radio"value="0" <?php if ($sex == "0") echo "checked"?>><input type="radio"value="1" <?php if ($sex == "1") echo "checked"?>><br><br> <labal>年齡:</labal><input type="text" value="<?php echo $age ?>" readonly="readonly"><br><br> <labal>郵箱:</labal><input type="text" value="<?php echo $email ?>" readonly="readonly"><br><br> <a href="denglu.html">返回登錄頁面</a> </div> </body> </html>

總結(jié)

以上是生活随笔為你收集整理的php实现用户登录、注册以及修改功能(附加源码~)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。