我是 Kohana 的菜鸟,正在尝试使用 Auth ORM 实现登录功能。以下是我编写的代码:
/classes/controller/admin.php
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Admin extends Controller_Default
{
public function action_index()
{
// Enter a new user manually
$user = ORM::factory('admin');
$user->username = 'admin';
$user->password = 'password';
$user->save();
// Login with this user
$success = Auth::instance()->login('admin','password','admin');
if ($success){
echo "Welcome !";
}else{
echo "Not welcome...";
}
}
}
/classes/models/admin.php
<?php defined('SYSPATH') or die('No direct access allowed.');
class Model_Admin extends ORM {
public function save(Validation $validation = NULL)
{
$this->salt = uniqid();
$this->password = Auth::instance()->hash($this->password, $this->salt);
$this->created = date('Y-m-d');
parent::save($validation);
}
}
引导程序.php
Kohana::modules(array(
'auth' => MODPATH.'auth', // Basic authentication
// 'cache' => MODPATH.'cache', auth // Caching with multiple backends
// 'codebench' => MODPATH.'codebench', // Benchmarking tool
'database' => MODPATH.'database', // Database access
// 'image' => MODPATH.'image', // Image manipulation
'orm' => MODPATH.'orm', // Object Relationship Mapping
// 'unittest' => MODPATH.'unittest', // Unit testing
// 'userguide' => MODPATH.'userguide', // User guide and API documentation
));
Cookie::$salt = 'somerandomstring';
数据库表结构如下:id=>主键用户名密码salt created
我有
而且我每次都会收到“不欢迎......”的消息。我不确定我哪里出错了。