php+mysql实现简单登录注册修改密码网页(2)

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>修改密码</title> <style type="text/css"> form{ text-align: center; } </style> </head> <body> <?php session_start(); ?> <form action="alter_password.php" method="post" onsubmit="return alter()"> 用户名<input type="text" id ="username" /><br/> 旧密码<input type="password" id ="oldpassword"/><br/> 新密码<input type="password"/><br/> 确认新密码<input type="password"/><br/> <input type="submit" value="修改密码"> </form> <script type="text/javascript"> document.getElementById("username").value="<? php echo "${_SESSION["username"]}";?>" </script> <script type="text/javascript"> function alter() { var username=document.getElementById("username").value; var oldpassword=document.getElementById("oldpassword").value; var newpassword=document.getElementById("newpassword").value; var assertpassword=document.getElementById("assertpassword").value; var regex=https://www.jb51.net/^[/s]+$/; if(regex.test(username)||username.length==0){ alert("用户名格式不对"); return false; } if(regex.test(oldpassword)||oldpassword.length==0){ alert("密码格式不对"); return false; } if(regex.test(newpassword)||newpassword.length==0) { alert("新密码格式不对"); return false; } if (assertpassword != newpassword||assertpassword==0) { alert("两次密码输入不一致"); return false; } return true; } </script> </body> </html>

5.修改密码的后台操作alter_password.php:

<!doctype html> <html> <head> <meta charset="UTF-8"> <title>正在修改密码</title> </head> <body> <?php session_start (); $username = $_REQUEST ["username"]; $oldpassword = $_REQUEST ["oldpassword"]; $newpassword = $_REQUEST ["newpassword"]; $con = mysql_connect ( "localhost", "root", "root" ); if (! $con) { die ( '数据库连接失败' . $mysql_error () ); } mysql_select_db ( "user_info", $con ); $dbusername = null; $dbpassword = null; $result = mysql_query ( "select * from user_info where username ='$username';" ); while ( $row = mysql_fetch_array ( $result ) ) { $dbusername = $row ["username"]; $dbpassword = $row ["password"]; } if (is_null ( $dbusername )) { ?> <script type="text/javascript"> alert("用户名不存在"); window.location.href="https://www.jb51.net/alter_password.html"; </script> <?php } if ($oldpassword != $dbpassword) { ?> <script type="text/javascript"> alert("密码错误"); window.location.href="https://www.jb51.net/alter_password.html"; </script> <?php } mysql_query ( "update user_info set password='$newpassword' where username='$username'" ) or die ( "存入数据库失败" . mysql_error () );//如果上述用户名密码判定不错,则update进数据库中 mysql_close ( $con ); ?> <script type="text/javascript"> alert("密码修改成功"); window.location.href="index.html"; </script> </body> </html>

6.注册帐号的主界面register.html:

<!doctype html> <html> <head> <meta charset="UTF-8"> <title>注册系统</title> <style type="text/css"> form { text-align: center; } </style> </head> <body> <form action="register.php" method="post" onsubmit="return check()"> 用户名<input type="text"><br> 密码<input type="password"><br> 确认密码<input type="password"><br> <input type="submit" value="注册"> </form> <script type="text/javascript"> function check() { var username=document.getElementById("username").value; var password=document.getElementById("password").value; var assertpassword=document.getElementById("assertpassword").value; var regex=https://www.jb51.net/^[/s]+$/; if(regex.test(username)||username.length==0){ alert("用户名格式不对"); return false; } if(regex.test(password)||password.length==0){ alert("密码格式不对"); return false; } if(password!=assertpassword){ alert("两次密码不一致"); return false; } } </script> </body> </html>

7.注册帐号的后台操作register.php:

<!doctype html> <html> <head> <meta charset="UTF-8"> <title>注册用户</title> </head> <body> <?php session_start(); $username=$_REQUEST["username"]; $password=$_REQUEST["password"]; $con=mysql_connect("localhost","root","root"); if (!$con) { die('数据库连接失败'.$mysql_error()); } mysql_select_db("user_info",$con); $dbusername=null; $dbpassword=null; $result=mysql_query("select * from user_info where username ='$username';"); while ($row=mysql_fetch_array($result)) { $dbusername=$row["username"]; $dbpassword=$row["password"]; } if(!is_null($dbusername)){ ?> <script type="text/javascript"> alert("用户已存在"); window.location.href="register.html"; </script> <?php } mysql_query("insert into user_info (username,password) values('$username','$password')") or die("存入数据库失败".mysql_error()) ; mysql_close($con); ?> <script type="text/javascript"> alert("注册成功"); window.location.href="index.html"; </script> </body> </html>

8.非法登录时退出登录的操作exit.php:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/fbbb4c3eea6b29f099db0a01701b0c45.html