这是我第一次尝试创建一个类。
我创建了一个用户类,它可以登录、注册和注销。
似乎它正在工作,但我确定我做错了什么。例如,当我只使用 login() 方法时,为什么我必须给出我在 ___construct 中创建的所有变量。登录我只需要用户名和密码。
我在模板系统中使用 $_ERROR 变量显示错误。如何在课堂上显示错误。我用 throw new ExampleException
谁能复习我的课并告诉我我犯的错误吗?我只是想学习如何使用类...
这是我的课
<?php
class User
{
var $Username = '';
var $Email = '';
var $Password = '';
var $Salt = '';
protected $_username;
protected $_email;
protected $_password;
protected $_salt;
protected $_db;
protected $_user;
public function __construct(PDO $db, $Username, $Email, $Password, $Salt)
{
$this->_db = $db;
$this->_username = $Username;
$this->_email = $Email;
$this->_password = $Password;
$this->_salt = $Salt;
}
private function saltString($Password) // Creates random string for password
{
$Characters = 'PEcN0';
$string = md5( $Password . $Characters );
return $string;
}
public function login() // checks if CheckCredentials is true. if it's true it creates sessions
{
$user = $this->_CheckCredentials();
if($user){
$this->_user = $user;
$_SESSION['UserID'] = $user['UserID'];
$_SESSION['Username'] = $user['Username'];
$_SESSION['Level'] = $user['Level'];
$_SESSION['Reputation'] = $user['Reputation'];
return $user['UserID'];
}
return false;
}
public function register() // checks if addUser is true.
{
$user = $this->_addUser();
if($user){
$this->createSession($user);
}
return false;
}
public function _CheckCredentials() // checks password and username for login method
{
$sql = $this->_db->query("SELECT * FROM users WHERE Username = '" . $this->_username . "'");
if($sql->rowCount() > 0){
$user = $sql->fetch(PDO::FETCH_ASSOC);
$pass = $this->saltString($this->_password);
if($pass == $user['Password']){
$sql = $this->_db->query( "UPDATE users SET LastLogin = NOW() WHERE UserID = '" . $user['UserID'] . "'" );
return $user;
}
}
return false;
}
public function _addUser() // add user into database
{
$sql = $this->_db->query( "SELECT * FROM users WHERE Username = '" . $this->_username . "' OR Email = '" . $this->_email . "'" );
if($sql->rowCount()){
$user = $sql->fetch(PDO::FETCH_ASSOC);
if($user['Username'] == $this->_username){
throw new ExampleException("This username was taken.");
}else{
throw new ExampleException("This e-mail address is being used.");
}
}else{
$intert = $sql->query( "INSERT INTO users ( UserID, OauthUid, OauthProvider, Username, Password, Email, CookieKey, Level, Registered, LastLogin, Reputation, Contents )
VALUES ( NULL, '". $UserID ."', '". $Provider ."', '" . $Username . "', '" . $Password . "', '" . $Email . "', '" . generateKey( 5 ) . "', '1', NOW(), NOW(), '0', '0')" );
$UserID = $sql->lastInsertId();
if($instert){
$_SESSION['UserID'] = $UserID;
$_SESSION['Username'] = $Username;
$_SESSION['Level'] = '1';
}
}
return false;
}
public function getUser()
{
return $this->_username;
}
}
?>
这就是我使用它的方式......
<?php
if ( isset( $_POST['Username'] ) )
{
$Username = trim( $_POST['Username'] );
$Password = trim( $_POST['Password'] );
if ( !preg_match( '/^[a-z0-9_]+$/i', $Username ) )
$_ERROR = 'bad username!';
else
{
$UserClass = new User($dbc, $Username, $Email, $Password, $Salt);
if($userid = $UserClass->login())
{
if ( isset( $_POST['Remember'] ) )
{
setcookie( 'UserID', $data['UserID'], time() + 31536000, '/', SITE );
setcookie( 'PassHash', md5( $data['Password'] . $data['CookieKey'] ), time() + 31536000, '/', SITE );
}
header( "Location: " . ( $_POST['Redirect'] != '' ? base64_decode( $_POST['Redirect'] ): '/index.php' ) );
die();
}
else
$_ERROR = "Check your username or password!";
$_INFO .= $_SESSION['UserID'];
}
}else{
$Reff = '';
if ( isset( $_SERVER['HTTP_REFERER'] ) )
$Reff = base64_encode( $_SERVER['HTTP_REFERER'] );
$_INFO .= $_SESSION['UserID'];
$_PAGE .= t_LoginForm( $Reff );
}
?>