生活随笔
收集整理的這篇文章主要介紹了
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;}}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
> 
 
 
 
<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) {mysqli_free_result($res
);$data
->closeConn();echo
"<script type='text/javascript'>";echo
"alert('賬號密碼錯誤,請重新輸入');";echo
"history.back();";echo
"</script>";
}
else{$id
= mysqli_fetch_object($res
)->id
;mysqli_free_result($res
);$data
->closeConn();setcookie("id",$id
);setcookie("password","TRUE");header("Location:main.php");
}$data
->closeConn();
?>
登錄跳轉(zhuǎn)頁面main.php
代碼
<?php
require("./MysqlData.php");
$id
= $_COOKIE
["id"];
$password
= $_COOKIE
["password"];
if ($password
!= "TRUE") {header("location:denglu.html");exit();
}
$data
= new MysqlData;
$sql
= "SELECT * FROM users WHERE id = ".$id
.";";
$row
= $data
->getRow($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
$id
= $_COOKIE
["id"];
$password
= $_COOKIE
["password"];
if ($password
!= "TRUE") {header("location:denglu.html");exit();
}
else{require("./MysqlData.php");$id
= $_COOKIE
["id"];$data
= new MysqlData;$sql
= "SELECT * FROM users WHERE id = ".$id
.";";$row
= $data
->getRow($sql
);$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
;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 spanNode
= document
.getElementById("user_sex");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");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;}}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
: 
 
 
<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
> 
 
 
 
<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
$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'];$data
= new MysqlData;$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
;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 spanNode
= document
.getElementById("user_sex");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");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;}}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
> 
 
 
 
<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"];
$user_password
= md5($password
);
$zhuce_password
= md5($upassword
);$data
= new MysqlData;
$sele_sql
= "SELECT * FROM users WHERE name = '".$name
."';";
$res
= $data
->sqlRun($sele_sql
);
if ($res
->num_rows
!= 0) {mysqli_free_result($res
);echo
"<script type='text/javascript'>";echo
"alert('此賬號已經(jīng)有人使用');";echo
"history.back();";echo
"</script>";
}
else{mysqli_free_result($res
);$zhuce_sql
= $data
->zhuceData($name
,$user_password
,$sex
,$age
,$email
);$res
= $data
->sqlRun($zhuce_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 $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)容還不錯,歡迎將生活随笔推薦給好友。