php注册登录源代码

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>"; ?>

 

转载于:https://www.cnblogs.com/fan-bk/p/6937740.html

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值