class Auth_Controller extends Controller {
public function __construct()
{
parent::__construct();
// Load some libraries
//foreach(array('profiler', 'auth', 'session') as $lib)
//{
// $class = ucfirst($lib);
// $this->$lib = new $class();
//}
}
function index()
{
// Display the install page
echo new View('auth/install');
}
function create()
{
header('Content-type: text/html; charset=gb2312');
if(! empty($_POST))//$this->input->post()
{
$this->validation->set_rules(array
(
// Format:
// key friendly name, validation rules
'email' => array('电子邮件', '=trim|required[6,127]|valid_email_rfc'),
'username' => array('用户名', '=trim|required[4,32]'),
'password' => array('密码', '=trim|required[4,50]'),
));
if ($this->validation->run())
{
// Create new user
$user = new User_Model;
if ( ! $user->username_exists($this->input->post('username')))
{
foreach($this->input->post() as $key => $val)
{
// Set user data
$user->$key = $val;
}
if ($user->save() AND $user->add_role('login'))
{
// Redirect to the login page
url::redirect('auth/login');
}
}
}
else
{
$this->validation->error_format("<li>{message}</li>");
?>
<ul>
<?php echo $this->validation->error_string ?>
</ul>
<?php
}
}//有数据
print form
:pen
('auth/create', array('id'=>'create_form'));
print form
::label('input_email', 'Email Address: ');
print form
::input('email', $this->input->post('email'));
print form
::label('input_name', 'User Name: ');
print form
::input('username', $this->input->post('username'));
print form
::label('input_password', 'Password: ');
print form
::password("password",$this->input->post('password'));
print form
::submit('submit', '提交');
print form
::close();
}
function login
()
{
header('Content-type: text/html; charset=gb2312');
if ( ! $this->session->get('user_id')) //没有登录
{
if(! empty($_POST))
{
$this->validation->set_rules(array
(
// Format:
// key friendly name, validation rules
'username' => array('用户名', '=trim|required[4,32]'),
'password' => array('密码', '=trim|required[4,50]'),
));
if ($this->validation->run())
{
// Load the user
$user = new User_Model
($this->input->post('username'));
// Attempt a login
if ($this->auth->login($user, $this->input->post('password')))
{
echo "<h4>Login Success!</h4>";
echo "<p>Your roles are:</p>";
echo Kohana
::debug($this->session->get('roles'));
}
else
{
echo "<h4>Login Failed!</h4>";
}
//return;
}
else
{
$this->validation->error_format("{message}<br/>");
echo $this->validation->error_string;
}
}//没有收到数据
print form
:pen
('auth/login', array('id'=>'login_form'));
print form
::label('input_name', 'User Name: ');
print form
::input('username', $this->input->post('username'));
print form
::label('input_password', 'Password: ');
print form
::password("password",$this->input->post('password'));
print form
::submit('submit', '登录');
print form
::close();
}//没有登录
if ($this->session->get('user_id'))
{
print form
:pen
('auth/logout', array('id'=>'logout_form'));
print form
::submit('submit', '注销');
print form
::close();
}
}
function logout
()
{
// Load auth and log out
$auth = new Auth
();
$auth->logout(TRUE);
// Redirect back to the login page
url
::redirect('auth/login');
}
} // End Auth_Controller