我在会话中遇到问题 我已经创建了一个名为“alogin”的登录页面
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
class alogin extends CI_Controller{
public function __construct() {
parent::__construct();
$this->load->model('user_model');
$this->load->model('admin_model');
$this->load->library('form_validation');
$this->load->library('session');
$this->load->model('articles_model');
$this->load->helper(array('form', 'url'));
}
function index(){
$this->form_validation->set_rules('username',' اسم المستخدم','trim|required|xxs_clean');
$this->form_validation->set_rules('password','كلمة المرور','trim|required|xss_clean');
$this->form_validation->run();
//post value
$data['username'] = $this->input->post('username');
$data['password'] = $this->input->post('password');
if($this->input->post('login')){
if($this->user_model->login($data)){
$this->setsession();
redirect('admin/index');
} else {
redirect('admin');
}
}
$this->load->view('admin/login');
}
public function setsession(){
$dat = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password'),
'loggedIn' => TRUE
);
$this->session->set_userdata($dat);
}
public function logout(){
if($this->session->sess_destroy()){
redirect('admin/alogin');
} else {
// redirect('admin/log/index');
}
}
}
并在名为“admin”的控制器文件中创建管理页面 这是代码
<?php
class admin extends CI_Controller
{
public function __construct() {
parent::__construct();
$this->load->model('user_model');
$this->load->model('admin_model');
$this->load->library('form_validation');
$this->load->library('session');
$this->load->model('articles_model');
$this->load->helper(array('form', 'url'));
if(!$this->session->userdata('loggedIn')){
redirect('alogin');
}
}
public function index(){
$data['count'] = $this->admin_model->count_message();
$data['messages'] = $this->admin_model->show_message();
$data['title'] = ' لوحة التحكم';
$data['subview'] = 'admin/main';
$this->load->view('admin/index',$data);
}
public function setting(){
$data['settings']= $this->admin_model->settings();
$data['title'] = ' تعديل اعدادات الموقع';
$data['subview'] = 'admin/setting';
$this->load->view('admin/index',$data);
}
public function set_update(){
$data = array(
'site_name' => $this->input->post('site_name'),
'site_desc'=> $this->input->post('site_desc')
);
$update = $this->admin_model->set_update($data);
if(isset($update)){
redirect('admin/setting');
}
}
function message(){
$data['messages'] = $this->admin_model->show_message();
$data['title'] = 'الرسائل';
$data['subview'] = 'admin/message';
$this->load->view('admin/index',$data);
}
/// articles
public function add_article(){
//
// do upload
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 3000;
$config['max_width'] = 1024;
$config['max_height'] = 1000;
$this->load->library('upload', $config);
if(!$this->upload->do_upload('file')){
$data['error'] = $this->upload->display_errors();
} else {
$data['img_data'] = $this->upload->data();
$img = $this->upload->data();
}
$articels = array(
'title' => $this->input->post('title'),
'content' => $this->input->post('content'),
'date' => date("Y-m-d H:i:s") ,
'img' => @$img['full_path'],
);
if($this->input->post('add')){
//form validation
$this->form_validation->set_rules('title','title','required');
$this->form_validation->set_rules('author','title','required');
$this->form_validation->set_rules('content','title','required');
$this->form_validation->set_rules('img','title','required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('admin/add_c');
}
else
{
$this->load->view('admin');
}
//form validation
if($this->articles_model->add_article($articels)){
$data['message'] = 'تم اضافة الخبر بنجاح';
}
} else {
echo 'problem';
}
$data['title'] = 'اضافة خبر';
$data['subview'] = 'admin/add_c';
$this->load->view('admin/index',$data);
}
public function articles(){
$data['articels'] = $this->articles_model->get_articles();
$data['title'] = 'عرض المقالات';
$data['subview'] = 'admin/articles';
$this->load->view('admin/index',$data);
}
function delete_articels($id){
$id = $this->uri->segment(4);
if( $this->articles_model->delete_articles($id)){
redirect('admin/articles');
}
}
function edit_articels(){
$id = $this->uri->segment(3);
$data['articels'] = $this->articles_model->get_article_id($id);
$artc = array(
'title' => $this->input->post('title'),
'author' => $this->input->post('author'),
'content' => $this->input->post('content'),
'img' => $this->input->post('img')
);
if($this->input->post('update')){
if($this->articles_model->edit_c($id,$artc)){
echo 'تم تعديل المقال بنجاح';
} else {
echo 'مشكلة فى تعديل البيانات';
}
}
$data['title'] = 'تعديل المقال';
$data['subview'] = 'edit_c';
$this->load->view('admin/index',$data);
}
public function stat(){
$data['stats']= $this->admin_model->get_static();
$data['subview'] = 'admin/stat';
$data['title'] = 'تعديل احصائيات العيادة';
$this->load->view('admin/index',$data);
}
// pat
function add_pat(){
$pats = array(
'pat_name' => $this->input->post('pat_name'),
'pat_pat' => $this->input->post('pat_pat'),
'pat_content'=> $this->input->post('pat_content')
);
if($this->input->post('add')){
$this->admin_model->add_pat($pats);
}
$data['subview'] = 'admin/add_pat';
$data['title'] ='اضافة حالة جديدة';
$this->load->view('admin/index',$data);
}
function show_pats(){
$data['title'] = 'عرض الحالات ';
$data['pats'] = $this->admin_model->show_pat();
$data['subview'] = 'admin/show_pats';
$this->load->view('admin/index',$data);
}
function delete_pat(){
$id = $this->uri->segment(3);
if($this->admin_model->delete_pat($id)){
redirect('admin/show_pats');
}
}
function edit_pat(){
$id = $this->uri->segment(3);
$pat = array(
'pat_name' => $this->input->post('pat_name'),
'pat_pat' => $this->input->post('pat_pat'),
'pat_content' => $this->input->post('pat_content')
);
if($this->input->post('update')){
if($this->admin_model->edit_pat($id,$pat)){
redirect('admin/show_pats');
echo 'done';
} else {
redirect('home');
}
}
$data['title'] = 'تعديل';
$data['pats'] = $this->admin_model->show_pat_id($id);
$data['subview'] = 'admin/edit_pat';
$this->load->view('admin/index',$data);
}
/// videos
function show_videos(){
$data['videos']= $this->admin_model->get_videos();
$data['title'] = 'عرض الفديوهات';
$data['subview'] ='admin/show_videos';
$this->load->view('admin/index',$data);
}
function add_video(){
$add = array(
'video_title' => $this->input->post('video_title'),
'video_url' => $this->input->post('video_url')
);
if($this->input->post('add')){
$this->admin_model->add_video($add);
redirect('admin/show_videos');
}
$data['title'] = 'اضف فديو جديد ';
$data['subview'] = 'admin/add_video';
$this->load->view('admin/index',$data);
}
function edit_video(){
$id = $this->uri->segment(3);
$data['videos'] = $this->admin_model->get_video_id($id);
$data['id'] = $id;
$edit = array(
'video_title' => $this->input->post('video_title'),
'video_url'=> $this->input->post('video_url')
);
if($this->input->post('update')){
$this->admin_model->edit_video($id,$edit);
redirect('admin/show_videos');
} else {
echo 'problem';
}
$data['title'] = 'تعديل الفديو';
$data['subview'] = 'admin/edit_video';
$this->load->view('admin/index',$data);
}
function delete_video(){
$id = $this->uri->segment(3);
if($this->admin_model->delete_video($id)){
redirect('admin/show_videos');
}
}
}
页面成功登录,但如果我刷新或转到任何内部管理页面 重定向到登录页面???
Array
(
[__ci_last_regenerate] => 1442853634
[username] => nader
[password] => 01147187698
[loggedIn] => 1
)
问题是我刷新索引页面或输入任何页面重定向到alogin(登录页面)
答案 0 :(得分:0)
试试你的管理控制器
public function __construct() {
parent::__construct();
if($this->session->userdata('loggedIn') == FALSE){
redirect('alogin');
}
$this->load->model('user_model');
$this->load->model('admin_model');
$this->load->library('form_validation');
$this->load->library('session');
$this->load->model('articles_model');
$this->load->helper(array('form', 'url'));
}
更改会话检查!为FALSE并将其移至顶部,如图所示
另外尝试和var转储会话只是为了确保工作。
echo '<pre>';
echo print_r($this->session->all_userdata());
echo '</pre>';
答案 1 :(得分:0)
只需在每个控制器的构造函数中检查用户会话 在您保存会话的控制器中。?
首先在base / parent控制器中创建一个方法来检查用户是否登录 然后在每个控制器的构造函数中调用它
您可以通过以下给定的方法检查所有存储的会话
$这 - &GT;会话而&GT; all_userdata();