i have a problem in a session
ihave made a login page called"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');
}
}
}
and make the admin pages in controller file called"admin"
and this is the code
<?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');
}
}
}
the page is login successfuly but if i refresh or go to any inner admin page
redirect to login page ???
Array
(
[__ci_last_regenerate] => 1442853634
[username] => nader
[password] => 01147187698
[loggedIn] => 1
)
and the problem is when i refresh the index page or enter any page redirect to alogin(login page)
Try on your admin controller
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'));
}
And change on your session check ! to FALSE and move it to the top like shown
Also try and var dump sessions just to make sure working.
echo '<pre>';
echo print_r($this->session->all_userdata());
echo '</pre>';
just check the user session in constructor of each controller
in your controller where you are saving session ..?
first of all make a method in base/parent controller to check the user is login
then call it in each controller's constructor
you can check all stored session through below given method
$this->session->all_userdata();
Related
I've created a project in Codeigniter. My problem is when I log in, auth controller shows the value that is set in session $this->session->userdata("logged_in") but it is not redirecting to dashboard.
I also changed the PHP version on the live server from PHP 7.1 to PHP 5.6 but it's still not working. Session works perfectly on local server with xampp but not working on live server
Auth_model
public function Authentification() {
$notif = array();
$email = $this->input->post('email',TRUE);
$password = Utils::hash('sha1', $this->input->post('password'), AUTH_SALT);
$this->db->select('*');
$this->db->from('users');
$this->db->where('email', $email);
$this->db->where('password', $password);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
$row = $query->row();
if ($row->is_active != 1) {
$notif['message'] = 'Your account is disabled !';
$notif['type'] = 'warning';
} else {
$sess_data = array(
'users_id' => $row->users_id,
'first_name' => $row->first_name,
'email' => $row->email
);
$this->session->set_userdata('logged_in', $sess_data);
}
} else {
$notif['message'] = 'Username or password incorrect !';
$notif['type'] = 'danger';
}
return $notif;
}
Auth controller
class Auth extends CI_Controller {
function __construct() {
parent::__construct();
Utils::no_cache();
if ($this->session->userdata('logged_in')) {
redirect(base_url('dashboard'));
exit;
}
}
public function index() {
redirect(base_url('home'));
}
public function login() {
$data['title'] = 'Login';
$this->load->model('auth_model');
if (count($_POST)) {
$this->load->helper('security');
$this->form_validation->set_rules('email', 'Email address', 'trim|required|valid_email|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
if ($this->form_validation->run() == false) {
// $data['notif']['message'] = validation_errors();
// $data['notif']['type'] = 'danger';
$status = validation_errors();
if ( $this->input->is_ajax_request() ) {
echo json_encode($status);
exit;
}
}
else {
$data['notif'] = $this->auth_model->Authentification();
// it show the result here but not redirect to dashboard
// print_r($this->session->userdata("logged_in"));
// die("auth/login");
}
}
if ($this->session->userdata('logged_in')) {
redirect(base_url('dashboard'));
exit;
}
/*
* Load view
*/
$this->load->view('includes/header', $data);
$this->load->view('home/index');
$this->load->view('includes/footer');
}
dashboard
class Dashboard extends CI_Controller {
var $session_user;
function __construct() {
parent::__construct();
$this->load->model('auth_model');
$this->load->helper('tool_helper');
Utils::no_cache();
if (!$this->session->userdata('logged_in')) {
redirect(base_url('home'));
exit;
}
$this->session_user = $this->session->userdata('logged_in');
}
/*
*
*/
public function index() {
$data['title'] = 'Dashboard';
$data['session_user'] = $this->session_user;
// print_r($this->session->userdata("logged_in")); //its show empty
$data['items'] = $this->auth_model->get_all_products();
$this->load->view('includes/header', $data);
// $this->load->view('includes/navbar');
$this->load->view('includes/navbar_new');
$this->load->view('dashboard/index');
$this->load->view('includes/footer');
}
I don't know why session not set. I have been stuck in this for a week. Please help me out.
Try this.
Change $config['sess_save_path'] = sys_get_temp_dir(); to $config['sess_save_path'] = FCPATH . 'application/cache/sessions/'; in config.php
Hi i need to add authentication to my staging website index page.Whenever the user tries to open the staging site it should show login page.Once user login then only he can see the website.I have tried added a code for displaying login page but it is displaying blank page not displaying any error messages as well.Here is my code.
As my website looks link will be for eg:staging.website.com
whenever the user tries to open this site it should ask login first.
Controller:
class Welcome extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->model('index_model');
$this->load->model('login_model');
}
public function index()
{
if($this->session->userdata('admin_logged_in'))
{
$data['admin_details'] = $this->session->userdata('admin_logged_in');
$data['records2'] = $this->index_model->get_all_banners();
$data['records7'] = $this->index_model->get_all_banners();
$data['mainpage'] = "index";
$this->load->view('templates/templatessss',$data);
}
else
{
$this->load->view('login');
}
}
public function login(){
$this->form_validation->set_rules('user_name','User Name','trim|required|xss_clean');
$this->form_validation->set_rules('password','Password','trim|required|xss_clean');
$this->form_validation->set_error_delimiters('<span class="ferror"> ','</span><br />');
if($this->form_validation->run()==FALSE){
$this->load->view('login');
}
else{
$user_name = $this->input->post('user_name');
$password = $this->input->post('password');
if($this->login_model->login_user($user_name,$password)):
redirect('welcome');
else:
$data['error'] = '<span class="ferror"> User Name or Password enter wrong. Please try again </span>';
$this->load->view('login',$data);
endif;
}
}
view(login.php):
<?php
$form_attributes = array('class'=>'admin_login_form', 'name'=>'admin_login_form', 'id'=>'admin_login_form', 'enctype' => "multipart/form-data");
$user_name = array('name' => 'user_name', 'id' => 'user_name', 'value' => set_value('user_name'),'class' =>'text');
$password = array('name' => 'password', 'id' => 'password','class' =>'text');
$submit = array('id' => 'login_details', 'value' => 'Login', 'name' => 'login_details');
?>
<?php echo form_open('welcome/login',$form_attributes);?>
<?php echo (validation_errors()) ? validation_errors() : ' ';?>
<?php echo (isset($error)) ? $error : '' ;?>
<?php echo form_label('User Name', $user_name['id']);?>
<?php echo form_input($user_name); if(!$this->input->post('user_name')) echo form_error('$user_name');?>
<?php echo form_label('Password', $password['id']);?>
<?php echo form_password($password);?>
<div class="sep"></div>
<button type="submit" class="ok"><?php echo form_submit($submit);?></button>
Model(login_model.php):
class login_model extends MY_Model
{
function login_user($user_name = '', $password=''){
$userdetails = array(
'user_name' => $user_name,
'password' => md5($password),
);
$this->db->where($userdetails);
$query = $this->db->get('login_details');
if($query->num_rows()):
$user = $query->result();
$sess_arry = array(
'user_id' => $user[0]->user_id,
'name' => $user[0]->name
);
$this->session->set_userdata('admin_logged_in', $sess_arry);
return true;
else:
return false;
endif;
}
}
Add this code in your Controller:
class Welcome extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->model('index_model');
$this->load->model('login_model');
}
public function index()
{
if($this->session->userdata('admin_logged_in'))
{
$data['admin_details'] = $this->session->userdata('admin_logged_in');
$data['records2'] = $this->index_model->get_all_banners();
$data['records7'] = $this->index_model->get_all_banners();
$data['mainpage'] = "index";
$this->load->view('templates/templatessss',$data);
}
else
{
redirect('welcome/login');
}
I hope it will work for you!!
Change your else condition
else
{
$this->load->view('login');
}
To
else
{
redirect('login');
}
welcome class
function __construct()
{
parent::__construct();
$this->load->helper("url");
if (!$this->session->userdata('admin_logged_in')){
redirect(base_url("login"));
}
$this->load->library('form_validation');
$this->load->model('index_model');
$this->load->model('login_model');
}
A login Class
class Login extends CI_Controller{
function index(){
if (isset($this->input->post())){
$this->form_validation->set_rules('user_name','User Name','trim|required|xss_clean');
$this->form_validation->set_rules('password','Password','trim|required|xss_clean');
$this->form_validation->set_error_delimiters('<span class="ferror"> ','</span><br />');
if($this->form_validation->run()==FALSE){
$this->load->view('login');
}
else{
$user_name = $this->input->post('user_name');
$password = $this->input->post('password');
if($this->login_model->login_user($user_name,$password)):
redirect('welcome');
else:
$data['error'] = '<span class="ferror"> User Name or Password enter wrong. Please try again </span>';
$this->load->view('login',$data);
endif;
}
}else{
$this->load->view("login");
}
}
this should working fine
i try to make a candidate vote system on Codeigniter. Each user only can vote 1 candidate, and after vote the candidate user status will change from '1' to '0' so user cant vote again. Can someone help me to create this function ?
Here is my code.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Admin_calon extends CI_Controller
{
function __construct() {
parent::__construct();
$this->load->model('Admin_dcalon');
$this->load->model('Admin_dvoter');
$this->load->model('Voter_model');
$this->load->helper('url');
$this->load->database();
}
public function index() {
$data['calon'] = $this->Admin_dcalon->tampil_data()->result();
$this->load->view('Admin_calon',$data);
}
function tambah_calon(){
$nama = $this->input->post('nama');
$config['upload_path'] = base_url(). './uploads/';
$config['allowed_types'] = 'jpg|png|jpeg';
$config['max_size'] = '0';
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->upload->do_upload();
$img = $this->input->post('img');
$data = array(
'nama' => $nama,
'img' => $img,
'voted' => 0
);
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">Data Tersimpan</div>');
$this->Admin_dcalon->tambah_data($data,'calon');
redirect('Admin_calon');
}
public function delete_calon($id_calon){
$where = array('id_calon' => $id_calon);
$this->Admin_dcalon->hapus_data($where,'calon');
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Data Terhapus</div>');
redirect('Admin_calon');
}
function edit_calon($id_calon){
$where = array('id_calon' => $id_calon);
$data['calon'] = $this->Admin_dcalon->edit_data($where,'calon')->result();
$this->load->view('Admin_ecalon',$data);
}
function update_calon(){
$id_calon = $this->input->post('id_calon');
$nama = $this->input->post('nama');
$img = $this->input->post('img');
$data = array(
'nama' => $nama,
'img' => $img,
);
$where = array(
'id_calon' => $id_calon
);
$this->session->set_flashdata('msg','<div class="alert alert-info text-center">Perubahan Tersimpan</div>');
$this->Admin_dcalon->update_data($where,$data,'calon');
redirect('Admin_calon');
}
function vote_calon(){
$id_calon = $this->input->post('id_calon');
$voted = $this->input->post('voted');
$data = array(
'id_calon' => $id_calon,
'voted' => +1
);
$where = array(
'id_calon' => $id_calon,
);
$this->Admin_dcalon->vote_calon($where,$data,'calon');
redirect('voter/voter_sukses');
}
public function logout() {
$data = ['id_admin', 'username'];
$this->session->unset_userdata($data);
redirect ('Admin_login');
}
}
?>
and this Model code.
<?php
class Admin_dcalon extends CI_Model{
function tampil_data(){
$this->db->select('*');
$this->db->from('calon');
return $this->db->get();
}
function tambah_data($data,$table){
$this->db->insert($table,$data);
}
function hapus_data($where,$table){
$this->db->where($where);
$this->db->delete($table);
}
function edit_data($where,$table){
return $this->db->get_where($table,$where);
}
function update_data($where,$data,$table){
$this->db->where($where);
$this->db->update($table,$data);
}
function vote_calon($where,$data,$table)
{
$this->db->where($where);
$this->db->update($table,$data);
}
}
?>
Please help me. Thanks for every one.
*
When I load up my homepage, it works perfectly. The custom helper I made for the CSS and JS files is working and it loads through the correct controller/view.
Every other page, however, loads from a the wrong controller (the News Controller). And the controller isn't using the custom helper for whatever reason, so there's no style on the page.
Here's what I have in my routes.php and each of my controllers:
route.php
$route['translate_uri_dashes'] = FALSE;
$route['news'] = 'news/index/1';
$route['news/create'] = 'news/create';
$route['news/view'] = 'news/index/1';
$route['news/view/(:any)'] = 'news/view/$1';
$route['news/(:any)'] = 'news/index/$1';
$route['logout'] = 'login/logout';
$route['register'] = 'login/register';
$route['confirm/(:any)/(:any)'] = 'login/confirm/$1/$2';
$route['default_controller'] = 'index';
$route['404_override'] = 'error';
Index Controller
<?php
class Index extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->load->library('session');
$data['page'] = 'Home';
$data['section'] = 'None';
if (isset($_SESSION['id'])) {
$data['user'] = $this->user_model->get_user($_SESSION['id']);
}
$this->load->view('layout/header', $data);
$this->load->view('layout/breadcrumb', $data);
$this->load->view('layout/body');
$this->load->view('index');
$this->load->view('layout/close');
$this->load->view('layout/footer');
}
}
News Controller
<?php
class News extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('news_model');
$this->load->library('session');
}
public function index($page) {
$data['page'] = 'News';
$data['section'] = 'None';
if (isset($_SESSION['id'])) {
$data['user'] = $this->user_model->get_user($_SESSION['id']);
}
$data['news'] = $this->news_model->get_news(NULL, $page);
$data['pageNo'] = $page;
$data['entries'] = $this->news_model->get_total();
$this->load->view('layout/header', $data);
$this->load->view('layout/breadcrumb', $data);
$this->load->view('layout/body');
$this->load->view('news/index', $data);
$this->load->view('layout/close');
$this->load->view('layout/footer');
}
public function view($id) {
$data['page'] = 'News';
if (isset($_SESSION['id'])) {
$data['user'] = $this->user_model->get_user($_SESSION['id']);
}
$data['news'] = $this->news_model->get_news($id);
$data['section'] = $data['news']['title'];
$data['prev'] = $this->news_model->get_prev($data['news']['pubDate']);
$data['next'] = $this->news_model->get_next($data['news']['pubDate']);
$data['pub'] = $this->user_model->get_user($data['news']['publisher']);
$this->load->view('layout/header', $data);
$this->load->view('layout/breadcrumb', $data);
$this->load->view('layout/body');
$this->load->view('news/view', $data);
$this->load->view('layout/close');
$this->load->view('layout/footer');
}
public function create() {
$this->news_model->set_news();
$this->session->set_userdata('created', 1);
redirect('news');
}
}
Login Controller
<?php
class Login extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('session');
}
public function index() {
$this->load->helper('form');
$this->load->library('form_validation');
$data['page'] = 'Login';
$data['section'] = 'None';
if (isset($_SESSION['id'])) {
redirect('/');
} else {
$this->form_validation->set_rules('username', 'Username', 'required', array('required' => 'You must provide a %s.'));
$this->form_validation->set_rules('password', 'Password', 'required', array('required' => 'You must provide a %s.'));
if ($this->form_validation->run() !== FALSE) {
$user = $this->user_model->login();
if ($user) {
$this->session->set_userdata('id', $user['id']);
redirect('/');
} else {
$this->session->set_userdata('loginerr', 1);
}
}
$this->load->view('layout/header', $data);
$this->load->view('layout/breadcrumb', $data);
$this->load->view('layout/body');
$this->load->view('login');
$this->load->view('layout/close');
$this->load->view('layout/footer');
}
}
public function logout() {
$this->session->sess_destroy();
redirect('/');
}
public function register() {
$this->load->helper('form');
$this->load->library('form_validation');
$data['page'] = 'Register';
$data['section'] = 'None';
if (isset($_SESSION['id'])) {
redirect('/');
} else {
$this->form_validation->set_rules('username', 'Username', 'required', array('required' => 'You must provide a %s.'));
$this->form_validation->set_rules('email', 'Email', 'required', array('required' => 'You must provide a %s.'));
$this->form_validation->set_rules('password', 'Password', 'required', array('required' => 'You must provide a %s.'));
$this->form_validation->set_rules('confpass', 'Confirm Password', 'required', array('required' => 'You must provide a %s.'));
if ($this->form_validation->run() !== FALSE) {
$this->user_model->register();
if (isset($_SESSION['regerror'])) {
if ($_SESSION['regerror'] == 1) {
$data['error'] = 'Username is already in use.';
} else if ($_SESSION['regerror'] == 2) {
$data['error'] = 'Email Address is already in use.';
} else if ($_SESSION['regerror'] == 3) {
$data['error'] = 'Passwords do not match';
} else {
$data['error'] = 'This IP Address has been banned for the following reason: ' . $_SESSION['regerror'];
}
unset($_SESSION['regerror']);
} else if (isset($_SESSION['register'])) {
$data['success'] = 1;
unset($_SESSION['register']);
}
}
$this->load->view('layout/header', $data);
$this->load->view('layout/breadcrumb', $data);
$this->load->view('layout/body');
$this->load->view('register', $data);
$this->load->view('layout/close');
$this->load->view('layout/footer');
}
}
public function confirm($un = NULL, $conf = NULL) {
$data['page'] = 'Confirmation';
$data['section'] = 'None';
if (isset($_SESSION['id'])) {
redirect('/');
} else {
if ($un == NULL || $conf == NULL) {
$data['message'] = 'No account selected...';
} else {
$check = $this->user_model->confirm($un, $conf);
if ($check == 1) {
$data['message'] = 'Invalid information provided...';
} else if ($check == 2) {
$data['message'] = 'User already confirmed...';
} else {
$data['message'] = 'Your account has been activated! You may now Login!';
}
}
$this->load->view('layout/header', $data);
$this->load->view('layout/breadcrumb', $data);
$this->load->view('layout/body');
$this->load->view('confirm', $data);
$this->load->view('layout/close');
$this->load->view('layout/footer');
}
}
}
Errors Controller
<?php
class Error extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('session');
}
public function index() {
$data['page'] = 'Error 404';
$data['section'] = 'None';
if (isset($_SESSION['id'])) {
$data['user'] = $this->user_model->get_user($_SESSION['id']);
}
$this->output->set_status_header('404');
$this->load->view('layout/header', $data);
$this->load->view('layout/breadcrumb', $data);
$this->load->view('layout/body');
$this->load->view('404');
$this->load->view('layout/close');
$this->load->view('layout/footer');
}
}
Can anyone tell me why this is happening?
['default_controller'] should ALWAYS be at the bottom, this is the LAST point of contact for the routes.
Any custom routes should be above it, also
news/(:any) will overwrite the other news ones, it should be below news/view and news/create
e.g:
$route['translate_uri_dashes'] = FALSE;
$route['news'] = 'news/index/1';
$route['news/create'] = 'news/create';
$route['news/view'] = 'news/index/1';
$route['news/view/(:any)'] = 'news/view/$1';
$route['news/(:any)'] = 'news/index/$1';
$route['logout'] = 'login/logout';
$route['register'] = 'login/register';
$route['confirm/(:any)/(:any)'] = 'login/confirm/$1/$2';
$route['default_controller'] = 'index';
$route['404_override'] = 'error';
I am trying to develop a login panel using codeigniter but I am unable to do so as I believe my concept is not so clear yet though or Am i doing something wrong please help me out with this concern
Controllers>admin.php
class admin extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('verify_user');
}
public function verify() {
$this->load->library('form_validation');
$username = $this->form_validation->set_rules('username', '', 'required|trim');
$password = $this->form_validation->set_rules('password', '', 'required|trim');
if($this->form_validation->run()) {
$this->verify_user->can_log_in();
redirect('admin/dashboard');
} else {
$this->load->view('admin/login');
}
}
public function dashboard() {
if($this->session->userdata('is_logged_id') == true) {
$this->load->view('admin/dashboard');
} else {
redirect('admin/login');
}
}
models>verify_users.php
class verify_user extends CI_Model {
public function __construct() {
parent::__construct();
}
public function can_log_in() {
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', $this->input->post('password'));
$query = $this->db->get('users');
$query2 = $this->db->get_where('users', array(
'username' => $this->input->post('username')
));
if($query2->num_rows() == 1) {
$name = $query2->row()->first_name . " " . $query2->row()->last_name;
}
if($query->num_rows() == 1) {
$query = $this->db->get_where('users', array(
'username' => $this->input->post('username')
));
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => 1
);
$this->session->set_userdata('name', $name);
$this->session->set_userdata($data);
return true;
} else {
$data['message'] = 'Incorrect username/password';
$this->load->view('admin/login', $data);
}
}
}
The thing is happening when I login with correct username and password it redirects me back to login.php when I put the model script within the verify function it runs perfectly
Please help me out with this
This is the closest possible fix to your way of implementation.
You need to consider reading more about MVC.
Try replace your controller with this:
class admin extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('verify_user');
}
public function verify() {
$this->load->library('form_validation');
$username = $this->form_validation->set_rules('username', '', 'required|trim');
$password = $this->form_validation->set_rules('password', '', 'required|trim');
if($this->form_validation->run() && $this->verify_user->can_log_in()) {
redirect('admin/dashboard');
} else {
$this->load->view('admin/login');
}
}
public function dashboard() {
if($this->session->userdata('is_logged_in') == "1") {
$this->load->view('admin/dashboard');
} else {
redirect('admin/login');
}
}
}
And your model with this:
class verify_user extends CI_Model {
public function __construct() {
parent::__construct();
}
public function can_log_in() {
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', $this->input->post('password'));
$query = $this->db->get('users');
$query2 = $this->db->get_where('users', array(
'username' => $this->input->post('username')
));
if($query2->num_rows() == 1) {
$name = $query2->row()->first_name . " " . $query2->row()->last_name;
}
if($query->num_rows() == 1) {
$query = $this->db->get_where('users', array(
'username' => $this->input->post('username')
));
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => "1"
);
$this->session->set_userdata('name', $name);
$this->session->set_userdata($data);
return true;
} else {
$data['message'] = 'Incorrect username/password';
return false;
}
}
}
Check this
class admin extends CI_Controller {
^// this should be Admin
IN model
else {
//$data['message'] = 'Incorrect username/password';
//$this->load->view('admin/login', $data);
//dont load view in model
return false;
}
In controller
if($this->form_validation->run()) {
$res = $this->verify_user->can_log_in();
if($res)
redirect('admin/dashboard');
else
redirect('admin/login');
} else {
$this->load->view('admin/login');
}
Fixing these 3 errors should help you.