php实现基本网站登陆与注册
基于win7虚拟机+phpstudy
创建注册与登陆html页面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登录界面</title>
</head>
<body>
<form method="post" action="login.php">
账号:
<input type="text" name="usernamel"><br/><br/>
密码:
<input type="password" name="passwordl">
<input type="submit" value="登录" name="subl">
<a href="http://www.shixun.com/register.html">没有账号,注册</a>
</form>
</body>
</html>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>注册界面</title>
</head>
<body>
<form method="post" action="register.php">
姓名:<br/><br/>
<input type="text" name="username"><br/><br/>
年龄:<br/><br/>
<input type="text" name="userage"><br/><br/>
性别:<br/><br/> <input name="usersex" type="radio" value="0" />男 <input name="usersex" type="radio" value="1" />女 <input name="usersex" type="radio" value="2" />其它 <br/><br/>
身份信息:<br/><br/> <input type="text" name="userid"><br/><br/>
账号:<br/><br/> <input type="text" name="id"><br/><br/>
密码:<br/><br/> <input type="password" name="password">
<br/><br/> <input type="submit" value="注册" name="sub">
</form>
</body>
</html>
|
注册和登陆检查-php(连接mysql数据库)
登陆检查php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| <?php
header("Content-type:text/html;charset=utf-8");
$link=mysql_connect("localhost","root","root");
if($link)
{ $select=mysql_select_db("login",$link);
if($select)
{ if(isset($_POST["subl"]))
{ $name=$_POST["usernamel"];
$password=$_POST["passwordl"];
if($name==""||$password=="")
{ echo"<script type="."\""."text/javascript"."\"".">"."window.alert"."("."\""."请填写正确的信息!"."\"".")".";"."</script>";
echo"<script type="."\""."text/javascript"."\"".">"."window.location="."\""."http://www.shixun.com/login.html"."\""."</script>";
exit; }
$str="select password from register where id="."'"."$name"."'";
mysql_query('SET NAMES UTF8'); $result=mysql_query($str,$link);
$pass=mysql_fetch_row($result);
$pa=$pass[0];
if($pa==$password)
{ echo"登录成功!"; } else { echo"<script type="."\""."text/javascript"."\"".">"."window.alert"."("."\""."登录失败!"."\"".")".";"."</script>";
echo"<script type="."\""."text/javascript"."\"".">"."window.location="."\""."http://www.shixun.com/login.html"."\""."</script>";
} } } } ?>
|
注册检查
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
| <?php
$link=mysql_connect("localhost","root","root");
header("Content-type:text/html;charset=utf-8");
if($link)
{
$select=mysql_select_db("login",$link);
if($select)
{
if(isset($_POST["sub"]))
{ $username=$_POST["username"]; $userage=$_POST["userage"]; $usersex=$_POST["usersex"]; $userid=$_POST["userid"]; $id=$_POST["id"];
$password=$_POST["password"]; if($username==""||$userage==""||$usersex==""||$userid==""||$id==""||$password=="")
{
echo"<script type="."\""."text/javascript"."\"".">"."window.alert"."("."\""."请填写完成!"."\"".")".";"."</script>";
echo"<script type="."\""."text/javascript"."\"".">"."window.location="."\""."http://www.shixun.com/register.html"."\""."</script>"; exit;
} $regex = "/\/|\~|\!|\@|\#|\\$|\%|\^|\&|\*|\(|\)|\_|\+|\{|\}|\:|\<|\>|\?|\[|\]|\,|\.|\/|\;|\'|\`|\-|\=|\\\|\|/"; if(preg_match($regex,$username))
{ echo"<script type="."\""."text/javascript"."\"".">"."window.alert"."("."\""."姓名不能有特殊字符!"."\"".")".";"."</script>";
echo"<script type="."\""."text/javascript"."\"".">"."window.location="."\""."http://www.shixun.com/register.html"."\""."</script>";
exit; } if(!is_numeric($userage)||$userage>150||$userage<1) { echo"<script type="."\""."text/javascript"."\"".">"."window.alert"."("."\""."年龄请输入数字且在1-150之间!"."\"".")".";"."</script>"; exit; } if(!preg_match("/^\d*$/",$userid)||strlen($userid)!=18) { echo"<script type="."\""."text/javascript"."\"".">"."window.alert"."("."\""."身份信息请输入数字且为18位!"."\"".")".";"."</script>"; exit; } if(preg_match($regex,$password)||strlen($password)<=6)
{ echo"<script type="."\""."text/javascript"."\"".">"."window.alert"."("."\""."密码不能有特殊字符且大于6位!"."\"".")".";"."</script>";
echo"<script type="."\""."text/javascript"."\"".">"."window.location="."\""."http://www.shixun.com/register.html"."\""."</script>";
exit; } $str="select count(*) from register where id="."'"."$id"."'";
$result=mysql_query($str,$link);
$pass=mysql_fetch_row($result);
$pa=$pass[0];
if($pa==1)
{
echo"<script type="."\""."text/javascript"."\"".">"."window.alert"."("."\""."该用户名已被注册"."\"".")".";"."</script>";
echo"<script type="."\""."text/javascript"."\"".">"."window.location="."\""."http://www.shixun.com/register.html"."\""."</script>";
exit;
} echo"username:"."$username"; echo"userage:"."$userage"; echo"usersex:"."$usersex"; echo"userid:"."$userid"; echo"id:"."$id"; echo"password:"."$password"; $age=(int)$userage; $sex=(int)$usersex; $sql="insert into register values("."\""."$username"."\"".","."\""."$age"."\"".","."\""."$sex"."\"".","."\""."$userid"."\"".","."\""."$id"."\"".","."\""."$password"."\"".")"; mysql_query($sql,$link); mysql_query('SET NAMES UTF8'); $close=mysql_close($link); if($close) { echo"数据库关闭"; echo"注册成功!"; } } } } ?>
|
知识点:
1、php-echo中 . 能连接两个“”字符串,并且echo出js脚本的时候能够执行js。
2、preg_match(),前一个参数可以是正则表达式,后一个参数是字符串
3、”//|~|!|@|#|\$|%|^|&|*|(|)|_|+|{|}|:|<|>|?|[|]|,|.|/|;|'|`|-|=|\||/“判断包含特殊字符的正则表达式
4、”/^\d*$/“判断字符串是否是纯数字的正则表达式
5、mysql命令:
1 2 3
| create database login;#创建login数据库 use login create table register(username varchar(20),userage int,usersex int,userid varchar(18),id varchar(20),password varchar(20));#创建register表
|
评论加载中