php注册登录源代码
链接数据库
<?php $conn=mysql_connect('localhost','root',''); mysql_select_db('ht',$conn); mysql_query("set names utf8"); error_reporting(0); ?>
以下为注册源代码:
<?php //html POST 的数据 $user1=$_POST['username1']; $password1=$_POST['password1']; //判断点击事件 if(add==$_GET['add']){ if( empty($user1) || empty($password1)){ echo "<script>alert('必填处不能为空');window.location.href='denglu.php';</script>"; }else{ $sqlzc="insert into admin(id,username,password,name,sex)values('','$user1','$password1','','')";//添加数据库信息 $row=mysql_query($sqlzc); if($row){ echo "<script>alert('注册成功');window.location.href='index.php'</script>"; }else{ echo "<script>alert('注册失败');window.location.href='denglu.php'</script>"; } } } ?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<form name="login" action="?add=add" method="post">
<p>用户名<input type=text name="username1"></p>
<p>密 码<input type=password name="password1"></p>
<p><input type="submit" name="submit" value="注册"></p>
</form>
</body>
</html>
以下为登录源代码:
<?php //html POST 的数据 $user=$_POST['name']; $password=$_POST['password']; //判断点击事件 if(aee==$_GET['aee']){ if( empty($user) || empty($password)){ echo "<script>alert('必填处不能为空');window.location.href='denglu.php';</script>"; }else{ $sqlcx="select * from admin where username='$user' and password='$password' ";//检测数据库是否有对应username和password; $result = mysql_query($sqlcx);//执行sql $rows=mysql_num_rows($result);//返回一个数值 if($rows){ echo "<script>alert('登录成功');window.location.href='index.php'</script>"; }else{ echo "<script>alert('登录失败');window.location.href='denglu.php'</script>"; } } } ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登陆</title> </head> <body> <form name="login" action="?aee=aee" method="post"> <p>用户名<input type=text name="name"></p> <p>密 码<input type=password name="password"></p> <p><input type="submit" name="submit" value="登录"></p> </form> </body> </html>
以下为PDO写法实例:
<?php include "conn.php"; //链接数据库 header("Content-Type:text/html;charset=utf-8"); unset($_SESSION["user"]); //html POST 的数据 $user=$_POST['username']; $password=$_POST['password']; //判断点击事件 if(aee==$_GET['aee']){ //登录判断php if( empty($user) || empty($password)){ echo "<script>alert('必填处不能为空');window.location.href='login.php';</script>"; }else{ $sql = $db->query("SELECT * FROM users WHERE username='".$user."' AND password='".$password."'")->fetchAll(PDO::FETCH_ASSOC); if(count($sql) > 0){ $_SESSION["user"]="yes"; echo "<script>alert('登录成功');window.location.href='index.php'</script>"; }else{ echo "<script>alert('登录失败!');window.location.href='login.php'</script>"; } } }
if(reg==$_GET['reg']){ //注册判断php if( empty($user) || empty($password)){ exit("<script>alert('必填处不能为空');window.location.href='login.php';</script>"); } else if(!preg_match("/^d*$/",$user) && !preg_match("/^d*$/",$password)){ exit('<script>alert("账号与密码请填写数字");window.location.href="index.php";</script>'); } else if(strlen($user) < 6 && strlen($password) < 6){ exit("<script>alert('账号与密码长度不能少于6位数');window.location.href='login.php';</script>"); } else{ $sql = $db->query("SELECT * FROM users WHERE username='".$user."'")->fetchAll(PDO::FETCH_ASSOC); if(count($sql) > 0){ exit('<script>alert("该账号已被注册");window.location.href="index.php";</script>'); } $sql = $db->query("INSERT INTO users (id,username,password) VALUES ('','".$user."','".$password."')"); if($sql > 0){ echo "<script>alert('注册成功');window.location.href='index.php'</script>"; }else{ echo "<script>alert('注册失败');window.location.href='login.php'</script>"; } } } //echo "<script>alert('".$user.'<br>'.$password.count($sql)."')</script>"; ?>