简单的bbs登录系统

开发bbs登录系统

首先,我们需要知道开发系统中我们需要哪些文件和组成部分;废话不多说直接上手开发的图:

上面就是分析图。下面我们直接开始代码:

1.bbstest.sql  添加数据库

set default_storage_engine = InnoDB;
set character_set_client = utf8;
set character_set_connection = utf8;
set character_set_database = utf8;
set character_set_results = utf8;
set character_set_server = utf8;

create database bbstest;

use bbstest;

create table users(
username varchar(50) not NULL,
password varchar(30) not NULL,
email varchar(50) not NULL
);

insert into users(username,password,email) values ('admin1','123','[email protected]');

2、ConnectionDB.class.php 主要的文件(数据库类)

<?php
class ConnectionDB{
private $hostname = '127.0.0.1';
private $username = 'root';
private $password = 'root';//数据库密码
private $dbname = 'bbstest';

private $bbsname;
private $bbspassword;
private $email;

public $regArray;
public $link;
public $result3;
public $checkArray;

public function __construct($username1,$password1,$email1){//构造方法
$this->bbsname = $username1;
$this->passwordReg = $password1;
$this->email = $email1;
}

public function run(){


$this->_connectionDB();
$this->selectDB();
}

public function _connectionDB(){
$this->hostname;
$this->username;
$this->password;
$this->dbname;
$this->link = mysqli_connect("$this->hostname","$this->username","$this->password","$this->dbname");// 用的mysqli写的; 连接数据库
if (isset($this->link)) {//判断有没有连上数据库
echo "Mysql connect successful!";
}else{
echo "error!";
}

}

public function insertDB(){//对数据库中写数据,主要用在reg.class.php中 对用户的收集
$str1 = "insert into users(username,password,email) values('$this->bbsname','$this->passwordReg','$this->email')";
$result1 = mysqli_query($this->link,$str1);//将用户信息写入数据中
if (isset($result1)) {//判断
echo "successful";
}
}

public function selectDB(){ //查询数据库
//主要对Login.class.php中进行与数据中的信息进行比对

$str2 = "select username,password from users where username='$this->bbsname'";
$result2 = mysqli_query($this->link,$str2);
$this->regArray = mysqli_fetch_array($result2);

}

public function checkDB(){
$str3 = "select username from users where username='$this->bbsname'";
$this->result3 = mysqli_query($this->link,$str3);
$this->checkArray = mysqli_fetch_array($this->result3);
}

public function closeDB(){
mysqli_close($this->link);
}
}

?>

3、登录页面类(Login.class.php)

<?php


class Login extends User{
//写一个构造方法来接受表单的值;
public function __construct($_userName,$_passWord){
$this->_username = $_userName;
$this->_password = $_passWord;
}
//将信息注册到XML中
public function _query(){
$conn = new ConnectionDB($_POST['username'],$_POST['password'],' ');//
$conn->run();
//$conn->_connectionDB ;
//$conn->selectDB();


if($this->_username == $conn->regArray['username']&& $this->_password == $conn->regArray['password']){
setcookie('username', $this->_username);
echo '登陆成功';
Tool::_alertLocation($this->_username.'.欢迎你回来','?index=Member');
}else{

Tool::_alertBack('登录失败');
}
$conn->closeDB();//每次开启过后都需要关闭数据库;
}
public function _check(){
if (empty($this->_username)||empty($this->_password)){
return false;
}else{
return true;
}
}

}
?>

4、注册页面类(Reg.class.php)

<?php
class Reg extends User{
//public function _DB()连接数据库
//写一个构造方法来接收表单的值

public function __construct($_userName,$_passWord,$_notPassword,$_Email){
$this->_username = $_userName;
$this->_password = $_passWord;
$this->_notpassword = $_notPassword;
$this->_email = $_Email;
}

public function _query(){
$email = '/([\w\.\_]{2,10})@(\w{1,}).([a-z]{2,4}$)/';
if (preg_match($email, $this->_email)) {

if($this->_password == $this->_notpassword){
$connReg = new ConnectionDB($_POST['username'],$_POST['password'],$_POST['email']);
$connReg->_connectionDB();
$connReg->checkDB();
if($this->_username==$connReg->checkArray['username']){
Tool::_alertLocation('该用户名已被注册,请重试!','?index=reg');
}else{
$connReg->insertDB();
}
$connReg->closeDB();

Tool::_alertLocation('恭喜你,注册成功!','?index=login');


}else{
Tool::_alertLocation('两次输入的密码不一致!');
}
}else{
Tool::_alertLocation('输入的电子邮件不合法!');
}

}
//给注册做验证
public function _check(){
if (empty($this->_username)||empty($this->_password)||empty($this->_notpassword)||empty($this->_email)){
return false;
}else{
return true;

}

}

}
?>

5、登录页面(login.php)

<!DOCTYPE html>
<html>
<head>
<title>登录页面</title>
</head>
<body>
<h3>请登录你的账号</h3>
<div class="login">
<form method="post" action="index.php">
<p>用户名:<input type="text" name="username" /></p>
<p>密&nbsp;&nbsp;&nbsp;&nbsp;码:<input type="password" name="password" /></p>
<p><input type="submit" name="send" value="登录" /></p>
<p><a href="?">[返回上一层]</a></p>
</form>
</div>


</body>
</html>

6、注册页面

<!DOCTYPE html>
<html>
<head>
<title>注册页面</title>
</head>
<body>
<h3>请注册您的个人信息</h3>
<div class="reg">
<form method="post" action="index.php">
<P>用户名:<input type="text" name="username" /></P>
<p>密&nbsp;&nbsp;&nbsp;&nbsp;码:<input type="password" name="password" /></p>
<p>密码确认:<input type="password" name="notpassword" /></p>
<p>电子邮件:<input type="text" name="email" /></p>
<p><input type="submit" name="send" value="注册" /></p>
<P><a href="?">[返回上一层]</a></P>
</form>
</div>

</body>
</html>

7、入口文件(index.php)

<?php ob_start();//解决缓存问题,跳转到cookie生成 ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>会员系统</title>
<link rel="stylesheet" type="text/css" href="sty.css">
</head>
<body>
<?php
//魔法方法 __autoload()
function __autoload($_classname){
require $_classname.'.class.php';

}

if (isset($_GET['index'])){
$_main = new Main($_GET['index']);
}else{
$_main = new Main();//可以为空 null Mian()括号可以为空
}

/* if (empty($_GET['index'])) {
$_main = new Mian($_GET['index']);
elseif (empty($_GET['index']!=)) {
# code...
}
*/
// $_main->_ui();
// $_main->_send();
//为了安全 上面的可以修改一下;因为把另两个方法设置私有的;
$_main->_run();
?>
</body>
</html>

8、辅助工具类(Tool.class.php)

<?php
//辅助工具类
final class Tool{
//弹出一个信息,然后跳转到指定的页面
static public function _alertLocation($_info,$_url){
echo "<script type='text/javascript'>alert('$_info');location.href='$_url';</script>";
exit();
}
//弹窗返回之前
static public function _alertBack($_info){
echo "<script type='text/javascript'>alert('$_info');history.back();</script>";
exit();

}
}

?>

9、抽象类(User.class.php)

<?php
//用户这个类用抽象类 制定一种规范
abstract class User{
protected $user='user.xml';
protected $_username;
protected $_password;
protected $_notpassword;
protected $_email;

// 第一个方法 登录或者注册
abstract function _query();

//第二个方法 实验验证 注册 登录 都需要认证,
//可以用正则 , js脚本(不会用) php一些函数

abstract function _check();
}

?>

10、页面切换类(Main.class.php)

<?php
class Main{

private $_index;
private $_send;

//reg login null
//主类
//1 控住不同页面的切换
//2 处理不同的数据 如 input表单 注册|登录 两个表单提交的数据
public function __construct($_index=''){//构造方法
$this->_index = $_index; //主要有reg 和 login 传进来 还有一个null
//echo $this->_index;
if (isset($_POST['send'])){
$this->_send = $_POST['send'];
}

}
public function _run(){
//处理数据
$this->_send();
//如果注释了,xml文件就不会生成

include $this->_ui();
}


private function _ui(){
if(empty($this->_index)||!file_exists($this->_index.'.php')){//_index 将会有两个reg或者login 传进来 否则index将传入start
$this->_index='start';
}
return $this->_index.'.php';
}
// ui 方法 切换到两个页面 登录 注册
/*private function _ui(){
if(empty($this->_index)){
include_once 'start.php';
}else{
include_once $this->_index.'.php';
}
}
*/

private function _send(){
switch ($this->_send){
case '注册':
//$_reg = new Reg();
//$this->_exec($_reg);
$this->_exec(new Reg($_POST['username'],$_POST['password'],$_POST['notpassword'],$_POST['email']));
break;
case '登录':


//$_login = new Login();
//$this->_exec($_login);
$this->_exec(new Login($_POST['username'],$_POST['password']));

break;
}

}
//创建一个执行的方法,里面传入一个参数,是Reg或者Login类对象引用
private function _exec($_class){
if ($_class->_check()){
$_class->_query();
}else{
Tool::_alertBack('字段不能为空!');
}
}
}
?>

11.跳转页面控制(Member.php)

<!DOCTYPE html>
<html>
<head>
<img src="./images/1.jpg" width="165" height="160" />
<title></title>
</head>
<body>
<style>


</style>
<h3>欢迎您回来</h3>
<div class = "start">
<p>欢迎您归来,[<?php echo $_COOKIE['username'] ?>]</p>
</div>
</body>
</html><!---body{background: url(./images/1.jpg);}

12、注册失败跳转页面(start.php)

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
</head>
<body>
<h3>欢迎光临会员俱乐部</h3>
<div class="start">
<a href="index.php?index=login">登录</a>
<a href="index.php?index=reg">注册</a>
</div>
</body>
</html>

13、css修饰(sty.css)

h3,div.start,div.reg,div.login{
margin: 20px;
text-align: center;
}

以上就是开发登录系统主要内容,我还是个菜鸡多多包涵,很多都是百度大佬们的写法;才写出来的,希望大家多多提建议,帮忙美化一下!

若不懂请及时留言!

猜你喜欢

转载自www.cnblogs.com/hcrk/p/10788354.html