所以我有一个指向相应视图的“仪表板”控制器。只需转至 即可随时访问此文件http://domain.com/dashboard
。但是,如果用户已经登录,我只希望访问此 URL。如果没有,它们将被重定向回主页。
所以两部分问题:
如果用户没有先登录,我如何防止他们访问“仪表板”(否则应重定向到主页)...编辑:此问题已得到修复并且代码更新如下
在注册控制器上,
thank()
我如何自动登录用户并将他们带到破折号?
这是我的“仪表板”控制器。编辑:现在可以正常工作了。上面的第二(2)个问题仍然存在。
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Dashboard extends CI_Controller{
function __construct(){
parent::__construct();
$this -> load -> model('user_model');
}
public function index()
public function index()
{
$session = $this->session->userdata('logged_in');
if ($session == 1)
{
$data['title'] = 'Dashboard';
$this -> load -> view('shared/header_view', $data);
$this -> load -> view('dash', $data);
$this -> load -> view('shared/footer_view', $data);
}
else {
redirect('register');
}
}
}
这是我的注册控制器,用于登录、注销和注册用户:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Register extends CI_Controller {
public function __construct()
{
parent::__construct();
$this -> load -> model('user_model');
}
public function index()
{
if (($this -> session -> userdata('user_name') != "")) {
$this -> welcome();
}
else
{
$data['title'] = 'Home';
$this -> load -> view('shared/header_view', $data);
$this -> load -> view("registration_view.php", $data);
$this -> load -> view('shared/footer_view', $data);
}
}
public function welcome()
{
redirect('/dashboard/', 'refresh');
}
public function login()
{
$email = $this -> input -> post('email');
$password = md5($this -> input -> post('pass'));
$result = $this -> user_model -> login($email, $password);
if ($result)
$this -> welcome();
else
$this -> index();
}
public function thank()
{
$data['title'] = 'You are now registered!';
$this -> load -> view('shared/header_view', $data);
$this -> load -> view('thank_view.php', $data);
$this -> load -> view('shared/footer_view', $data);
}
public function registration()
{
$this -> load -> library('form_validation');
// field name, error message, validation rules
$this -> form_validation -> set_rules('user_name', 'User Name', 'trim|required|min_length[4]|xss_clean');
$this -> form_validation -> set_rules('email_address', 'Your Email', 'trim|required|valid_email');
$this -> form_validation -> set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this -> form_validation -> set_rules('con_password', 'Password Confirmation', 'trim|required|matches[password]');
if ($this -> form_validation -> run() == FALSE)
{
$this -> index();
}
else
{
$this -> user_model -> add_user();
$this -> thank();
}
}
public function logout()
{
$newdata = array('user_id' => '', 'user_name' => '', 'user_email' => '', 'logged_in' => FALSE, );
$this -> session -> unset_userdata($newdata);
$this -> session -> sess_destroy();
redirect('/', 'refresh');
}
}
编辑:按要求,这是 user_model:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class User_model extends CI_Model {
public function __construct() {
parent::__construct();
}
function login($email, $password)
{
$this -> db -> where("email", $email);
$this -> db -> where("password", $password);
$query = $this -> db -> get("user");
if ($query -> num_rows() > 0)
{
foreach ($query->result() as $rows)
{
//add all data to session
$newdata = array(
'user_id' => $rows -> id,
'user_name' => $rows -> username,
'user_email' => $rows -> email,
'logged_in' => TRUE,
);
}
$this -> session -> set_userdata($newdata);
return true;
}
return false;
}
public function add_user()
{
$data = array(
'username' => $this -> input -> post('user_name'),
'email' => $this -> input -> post('email_address'),
'password' => md5($this -> input -> post('password')),
);
$this -> db -> insert('user', $data);
}
}
Codeigniter 新手。我究竟做错了什么?