第五章 使用OOP注册会员

index.php

<?php 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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<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
// 登录类
class Login extends User {
	// 写一个构造方法来接收表单的值
	public function __construct($_userName, $_passWord) {
		$this->_username = $_userName;
		$this->_password = $_passWord;
	}
	// 从xml里读出信息
	public function _query() {
		// 载入xml文件
		$_sxe = simplexml_load_file ( 'user.xml' );
		if ($this->_username == $_sxe->username && $this->_password == $_sxe->password) {
			// 生成一个cookies
			setcookie ( 'user', $this->_username );
			Tool::_alertLocation ( $this->_username . ',欢迎您回来!', '?index=member' );
		} else {
			Tool::_alertBack ( '登录失败!' );
		}
	}
	public function _check() {
		if (empty ( $this->_username ) || empty ( $this->_password )) {
			return false;
		}
		return true;
	}
}
?>

login.inc.php

<h3>请登录您的账号</h3>
<div class="login">
	<form method="post" action="">
		<p>用 户 名:<input type="text" name="username" /></p>
		<p>密  码:<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
// 主类,控制界面载入,处理数据
class Main {
	private $_index;
	private $_send;
	// 构造方法,用来初始化数据
	public function __construct($_index = '') {
		$this->_index = $_index;
		if (isset ( $_POST ['send'] )) {
			$this->_send = $_POST ['send'];
		}
	}
	// 总管
	public function _run() {
		// 处理数据
		$this->_send ();
		// 载入界面
		include $this->_ui ();
	}
	// 创建一个载入界面的方法
	// 这个方法,我想得到login.inc.php这个字符串
	private function _ui() {
		if (empty ( $this->_index ) || ! file_exists ( $this->_index . '.inc.php' )) {
			$this->_index = 'start';
		}
		return $this->_index . '.inc.php';
	}
	// 创建一个方法来接收登录和注册发送的操作
	private function _send() {
		switch ($this->_send) {
			case '注册' :
				$this->_exec ( new Reg ( $_POST ['username'], $_POST ['password'], $_POST ['notpassword'], $_POST ['email'] ) );
				break;
			case '登录' :
				$this->_exec ( new Login ( $_POST ['username'], $_POST ['password'] ) );
				break;
		}
	}
	// 创建一个执行的方法,里面传一个参数,是Reg或者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['user']?>]</p>
</div>

Reg.class.php

<?php
// 注册类
class Reg extends User {
	// 写一个构造方法来接收表单的值
	public function __construct($_userName, $_passWord, $_notPassWord, $_eMail) {
		$this->_username = $_userName;
		$this->_password = $_passWord;
		$this->_notpassword = $_notPassWord;
		$this->_email = $_eMail;
	}
	// 将信息注册到xml里
	public function _query() {
		// xml字符串
		$_xml = <<<_xml
<?xml version="1.0" encoding="utf-8"?>
<user>
	<username>$this->_username</username>
	<password>$this->_password</password>
	<email>$this->_email</email>
</user>
_xml;
		// 创建simplexml类
		$_sxe = new SimpleXMLElement ( $_xml );
		// 生成xml
		$_sxe->asXML ( 'user.xml' );
		// 跳转到login.inc.php页面
		Tool::_alertLocation ( '恭喜你,注册成功!', '?index=login' );
	}
	// 给注册做验证
	public function _check() {
		if (empty ( $this->_username ) || empty ( $this->_password ) || empty ( $this->_notpassword ) || empty ( $this->_email )) {
			return false;
		}
		return true;
	}
}
?>

reg.inc.php

<h3>请注册您的个人信息</h3>
<div class="reg">
	<form method="post" action="">
		<p>用 户 名:<input type="text" name="username" /></p>
		<p>密  码:<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>欢迎光临onestopweb博客</h3>
<div class="start">
	<a href="index.php?index=login">登录</a> | 
	<a href="index.php?index=reg">注册</a>
</div>

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 ();
	}
}
?>

User.class.php

<?php
// 这个用户类,规范子类的字段和方法
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>onestopweb</username>
	<password>123456</password>
	<email>[email protected]</email>
</user>

效果图:

 

猜你喜欢

转载自onestopweb.iteye.com/blog/2327813
今日推荐