CakePHP身份验证

CakePHP身份验证,php,authentication,cakephp-2.0,Php,Authentication,Cakephp 2.0,我正在编写自己的CakePHP身份验证,它是使用我在另一个项目中使用的旧脚本启动的 此错误不断发生: 分析错误:语法错误,意外的T_变量,在第10行的/data/sites/web/ismartbe/subsites/cms.ismart.be/app/Controller/userscocontroller.php中需要T_函数 有人能解释我为什么老是抱怨这个错误吗? 错误必须在此块中: 分析错误:语法错误,意外的T_变量,在第10行的/data/sites/web/ismartbe/subs

我正在编写自己的CakePHP身份验证,它是使用我在另一个项目中使用的旧脚本启动的

此错误不断发生:

分析错误:语法错误,意外的T_变量,在第10行的/data/sites/web/ismartbe/subsites/cms.ismart.be/app/Controller/userscocontroller.php中需要T_函数

有人能解释我为什么老是抱怨这个错误吗? 错误必须在此块中:

分析错误:语法错误,意外的T_变量,在第10行的/data/sites/web/ismartbe/subsites/cms.ismart.be/app/Controller/userscocontroller.php中需要T_函数

另一方面,我希望你能告诉我,我是否需要创建一个组件来从我的控制器中获取一些逻辑,或者我的控制器看起来像现在一样好

这是我的密码:

类UsersController扩展了AppController{

public function beforeFilter() {
    parent::beforeFilter();
 }

$this->loadModel('Attempt');
$this->loadModel('Session');
$this->loadModel('Configuration',1);

/**
 * Settings to use when Auth needs to do a flash message with SessionComponent::setFlash().
 * Available keys are:
 *
 * - `element` - The element to use, defaults to 'default'.
 * - `key` - The key to use, defaults to 'auth'
 * - `params` - The array of additional params to use, defaults to array()
 *
 * @var array
 */
public $flash = array(
    'element' => 'default',
    'key' => 'authentication',
    'params' => array()
);

public function flash($message) {
    if ($message === false) {
        return;
    }
    $this->Session->setFlash($message, $this->flash['element'], $this->flash['params'], $this->flash['key']);
}



public function showusers() {

}

public function register()
{
    if ($this->request->isPost()) {
        if(!$this->Session->check('auth.session'))
        {
            $username =  $this->request->data['User']['username'];
            $email = $this->request->data['User']['email'];
            $this->request->data['User']['salt'] = $this->Authentication->__CreateSalt($username,$email);
            $this->request->data['User']['password'] = crypt($this->request->data['User']['plain_password'],$this->request->data['User']['salt']);
            if ($this->User->save($this->request->data)) {
                // Set a session flash message and redirect.
                $this->Session->setFlash('De registratie is succesvol verlopen. Activeer uw account door de link te volgen die in de activatiemail staat.');
                return $this->redirect(
                    array('controller' => 'users', 'action' => 'login')
                );
            }


        }
        else { $this->Session->setFlash('Je bent reeds aangemeld. Als jij niet '.$this->Session->check('auth.username').' bent gelieve dan af te melden.'); }
    }
    else {

    }
}




function login()
{
    if ($this->request->isPost()) {
        if(!$this->Session->check('auth.session'))
        {
            $attcount = $this->__getAttempt($_SERVER['REMOTE_ADDR']);

            if($attcount >= $this->Configuration->['max_attempts'])
            {
                flash('Je hebt het maximaal aantal pogingen (5) overschreden, wacht 15 minuten en probeer het opnieuw.');
                return false;
            }
            else 
            {

                    $username = $this->request->data['User']['username'];
                    $password = $this->request->data['User']['password'];
                    $user_data = $this->User->findbyUsername($username);
                    $password = crypt($password,$user_data['User']['salt']);
                    $count = count($user_data['User']);

                    if($count == 0)
                    {
                        // Username and / or password are incorrect
                        $this->__addAttempt($_SERVER['REMOTE_ADDR']);

                        $attcount = $attcount + 1;
                        $remaincount = $this->Configuration->['max_attempts'] - $attcount;


                    }
                    elseif (($count == 1) && ($password == $user_data['User']['password'])) 
                    {
                        // Username and password are correct

                        if($user_data['User']['active'] == "0")
                        {
                            // Account is not activated

                            flash('Je gebruikersaccount is nog niet geactiveerd. Gebruik de activatiemail om het account te valideren');
                            return false;
                        }
                        else
                        {
                            // Account is activated
                            $this->__newSession($username, $user_data['User']['id']);               
                            flash('U bent succesvol aangemeld, welkom!');
                            return true;
                        }
                    }
            }
        }
        else 
        {
            // User is already logged in
            flash('U bent reeds aangemeld.');
            return false;
        }
    }
}

private function __newSession($username,$user_id)
{
    $hash = md5(microtime());

    // Delete all previous sessions :
    $this->Session->deleteAll(array('Session.user_id' => $user_id), false);


    $ip = $_SERVER['REMOTE_ADDR'];
    $expiredate = date("Y-m-d H:i:s", strtotime($this->Configuration->['session_duration']));
    $expiretime = strtotime($expiredate);

    $this->Session->set(array(
        'ip' => $ip,
        'user_id' => $user_id,
        'expiredate' => $expiretime,
        'hash' => $hash,
        'hash' => $username
    ));
    $this->Session->save();

    $this->Session->write('auth.session', $hash); 
} 


//create custom salt
public function __CreateSalt($parameter1,$parameter2) {
    $parameter1 = md5($parameter1);
    $parameter2 = md5($parameter2);
    $unique = uniqid();
    $salt = $parameter1.$unique.$parameter2;
    return $salt;
}



    /*
* Adds a new attempt to database based on user's IP
* @param string $ip
*/

private function __addAttempt($ip)
{
       $attempts = $this->Attempt->findByIp($ip);


       $count = $attempts['Attempt']['count'];

       if($count == 0)
        {
            // No record of this IP in attempts table already exists, create new

            $attempt_expiredate = date("Y-m-d H:i:s", strtotime("+15 minutes"));
            $attempt_count = 1;


            $this->Attempt->set(array(
                'ip' => $ip,
                'count' => $attempt_count
            ));
            $this->Attempt->save();
        }
        else 
        {
            // IP Already exists in attempts table, add 1 to current count

            $attempt_expiredate = date("Y-m-d H:i:s", strtotime($this->Configuration->['security_duration']));

            $attempt_count = $count + 1;
            $this->Attempt->read(null, $attempts['Attempt']['id']);
            $this->Attempt->set(array(

                'ip' => $ip,
                'count' => $attempt_count,
                'expiredate' => $attempt_expiredate
            ));
            $this->Attempt->save();


        }
}

/*
* Provides amount of attempts already in database based on user's IP
* @param string $ip
* @return int $attempt_count
*/

private function __getAttempt($ip)
{
    $attempts = $this->Attempt->findByIp($ip);
    $attempt_count = $attempts['Attempt']['count'];
    return $attempt_count;


}

/*
* Function used to remove expired attempt logs from database (Recommended as Cron Job)
*/

private function __expireAttempt()
{
/*
    $query = $this->mysqli->prepare("SELECT ip, expiredate FROM attempts");
    $query->bind_result($ip, $expiredate);
    $query->execute();
    $query->store_result();
    $count = $query->num_rows;

    $curr_time = strtotime(date("Y-m-d H:i:s"));

    if($count != 0)
    {
        while($query->fetch())
        {
            $attempt_expiredate = strtotime($expiredate);

            if($attempt_expiredate <= $curr_time)
            {
                $query2 = $this->mysqli->prepare("DELETE FROM attempts WHERE ip = ?");
                $query2->bind_param("s", $ip);
                $query2->execute();
                $query2->close();
            }
        }
    }*/
}

}给你一条鱼:在PHP中,你不能在方法之外调用函数。因此,如果您希望在每个控制器的操作中加载这样的模型,则可能的修复方法是:

public function beforeFilter() {
    parent::beforeFilter();

    $this->loadModel('Attempt');
    $this->loadModel('Session');
    $this->loadModel('Configuration',1);
}
对于不希望预加载特定行的模型,另一个更好的方法是使用cake控制器::$uses属性加载模型

class UsersController extends AppController {
    public $uses = array(
        'Attempt',
        'Session'
    );
}
更好地学习钓鱼:访问PHP手册并阅读更多关于钓鱼的信息。以及控制器属性。希望能有帮助