php实现登录注册界面
php实现登录注册界面
首先你要搭建一个自己的数据库
我用wamp64创了一个people的数据库
具体操作可以参考该搭建链接:
https://blog.csdn.net/qq_36934826/article/details/78170855
这里就讲下我实现的功能代码:
创建sql.func.php实现一些基本函数功能
<?php
/**
*弹框
*/
function _alert($_info){
echo "<script type='text/javascript'>alert('$_info');history.back();</script>";
exit;
}
/**
* _location():弹出一个对话框并且转跳到另一个界面
* @access public
* @param string $_info 对话框上显示的信息
* @param string $_url 转跳的页面地址
* @return void
*/
function _location($_info,$_url){
if($_info==null){
header('Location:'.$_url);
}else{
echo "<script type='text/javascript'>alert('$_info');location.href='$_url';</script>";
exit;
}
}
/**
* _connect():连接数据库
* @access public
* @return void
*/
function _connect()
{
//定义全局变量$_conn,在函数外部也能调用
global $_conn;
$_conn=mysqli_connect(DB_HOST, DB_USER,DB_PWD);
if (!$_conn) {
exit('数据库连接失败:'.mysqli_error($_conn));
}
}
/**
* _select_db():选择数据库
* @access public
* @return void
*/
function _select_db(){
global $_conn;
if(!mysqli_select_db($_conn,DB_NAME)){
exit('找不到数据库'.mysqli_error($_conn));
}
}
/**
* _set_names():设置字符编码
* @access public
* @return void
*/
function _set_names(){
global $_conn;
if(!mysqli_query($_conn,'SET NAMES UTF8')){
exit('字符编码错误'.mysqli_error($_conn));
}
}
/**
* _query():执行sql语句
* @access public
* @param string $_sql sql操作语句
* @return string 返回结果集
*/
function _query($_sql){
global $_conn;
if(!$result=mysqli_query($_conn,$_sql)){
exit('SQL执行失败'.mysqli_error($_conn).mysqli_errno($_conn));
}
return $result;
}
/**
* _fetch_array():根据sql语句遍历数据库。返回一个数组,键名是数据库的表单结构名
* @access public
* @param string $_sql sql操作语句
* @return array|null
*/
function _fetch_array($_sql){
return mysqli_fetch_all(_query($_sql),MYSQLI_ASSOC);
}
/**
* _num_rows():返回数据库中查找条件的数据个数
* @access public
* @param string $_sql sql操作语句
* @return int 返回数据个数
*/
function _num_rows($_sql){
return mysqli_num_rows(_query($_sql));
}
/**
* _affected_rows():返回数据库里被影响到的数据条数
* @access public
* @return int 返回影响到的记录数
*/
function _affected_rows(){
global $_conn;
return mysqli_affected_rows($_conn);
}
/**
* _is_repeat():判断数据在数据库里是否已经存在
* @access public
* @param string $_sql sql操作语句
* @param string $_info 弹窗上显示的文字
* @return void
*/
function _is_repeat($_sql,$_info){
if(_fetch_array($_sql)){
_alert_back($_info);
}
}
/**
* _close():关闭数据库
* @access public
*/
function _close(){
global $_conn;
if(!mysqli_close($_conn)){
exit('数据库关闭异常'.mysqli_error($_conn));
}
}
?>
connect.php 实现数据库的连接功能
<?php
$_conn=mysqli_connect('localhost','root','');
if (!$_conn) {
exit('数据库连接失败:'.mysqli_error($_conn));
}
mysqli_select_db($_conn,'people')or die('找不到数据库:'.mysqli_error($_conn).mysqli_errno($_conn));
mysqli_query($_conn,"SET NAMES UTF8");
// var_dump($_conn);
include "sql.func.php";
?>
login.php实现登录响应操作
<?php
include "./connect.php";
//接收数据
if(isset($_POST['register']))
{
_location('欢迎注册','register.php');
}
if(isset($_POST['userid']) && isset($_POST['password'])){
//从数据库里查找用户名是否存在
$_sql = "SELECT user_id,user_password FROM user WHERE user_id='{$_POST['userid']}'";
$result = _fetch_array($_sql);
if(!empty($result[0])){
if($result[0]['user_password']==$_POST['password']){
_location('登录成功','https://www.cnblogs.com/cxl862002755/');
}else{
_alert('密码错误');
}
}else{
_alert('用户名不存在');
}
_close();
exit;
}
?>
register.php实现注册响应操作
<?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 = "INSERT INTO user(user_id,user_password)values('{$_POST['userid']}','{$_POST['password']}')";
$_result = _query($_sql);
_location("注册成功!","index.html");
_close();
exit;
}else
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册</title>
<STYLE type="text/css">
#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.imgtn.bdimg.com/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(http://images2.china.com/tech/zh_cn/news/product/891/20091209/2009120916491939987300.jpg);
background-size: cover;
}
</STYLE>
</head>
<body>
<div id="register">
<div id="form">
<form action="register.php" method="post">
<ul>
<li>用户名:<input type="text" name="userid"></li>
<li>密   码:<input type="password" name="password"></li>
<li style="list-style-type: none;">
<label for=""></label>
<input type="submit" value="注册">
<input type="submit" name="index" value="返回">
</li>
</ul>
</form>
</div>
</div>
</body>
</html>
index.html最终页面展示效果网页
<!DOCTYPE html>
<body>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录</title>
<STYLE type="text/css">
#login {
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.imgtn.bdimg.com/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(https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1593606498135&di=c7b87f8530b0f0c3b60f9eddf7ea7ad0&imgtype=0&src=http%3A%2F%2Fgss0.baidu.com%2F-4o3dSag_xI4khGko9WTAnF6hhy%2Fzhidao%2Fpic%2Fitem%2F7e3e6709c93d70cf0f2918ebfcdcd100bba12b00.jpg);
background-size: auto;
}
</STYLE>
</head>
<body>
<div id="login">
<div id="form">
<form action="login.php" method="post">
<fieldset>
<legend>用户登录</legend>
<ul>
<li>
<label>用户名:</label>
<input type="text" name="userid">
</li>
<li>
<label>密 码:</label>
<input type="password" name="password">
</li>
<li style="list-style-type: none;">
<label> </label>
<input type="submit" name="login" value="登录">
<input type="submit" name="register" value="注册">
</li>
</ul>
</fieldset>
</form>
</div>
</div>
</body>
</html>
</body>
部分效果图
个人总结:个人也是一知半解,还是需要去吃透这些代码,理解php响应的内涵要点,本人菜鸟,不喜勿喷