php实现登录注册界面
首先你要搭建一个自己的数据库
我用wamp64创了一个people的数据库
具体操作可以参考该搭建链接:
这里就讲下我实现的功能代码:
创建sql.func.php实现一些基本函数功能
/**
*弹框
*/
function_alert($_info){
echo"";
exit;
}
/**
*_location():弹出一个对话框并且转跳到另一个界面
*@accesspublic
*@paramstring$_info对话框上显示的信息
*@paramstring$_url转跳的页面地址
*@returnvoid
*/
function_location($_info,$_url){
if($_info==null){
header('Location:'.$_url);
}else{
echo"";
exit;
}
}
/**
*_connect():连接数据库
*@accesspublic
*@returnvoid
*/
function_connect()
{
//定义全局变量$_conn,在函数外部也能调用
global$_conn;
$_conn=mysqli_connect(DB_HOST,DB_USER,DB_PWD);
if(!$_conn){
exit('数据库连接失败:'.mysqli_error($_conn));
}
}
/**
*_select_db():选择数据库
*@accesspublic
*@returnvoid
*/
function_select_db(){
global$_conn;
if(!mysqli_select_db($_conn,DB_NAME)){
exit('找不到数据库'.mysqli_error($_conn));
}
}
/**
*_set_names():设置字符编码
*@accesspublic
*@returnvoid
*/
function_set_names(){
global$_conn;
if(!mysqli_query($_conn,'SETNAMESUTF8')){
exit('字符编码错误'.mysqli_error($_conn));
}
}
/**
*_query():执行sql语句
*@accesspublic
*@paramstring$_sqlsql操作语句
*@returnstring返回结果集
*/
function_query($_sql){
global$_conn;
if(!$result=mysqli_query($_conn,$_sql)){
exit('SQL执行失败'.mysqli_error($_conn).mysqli_errno($_conn));
}
return$result;
}
/**
*_fetch_array():根据sql语句遍历数据库。返回一个数组,键名是数据库的表单结构名
*@accesspublic
*@paramstring$_sqlsql操作语句
*@returnarray|null
*/
function_fetch_array($_sql){
returnmysqli_fetch_all(_query($_sql),MYSQLI_ASSOC);
}
/**
*_num_rows():返回数据库中查找条件的数据个数
*@accesspublic
*@paramstring$_sqlsql操作语句
*@returnint返回数据个数
*/
function_num_rows($_sql){
returnmysqli_num_rows(_query($_sql));
}
/**
*_affected_rows():返回数据库里被影响到的数据条数
*@accesspublic
*@returnint返回影响到的记录数
*/
function_affected_rows(){
global$_conn;
returnmysqli_affected_rows($_conn);
}
/**
*_is_repeat():判断数据在数据库里是否已经存在
*@accesspublic
*@paramstring$_sqlsql操作语句
*@paramstring$_info弹窗上显示的文字
*@returnvoid
*/
function_is_repeat($_sql,$_info){
if(_fetch_array($_sql)){
_alert_back($_info);
}
}
/**
*_close():关闭数据库
*@accesspublic
*/
function_close(){
global$_conn;
if(!mysqli_close($_conn)){
exit('数据库关闭异常'.mysqli_error($_conn));
}
}
?>
connect.php 实现数据库的连接功能
$_conn=mysqli_connect('localhost','root','');
if(!$_conn){
exit('数据库连接失败:'.mysqli_error($_conn));
}
mysqli_select_db($_conn,'people')ordie('找不到数据库:'.mysqli_error($_conn).mysqli_errno($_conn));
mysqli_query($_conn,"SETNAMESUTF8");
//var_dump($_conn);
include"sql.func.php";
?>
login.php实现登录响应操作
include"./connect.php";
//接收数据
if(isset($_POST['register']))
{
_location('欢迎注册','register.php');
}
if(isset($_POST['userid'])&&isset($_POST['password'])){
//从数据库里查找用户名是否存在
$_sql="SELECTuser_id,user_passwordFROMuserWHEREuser_id='{$_POST['userid']}'";
$result=_fetch_array($_sql);
if(!empty($result[0])){
if($result[0]['user_password']==$_POST['password']){
_location('登录成功','/cxl862002755/');
}else{
_alert('密码错误');
}
}else{
_alert('用户名不存在');
}
_close();
exit;
}
?>
register.php实现注册响应操作
include"./connect.php";
if(isset($_POST['index']))_location("","index.html");
//接收数据
if(isset($_POST['userid'])&&isset($_POST['password'])){
$_userid=$_POST['userid'];
$_password=$_POST['password'];
if($_userid==''||$_password=='')_location("用户名和密码不能为空!","register.php");
//插入到数据库中
$_sql="INSERTINTOuser(user_id,user_password)values('{$_POST['userid']}','{$_POST['password']}')";
$_result=_query($_sql);
_location("注册成功!","index.html");
_close();
exit;
}else
?>
注册
#register{
width:600px;
height:280px;
position:absolute;
left:50%;
top:50%;
color:red;
font-size:20px;
font-weight:600;
margin-left:-300px;
margin-top:-140px;
border:1px;
background-color:red;
background-image:url(http://img0./it/u=1999267794,2294725296&fm=26&gp=0.jpg);
}
#form{
width:400px;
height:160px;
position:relative;
left:50%;
top:50%;
margin-left:-200px;
margin-top:-80px;
}
label{
width:70px;
display:inline-flex;
height:30px;
}
body{
background-image:url(/tech/zh_cn/news/product/891/1209/120916491939987300.jpg);
background-size:cover;
}
用户名:密  码: