这是我第一次创建课程。
我创建了一个用于登录,注册和注销的User类。
它似乎在起作用,但我确信我做错了什么。例如,当我只使用login()方法时,为什么我必须提供我在___construct中创建的所有变量。登录我只需要用户名和密码。
我在模板系统中使用$ _ERROR变量显示错误。如何在课堂上显示错误。我使用了抛出新的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 );
}
?>
答案 0 :(得分:1)
您使用的PDO总是一个优点,但问题是您没有绑定您的参数。 PDO的最大优点实际上是绑定参数。在您的示例中:
$sql->bindParam(':userid',$UserID);
$sql->bindParam(':provider',$Provider);
依此类推...............
第二部分让我害怕并且如果你读到密码安全性应该吓到你这就是你使用md5作为你的密码而只是一个名为$ characters的随机密钥字符串你刚刚连接?先生......是......只是......愚蠢(抱歉直言不讳但实际上这是一个大问题,可能会导致你的客户/用户非常严重的麻烦)。
在此尝试此Bcrypt Library。这是2013年你甚至不应该使用md5即使用盐,甚至使用你正在这样做的方法:
$Characters = 'PEcN0';
$string = md5( $Password . $Characters );
return $string;
相信我远离构建自己的“加密/散列”方法并使用社区可信方法。
同样回到PDO部分,不要忘记将其添加到您的属性中:
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE);
我希望你从这篇文章中学到一些东西。如果没有bindParam,您不应该使用PDO,并且您不应该使用MD5,无论是否使用盐。 Bcrypt Bcrypt Bcrypt Bcrypt Bcrypt。