带数据库的php登录界面 数据库 php

用php做个登陆界面,代码要怎么写,用户名和密码在数据库中 , 怎么关联呢 。你可以做一个简单的例如你只有用户名和密码即可那么你先在数据库(以mysql为例)中建表例如叫做user字段为 id name pass分别是编号、用户名、密码长度分别是int(8) a_t(自动编号) 主键,varchar(50) ,varchar(50) 你可以添加一个测试数据 例如 1,admin,admin//说明密码这里不说加密的问题 , 用明文实现 登录页面的代码:login.phphtmlheadtitle用户登录/title/headbodyform action="checklogin.php" name="loginform" method="post"用户名:input name="name" type="text"br密码:input name="password" type="password"brinput value="https://www.04ip.com/post/登录" type="submit"br/form/body/html checklogin.php代码(检测登录): $name=$_POST[name];$pass=$_POST[password];session_start();//这个可以维持登录状态,可以参照session的使用
//登录检查函数function login_state($uid,$user_shell){$sql="SELECT * FROM `user` WHERE `name`='$name'";
$query=@mysql_query($sql) or die(mysql_error());
$us=is_array($row=@mysql_fetch_array($query));
$user_shell=$us ? $user_shell==$row[password]:FALSE;
if($user_shell){echo "scriptalert('登录成功');/script";
return $row;
}else{
echo "scriptalert('您暂时不能浏览该页面,请先登录');location.href='https://www.04ip.com/post/login.php';/script";
}}login_state($name,$password);?
希望可以帮到您 , 如果还有问题可以联系2458285853
php登录页面完整代码连接数据库创建conn.php , 连接数据库 。
$dns = 'mysql:host=127.0.0.1;dbname=test';
$username = 'root';
$password = 'root';
// 1.连接数据库,创建PDO对象
$pdo = new PDO($dns,$username,$password);
创建login.html,登陆页面 。
用户名
密 码
创建login.php , 验证账号密码 。
header("Content-Type: text/html; charset=utf8");
if(!isset($_POST["submit"])){
exit("错误执行");
}//检测是否有submit操作
include('conn.php');//链接数据库
$name = $_POST['name'];//post获得用户名表单值
$pwd = sha1($_POST['password']);//post获得用户密码单值
if ($name$pwd){//如果用户名和密码都不为空
$sql = "select * from user where username = '$name' and password='$pwd'";//检测数据库是否有对应的username和password的sql
$stmt = $pdo-prepare($sql);
$stmt-execute();
if($stmt-fetch(PDO::FETCH_BOUND)){//0 false 1 true
header("refresh:0;url=welcome.html");//如果成功跳转至welcome.html页面
exit;
}else{
echo "用户名或密码错误";
echo "
setTimeout(function(){window.location.href='https://www.04ip.com/post/login.html';},1000);
";//如果错误使用js 1秒后跳转到登录页面重试;
}
}else{//如果用户名或密码有空
echo "表单填写不完整";
echo "
setTimeout(function(){window.location.href='https://www.04ip.com/post/login.html';},1000);
";
//如果错误使用js 1秒后跳转到登录页面重试;
}
$pdo = null;
创建signup.html , 注册页面
用户名:
密 码:
创建signup.php
header("Content-Type: text/html; charset=utf8");
if(!isset($_POST['submit'])){
exit("错误执行");
}//判断是否有submit操作
$name=$_POST['name'];//post获取表单里的name
$pwd = sha1($_POST['password']);//post获取表单里的password
include('conn.php');//链接数据库
$sql="insert into user(id,username,password) values (null,'$name','$pwd')";//向数据库插入表单传来的值的sql
$stmt = $pdo-prepare($sql);
$stmt-execute();
$stmt-fetch(PDO::FETCH_BOUND);
if (!$stmt){
die('Error: ' . $stmt-getMessage());//如果sql执行失败输出错误

推荐阅读