面向对象 登录与注册

文件布局

 

<!--index.php-->
<meta http-equiv="content-type" content="text/html" charset="utf-8">
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2015/8/26
* Time: 9:47
*/

//优化换成和跳转
ob_start();
?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>会员系统</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>

<?php
function __autoload($_className){
require $_className.'.class.php';
}

//实例化对象
if(isset($_GET['index'])) {
$_main=new Main($_GET['index']);
}else{
$_main=new Main();
}

//运行
$_main->_run();


?>

</body>
</html>


<!--Login.class.php-->
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2015/8/26
* Time: 14:26
*/

//登录类
class Login extends User {

public function __construct($_userName,$_passWord)
{
// TODO: Implement __construct() method.
$this->_username=$_userName;
$this->_password=$_passWord;
}

/**
*从xml文件里读取信息
*/
public function _query()
{
// TODO: Implement _query() method.
//载入xml文件
$_sxe=simplexml_load_file('user.xml');
if($this->_username==$_sxe->username&&$this->_password==$_sxe->password){
setcookie('username',$this->_username);
Tool::_alertLocation('登录成功!','?index=member');
}else{
Tool::_alertBack('登录失败!');
}

}

public function _check()
{
// TODO: Implement _check() method.
if( empty($this->_username)||
empty($this->_password)
){
return false;
}else{
return true;
}
}
}

?>


<!--login.inc.php-->
<h3>请登录您的账号</h3>

<div class="login">
<form method="post" action="">
<p>用 户 名:<input type="text" name="username"></p>
<p>密&nbsp;&nbsp;码:<input type="password" name="password"></p>
<p><input type="submit" name="send" value="登录"></p>
<p>[<a href="?">返回上一层</a>]</p>
</form>
</div>


<!--Main.class.php-->
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2015/8/26
* Time: 13:48
*/

//主类,控制界面载入,处理数据
class Main {
private $_index;
private $_send;

/**初始化数据
* @param string $_index
*/
public function __construct($_index='')
{
// TODO: Implement __construct() method.
$this->_index=$_index;
if(isset($_POST['send'])){
$this->_send=$_POST['send'];
}
}

/**总管
*
*/
public function _run(){
//处理数据
$this->_send();

//载入主界面面
include $this->_ui();
}

/**载入主页面的方法
* @return string
*/
private function _ui(){
if(empty($this->_index)||!file_exists($this->_index.'.inc.php')){
$this->_index='start';
}

return $this->_index.'.inc.php';
}

/**接收注册和登录发来的操作
* @return mixed
*/
private function _send(){
switch($this->_send){
case '注册':
$this->_exec(new Register($_POST['username'],$_POST['password'],$_POST['notpassword'],$_POST['email']));
break;
case '登录':
$this->_exec(new Login($_POST['username'],$_POST['password']));
break;
}
}

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

}

?>


<!--member.inc.php-->
<h3>欢迎您回来</h3>

<div class="start">
<p>欢迎您的归来,[<?php echo $_COOKIE['username']?>]</p>
</div>


<!--Register.class.php-->
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2015/8/26
* Time: 14:26
*/

//注册类
class Register extends User{

public function __construct($_userName,$_passWord,$_notPassWord,$_email)
{
// TODO: Implement __construct() method.
$this->_username=$_userName;
$this->_password=$_passWord;
$this->_notpassword=$_notPassWord;
$this->_email=$_email;
}


/**
*将信息注册到xml文件里
*/
public function _query()
{
// TODO: Implement _query() method.
$_xml=<<<_xml
<?xml version="1.0" encoding="UTF-8"?>
<user>
<username>$this->_username</username>
<password>$this->_password</password>
<email>$this->_email</email>
</user>
_xml;

//使用SimpleXMLElement创建模板
$_sxe=new SimpleXMLElement($_xml);
//生成xml文件
$_sxe->asXML('user.xml');
//跳转到登录页面
Tool::_alertLocation('恭喜你,注册成功!','?index=login');
}

public function _check()
{
// TODO: Implement _check() method.
if( empty($this->_username)||
empty($this->_password)||
empty($this->_notpassword)||
empty($this->_email)
){
return false;
}else{
return true;
}
}
}

?>


<!--register.inc.php-->
<h3>请注册您的信息</h3>

<div class="register">
<form method="post" action="">
<p>用 户 名:<input type="text" name="username"></p>
<p>密&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>


<!--start.inc.php-->
<h3>欢迎光临会员俱乐部</h3>

<div class="start">
<a href="index.php?index=login">登录</a>
<a href="index.php?index=register">注册</a>
</div>


<!--style.css-->
@CHARSET "UTF-8";
h3,.start,.register,.login{
margin: 20px;
text-align: center;
}



<!--Tool.class.php-->
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2015/8/26
* Time: 22:32
*/

//辅助类,里面都是放的静态方法,直接调用,无需实例化
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();
}

}

?>



<!--User.class.php-->
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2015/8/26
* Time: 14:21
*/

//抽象类规范字段和方法
abstract class User {
//成员字段
protected $_username;
protected $_password;
protected $_notpassword;
protected $_email;

//方法 注册和登录
abstract function _query();
abstract function _check();
}

?>



<!--user.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<user>
<username>小一</username>
<password>123456</password>
<email>925377458@qq.com</email>
</user>










转载于:https://www.cnblogs.com/xiaomingzaixian/p/4762052.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值