我尝试添加一种方法来检查用户帐户是否已激活,然后再登录。我已经添加了一种发送带有激活链接的电子邮件的方法,如果他们点击该链接,激活那里帐户。但是,当我尝试添加一种方法来检查用户帐户是否已在登录类中激活时,它无法正常工作。
这是登录类:
public function login($username = null, $password = null, $remember = false) {
$validate = new Validate();
if (!$username && !$password && $this->exists()) {
Session::put($this->_sessionName, $this->data()->id);
} else {
$user = $this->find($username);
if ($user) {
if ($this->data()->password === Hash::make($password, $this->data()->salt)) {
if ($this->data()->activated = 1) {
Session::put($this->_sessionName, $this->data()->id);
if ($remember) {
$hash = Hash::unique();
$hashCheck = $this->_db->get('users_session', array('user_id', '=', $this->data()->id));
if (!$hashCheck->count()) {
$this->_db->insert('users_session', array(
'user_id' => $this->data()->id,
'hash' => $hash
));
} else {
$hash = $hashCheck->first()->hash;
}
Cookie::put($this->_cookieName, $hash, Config::get('remember/cookie_expiry'));
}
} else {
$validate->addError('You need to activate your account before you login!');
}
return true;
} else {
$validate->addError('Invalid password!');
}
} else {
$validate->addError('That username does not exist!');
}
}
return false;
}
当我使用此代码^^即使帐户被激活,我也可以登录。如果我做
if ($this->data()->activated == 1) {
相反,只有激活的帐户才能登录,但是,错误消息(您需要在登录前激活您的帐户!,密码无效!等)不会显示,如果我尝试使用非活动帐户登录我被重定向到index.php但我不应该被重定向并且应该弹出错误。如果我做
if ($this->data()->activated === 1) {
我无法使用任何帐户登录,即使有些帐户被激活,我也会被重定向到index.php,但如前所述,我再也不会被重定向。
在数据库中,我有一个名为activated的字段,它是一个值为0或1的枚举。如果为0,则帐户不会被激活,如果它被激活,则
另外,我使用了phpacademy的oop登录/注册系统。这是我的第一个使用PDO和OOP的网站,所以我并不熟悉这种语言。
我该怎么做才能解决这个问题?有想法的人吗?
如果你们需要任何帮助我的东西,请告诉我。
这里是Session.php:
<?php
class Session {
public static function exists($name) {
return (isset($_SESSION[$name])) ? true : false;
}
public static function put($name, $value) {
return $_SESSION[$name] = $value;
}
public static function get($name) {
return $_SESSION[$name];
}
public static function delete($name) {
if (self::exists($name)) {
unset($_SESSION[$name]);
}
}
public static function flash($name, $string = '') {
if (self::exists($name)) {
$session = self::get($name);
self::delete($name);
return $session;
} else {
self::put($name, $string);
$session = self::get($name);
return $session;
}
}
}
和Validate.php:
<?php
class Validate {
private $_passed = false,
$_errors = array(),
$_db = null;
public function __construct() {
$this->_db = DB::getInstance();
}
public function check($source, $items = array()) {
foreach($items as $item => $rules){
$value = trim($source[$item]);
$alias = $item;
foreach($rules as $rule => $rule_value) {
if(isset($rules['alias'])){
$alias = $rules['alias'];
}
$alias = escape(ucfirst($alias));
if($rule === 'required' && empty($value)) {
$this->addError($alias.' is required.');
} else if(!empty($value)) {
switch($rule) {
case 'min':
if(strlen($value) < $rule_value) {
$this->addError($alias." must be a minimum of {$rule_value} characters.");
}
break;
case 'max':
if(strlen($value) > $rule_value) {
$this->addError($alias." can only be a maximum of {$rule_value} characters.");
}
break;
case 'matches':
if($value != $source[$rule_value]) {
$this->addError(ucfirst($rule_value).' must match '.strtolower($alias).'.');
}
break;
case 'unique':
$check = $this->_db->get($rule_value, array($item, '=', $value));
if($check->count()) {
$this->addError($alias.' has already been taken.');
}
break;
case 'first_not_numeric':
if (is_numeric($value[0])) {
$this->addError($alias.' cannot begin with a number.');
}
break;
case 'email';
if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false){
$this->addError("Email not valid");
}
break;
case 'email_exists';
if($users->email_exists($_POST['email']) === true) {
$this->addError("Email already exists!");
}
break;
}
}
}
}
if(empty($this->_errors)) {
$this->_passed = true;
}
return $this;
}
public function addError($error) {
$this->_errors[] = $error;
}
public function errors() {
return $this->_errors;
}
public function passed() {
return $this->_passed;
}
}
的login.php:
<?php
require_once 'core/init.php';
$user = new User();
if ($user->isLoggedIn()) {
Redirect::to('index.php');
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Log in</title>
<link rel="icon" href="favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="style/global.css">
<script src="js/jQuery.js"></script>
<script src="js/main.js"></script>
</head>
<body>
<input type="checkbox" id="sidebartoggler">
<input type="checkbox" id="profiletoggler">
<div class="page-wrap">
<?php require_once 'includes/header.php'; ?>
<?php require_once 'includes/navbar.php'; ?>
<?php require_once 'includes/profilebar.php'; ?>
<div class="page-content">
<form action="" method="post">
<h1>Log in</h1>
<div class="inset">
<?php
if (input::exists()) {
if (Token::check(Input::get('token'))) {
$validate = new Validate();
$validation = $validate->check($_POST, array(
'username' => array(
'required' => true,
'alias' => 'Username'
),
'password' => array(
'required' => true,
'alias' => 'Password'
)
));
if ($validation->passed()) {
$user = new User();
$remember = (Input::get('remember') === 'on') ? true : false;
$login = $user->login(Input::get('username'), Input::get('password'), $remember);
if ($login) {
Redirect::to('index.php');
} else {
foreach ($validation->errors() as $error) {
echo '<i class="fa fa-exclamation-triangle"></i> ', $error, '<br>';
echo '<br />';
//echo '<pre>', print_r($validation->errors()), '</pre>';
}
}
} else {
foreach ($validation->errors() as $error) {
echo '<i class="fa fa-exclamation-triangle"></i> ', $error, '<br>';
echo '<br />';
//echo '<pre>', print_r($validation->errors()), '</pre>';
}
}
}
}
if(Session::exists('login')) {
echo '<p><i class="fa fa-exclamation-triangle"></i> ' . Session::flash('login') . '</p><br />';
}
?>
<p>
<label for="username">USERNAME</label>
<input type="text" name="username" id="username" value="<?php echo escape(Input::get('username')); ?>">
</p>
<!--<?php
/*if(isset($validation) && !$validation->passed()){
foreach($validation->errors() as $error){
if ($error->alias = 'Username') {
echo '<i class="fa fa-exclamation-triangle"></i> ', $error, '<br>';
echo '<br />';
}
}
}*/
?>-->
<p>
<label for="password">PASSWORD</label>
<input type="password" name="password" id="password">
</p>
<p>
<input type="checkbox" name="remember" id="remember">
<label for="remember">Remember me for 1 month</label>
</p>
</div>
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
<p class="p-container">
<span>Forgot password?</span>
<input type="submit" name="go" id="go" value="Log in">
</p>
</form>
</div>
<?php require_once 'includes/footer.php'; ?>
</div>
</body>
</html>
在登录类之上,我有这个:
private $_db,
$_data,
$_sessionName,
$_cookieName,
$_isLoggedIn;
在登录类的下面我有这个:
public function data() {
return $this->_data;
}
public function isLoggedIn() {
return $this->_isLoggedIn;
}
提前致谢,
Busarna
答案 0 :(得分:2)
我可能错了,但ENUM字段被视为字符串。您可能希望将$this->data()->activated
代码与intval()
包装起来,将其强制转换为您尝试严格检查的整数。
if (1 === intval($this->data()->activated)) { ... }
在您的代码中:$this->data()->activated = 1
是一个错误 - 您正在尝试执行作业。
最后,您需要将return true;
移至if ($remember) { ... }
块之后。即使您遇到未被激活的错误条件,您也总是返回一个真实的陈述。
发布login.php文件后,我注意到您在$validate
方法中创建了一个新的$user->login
变量,但是没有返回它以在login.php脚本中使用。尝试删除该行:
$validate = new Validate();
来自登录功能(不 login.php文件),当您调用$ user-&gt;登录功能时,将$ validate变量作为参考传递。
public function login($username = null, $password = null, $remember = false, &$validate = null) { ... }
在login.php中调用它:
$login = $user->login(Input::get('username'), Input::get('password'), $remember, $validate);
答案 1 :(得分:1)
我真的没有找到这个功能干净,如果我是你,我会拆分成另一个功能只是为了检查用户是否是actif,并且只有在你确定它是活动的时才运行登录过程。
<强>功能强>
public function user_is_active($username, $password){
$is_active = ($this->data()->activated === 1);
return $is_active;
}
并检查函数的返回,以便在返回false时添加错误
$validate = new Validate();
$user_is_found = $this->find($username);
if(!$user_is_found){
$validate->addError('username not found');
}else{
$is_active = user_is_active($username);
if(!$is_active){
$validate->addError('user is not active');
}else{
$isLoggedIn = login($username, $password, true);
if(!$isLoggedIn){
$validate->addError('password is not valid');
}else{
//login succesful redirect here
}
}
}
所以只需设法在登录逻辑中返回true或false。
最后检查是否有错误:
$errors = $validate->errors();
if(count($errors)>0){
var_dump($errors);
}
答案 2 :(得分:0)
使用
if ($this->data()->activated == 1) {
并改变这个......
}
} else {
$validate->addError('You need to activate your account before you login!');
}
return true;
对此...
}
return true;
} else {
$validate->addError('You need to activate your account before you login!');
}