It bothers me because with the most recent update to PHP Intelephense that I received today, the intelephense keeps displaying an error for an unknown property, functions.
Intelephense (1014)
and this is my code:
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Firm extends CI_Controller
{
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* #see https://codeigniter.com/user_guide/general/urls.html
*/
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->database();
$this->load->model('Users');
$this->load->model('Projects_Details');
$this->load->model('Categories');
date_default_timezone_set('asia/manila');
}
public function index()
{
$this->load->view('services/default/header');
$this->load->view('services/firm/firm');
$this->load->view('services/default/footer');
}
public function services()
{
$this->load->library('session');
if (!$this->session->userdata('user')) {
redirect('login', 'refresh');
} else {
$this->load->view('firm/projects/header-nav');
$this->load->view('default/sidebar/firm/services/sidebar-menu');
$this->load->view('firm/services/services-1');
}
}
public function firm_services()
{
$this->load->library('session');
if (!$this->session->userdata('user')) {
redirect('login', 'refresh');
} else {
$this->load->view('firm/projects/header-nav');
$this->load->view('default/sidebar/firm/firm-services/sidebar-menu');
$this->load->view('firm/firm_services/firm_services');
}
}
public function add_firm_services()
{
$permitted_chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$user_id = $this->input->post('user_id');
$service_code = substr(str_shuffle($permitted_chars), 0, 10) . rand(10000000, 99999999);
$category_id = $this->input->post('category_id');
$sub_category_id = $this->input->post('sub_category_id');
$service_name = $this->input->post('service_name');
$description = $this->input->post('description');
$display_service_name = $service_name;
$display_amount = $this->input->post('display_amount');
$ope = rand(0, 1);
$status = 1;
$vat = 12;
$data = array(
'user_id' => $user_id,
'service_code' => $service_code,
'category_id' => $category_id,
'sub_category_id' => $sub_category_id,
'service_name' => $service_name,
'description' => $description,
'display_service_name' => $display_service_name,
'display_amount' => $display_amount,
'ope' => $ope,
'status' => $status,
'vat' => $vat
);
$this->db->insert('services', $data);
$this->session->set_flashdata('success', 'Service successfully added.');
redirect('Firm/firm_services', 'refresh');
}
public function projects()
{
$this->load->library('session');
if (!$this->session->userdata('user')) {
redirect('login', 'refresh');
} else {
$this->load->view('firm/projects/header-nav');
$this->load->view('default/sidebar/firm/projects/sidebar-menu');
$this->load->view('firm/projects/project_view1');
}
}
public function edit_projects($id)
{
$this->load->library('session');
if (!$this->session->userdata('user')) {
redirect('login', 'refresh');
} else {
$data['project'] = $this->Projects_Details->get_id($id);
$this->load->view('firm/projects/header-nav');
$this->load->view('default/sidebar/firm/projects/sidebar-menu');
$this->load->view('firm/projects/project_view_edit', $data);
}
}
public function update_projects($id)
{
$data = array(
'project_id' => $this->input->post('project_id'),
'service_id' => $this->input->post('service_id'),
'firm_id' => $this->input->post('firm_id'),
'assigned_id' => $this->input->post('assigned_id'),
'package_id' => $this->input->post('package_id'),
'amount' => $this->input->post('amount'),
'ope' => $this->input->post('ope'),
'start_date' => $this->input->post('start_date'),
'due_date' => $this->input->post('due_date'),
'status' => $this->input->post('status'),
'comment' => $this->input->post('comment'),
'updated_at' => $this->input->post('updated_at'),
);
$this->db->where('id', $id);
$this->db->update('projects', $data);
redirect('Firm/projects', 'refresh');
}
public function show_projects($id)
{
$this->load->library('session');
if (!$this->session->userdata('user')) {
redirect('login', 'refresh');
} else {
$data['project'] = $this->Projects_Details->get_id($id);
$this->load->view('firm/projects/header-nav');
$this->load->view('default/sidebar/firm/firm-specialist/sidebar-menu');
$this->load->view('firm/projects/project_view_show', $data);
}
}
public function delete_projects($id)
{
$this->db->delete('projects', ['id' => $id]);
redirect('Firm/projects', 'refresh');
}
public function specialist_view()
{
$this->load->library('session');
if (!$this->session->userdata('user')) {
redirect('login', 'refresh');
} else {
$this->load->view('services/default/header');
$this->load->view('services/services');
$this->load->view('services/default/footer');
}
}
public function project_details()
{
$this->load->library('session');
if (!$this->session->userdata('user')) {
redirect('login', 'refresh');
} else {
$this->load->view('services/default/header');
$this->load->view('services/services');
$this->load->view('services/default/footer');
}
}
public function add_specialist($id)
{
$this->load->library('session');
if (!$this->session->userdata('user')) {
redirect('login', 'refresh');
} else {
$data['project'] = $this->Projects_Details->get_id($id);
$this->load->view('firm/projects/header-nav');
$this->load->view('default/sidebar/firm/projects/sidebar-menu');
$this->load->view('firm/add_specialist', $data);
}
}
public function firm_projects()
{
$this->load->library('session');
if (!$this->session->userdata('user')) {
redirect('login', 'refresh');
} else {
$this->load->view('firm/projects/header-nav');
$this->load->view('default/sidebar/firm/view-project-details/sidebar-menu');
$this->load->view('firm/projects/project_details_view');
}
}
public function firm_editprojects($id)
{
$this->load->library('session');
if (!$this->session->userdata('user')) {
redirect('login', 'refresh');
} else {
$data['project'] = $this->Projects_Details->get_id_pd($id);
$this->load->view('firm/projects/header-nav');
$this->load->view('default/sidebar/firm/view-project-details/sidebar-menu');
$this->load->view('firm/projects/project_details_edit', $data);
}
}
public function firm_showprojects($id)
{
$this->load->library('session');
if (!$this->session->userdata('user')) {
redirect('login', 'refresh');
} else {
$data['project'] = $this->Projects_Details->get_id_pd($id);
$this->load->view('firm/projects/header-nav');
$this->load->view('default/sidebar/firm/firm-specialist/sidebar-menu');
$this->load->view('firm/projects/project_details_show', $data);
}
}
public function delete_projects_details($id)
{
$this->db->delete('projects_details', ['id' => $id]);
redirect('Firm/firm_projects', 'refresh');
}
public function specialist()
{
$this->load->library('session');
if (!$this->session->userdata('user')) {
redirect('login', 'refresh');
} else {
$this->load->view('firm/projects/header-nav');
$this->load->view('default/sidebar/firm/firm-specialist/sidebar-menu');
$this->load->view('firm/firm_specialist/firm_specialist_view');
}
}
public function edit_specialist($id)
{
$this->load->library('session');
if (!$this->session->userdata('user')) {
redirect('login', 'refresh');
} else {
$data['user'] = $this->Users->get_id($id);
$this->load->view('firm/projects/header-nav');
$this->load->view('default/sidebar/firm/firm-specialist/sidebar-menu');
$this->load->view('firm/firm_specialist/firm_edit-specialist_view', $data);
}
}
public function show_specialist($id)
{
$this->load->library('session');
if (!$this->session->userdata('user')) {
redirect('login', 'refresh');
} else {
$data['user'] = $this->Users->get_id($id);
$this->load->view('firm/projects/header-nav');
$this->load->view('default/sidebar/firm/firm-specialist/sidebar-menu');
$this->load->view('firm/firm_specialist/firm_show-specialist_view', $data);
}
}
public function update_specialist($id)
{
$data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'registered_address' => $this->input->post('registered_address'),
'city' => $this->input->post('city'),
'contact_number' => $this->input->post('contact_number'),
'email' => $this->input->post('email'),
);
$this->db->where('id', $id);
$this->db->update('users', $data);
$this->session->set_flashdata('success', 'User Details Updated Successfully.');
redirect('Firm/specialist', 'refresh');
}
public function delete_specialist($id)
{
$this->Users->delete_id($id);
$this->session->set_flashdata('success', 'User Deleted Successfully.');
redirect('Firm/specialist', 'refresh');
}
}
Is there a way to make the IntelliSense stop reporting errors even though there no error in the code?
You can disable Intelephense > Diagnostics: Undefined Properties in the Intelephense extension config using settings editor;
Or put this line in the settings.json
"intelephense.diagnostics.undefinedProperties": false
This happens because CodeIgniter injects these properties dynamically into the controller at runtime via CI_Loader, as Inteliphense could not find these properties declared in your code, it reports an unknown property error.
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
This is my home.php file in this controller I am facing Fatal error: Uncaught ArgumentCountError:
I have also attached a screenshot, In this screenshot error mentioned, You can easily understand the error through this screenshot, I am newbie in CodeIgniter.
[
I upload only my controller related file.
<?php
class home extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('select');
$this->load->helper(array('form', 'url', 'html'));
$this->load->library(array('form_validation', 'session'));
}
function records() {
$ytl['dta'] = $this->users->datas();
//$this->load->view('info',$ytl);
//echo "<pre>controller";print_r($ytl);
}
function primezone() {
$ytl['dta'] = $this->users->datas();
echo "<pre>controller";
print_r($ytl);
}
function form($id) {
//die($id);
if ($this->input->post()) {
//echo"<pre>";print_r( $this->input->post());die;
$this->form_validation->set_rules('fname', 'First Name', 'required|min_length[5]');
$this->form_validation->set_rules('lname', 'Last Name', 'required|callback_check_picture[lname]');
//$this->form_validation->set_rules('email','Email','required');
$this->form_validation->set_rules('mobile', 'Mobile Number', 'required|callback_valid_phone_number[mobile]');
$this->form_validation->set_error_delimiters('<h1 class="error">', '</h1>');
if ($this->form_validation->run() == TRUE) {
$data = [
'fname' => $this->input->post('fname'),
'lname' => $this->input->post('lname'),
'email' => $this->input->post('email'),
'mobile' => $this->input->post('mobile'),
'message' => $this->input->post('message'),
];
if (empty($id)) {
$ytl = $this->select->insertdata($data);
} else {
$ytl = $this->select->updatedata($data, $id);
}
if ($ytl) {
$this->session->set_flashdata('message', 'Successfully Added.');
redirect('home');
}
} else {
//$this->load->view('form');
}
}
$ytl['dta'] = $this->select->getDataById($id);
//echo "<pre>";print_r($ytl);die;
$this->load->view('form', $ytl);
}
public function check_picture($a) {
if ( ! is_numeric($a)) {
return TRUE;
} else {
$this->form_validation->set_message('check_picture', 'Please enter only char value');
return FALSE;
}
}
function valid_phone_number($value) {
$value = strlen($value);
//echo $value;die;
if ($value == 10) {
return TRUE;
} else {
$this->form_validation->set_message('valid_phone_number', 'Mobile number not in range'); //{10}
return FALSE;
}
}
public function index() {
//load the database
//$this->load->database();
//load the model
$this->load->model('select');
//load the method of model
$data['h'] = $this->select->select();
//return the data in view
$this->load->view('fetch_view', $data);
}
public function delete() {
$this->load->model('select');
$id = $this->input->get('id');
if ($this->select->deleteuser($id)) {
$data['data'] = $this->select->getuser();
$this->load->view('fetch_view', $data);
}
}
}
The error you have mention is normally came when there is mismatch in param mention in function and the passing ones.
According to your code
public function form($id)
$id is required and you are not passing that.
So change your code to
public function form($id=null)
Set default id value in your controller function like
function form($id = null){
....
}
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.
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();
I created a login system but every time I setup an if statement it loops back to the login page when I enter correct password. I need the index function in the controller, the list_employee function and View_employee function to redirect user to login page if they access it directly but if they enter correct password allow them to go to it.
user_authentication controller
<?php
session_start(); //we need to start session in order to access it through CI
Class User_Authentication extends CI_Controller {
public function __construct() {
parent::__construct();
// Load form helper library
$this->load->helper('form');
// Load form validation library
$this->load->library('form_validation');
// Load session library
$this->load->library('session');
// Load database
$this->load->model('login_database');
}
// Show login page
public function user_login_show() {
$this->load->view('login_form');
}
// Show registration page
public function user_registration_show() {
$this->load->view('registration_form');
}
// Validate and store registration data in database
public function new_user_registration() {
// Check validation for user input in SignUp form
$this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('email_value', 'Email', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
if ($this->form_validation->run() == FALSE) {
$this->load->view('registration_form');
} else {
$data = array(
'name' => $this->input->post('name'),
'user_name' => $this->input->post('username'),
'user_email' => $this->input->post('email_value'),
'user_password' => $this->input->post('password')
);
$result = $this->login_database->registration_insert($data) ;
if ($result == TRUE) {
$data['message_display'] = 'Registration Successfully !';
$this->load->view('login_form', $data);
} else {
$data['message_display'] = 'Username already exist!';
$this->load->view('registration_form', $data);
}
}
}
// Check for user login process
public function user_login_process() {
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
if ($this->form_validation->run() == FALSE) {
$this->load->view('login_form');
} else {
$data = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password')
);
$result = $this->login_database->login($data);
if($result == TRUE){
$sess_array = array(
'username' => $this->input->post('username')
);
// Add user data in session
$this->session->set_userdata('logged_in', $sess_array);
$result = $this->login_database->read_user_information($sess_array);
if($result != false){
$data = array(
'name' =>$result[0]->name,
'username' =>$result[0]->user_name,
'email' =>$result[0]->user_email,
'password' =>$result[0]->user_password
);
redirect('employee');
}
}else{
$data = array(
'error_message' => 'Invalid Username or Password'
);
$this->load->view('login_form', $data);
}
}
}
// Logout from admin page
public function logout() {
// Removing session data
$sess_array = array(
'username' => ''
);
$this->session->unset_userdata('logged_in', $sess_array);
$data['message_display'] = 'Successfully Logout';
$this->load->view('login_form', $data);
}
}
?>
employee controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Employee extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('login/employee_model');
}
//Shows the dashboard
public function index()
{
$this->load->view('header');
$this->load->view('employee');
$this->load->view('login/footer');
}
//Insert the employee
public function insert_employee()
{
$data=array('name'=>$this->input->post('name'),
'LanId'=>$this->input->post('LanId'),
'reason'=>$this->input->post('reason'),
'PepNumber'=>$this->input->post('PepNumber'),
'Employee_Number'=>$this->input->post('Employee_Number'),
'department'=>$this->input->post('department'),
'status'=>1);
//print_r($data);
$result=$this->employee_model->insert_employee($data);
if($result==true)
{
$this->session->set_flashdata('msg',"Employee Records Added Successfully");
redirect('employee');
}
else
{
$this->session->set_flashdata('msg1',"Employee Records Added Failed");
redirect('employee');
}
}
//List of Employees
public function list_employees()
{
$data['employee']=$this->employee_model->get_employee();
$this->load->view('header');
$this->load->view('list_of_employees',$data);
$this->load->view('login/footer');
}
//List of Employees
public function viewlist_employees()
{
$data['employee']=$this->employee_model->get_employee();
$this->load->view('header');
$this->load->view('viewlist_of_employees',$data);
$this->load->view('login/footer');
}
public function delete_employee()
{
$id=$this->input->post('id');
$data=array('status'=>0);
$result=$this->employee_model->delete_employee($id,$data);
if($result==true)
{
$this->session->set_flashdata('msg1',"Deleted Successfully");
redirect('employee/list_employees');
}
else
{
$this->session->set_flashdata('msg1',"Employee Records Deletion Failed");
redirect('employee/list_employees');
}
}
public function edit_employee()
{
$id=$this->uri->segment(3);
$data['employee']=$this->employee_model->edit_employee($id);
$this->load->view('header',$data);
$this->load->view('edit_employee');
}
public function update_employee()
{
$id=$this->input->post('id');
$data=array('name'=>$this->input->post('name'),
'LanID'=>$this->input->post('LanID'),
'reason'=>$this->input->post('reason'),
'PepNumber'=>$this->input->post('PepNumber'),
'Employee_Number'=>$this->input->post('Employee_Number'),
'department'=>$this->input->post('department'),
'status'=>1);
$result=$this->employee_model->update_employee($data,$id);
if($result==true)
{
$this->session->set_flashdata('msg',"Employee Records Updated Successfully");
redirect('employee/list_employees');
}
else
{
$this->session->set_flashdata('msg1',"No changes Made in Employee Records");
redirect('employee/list_employees');
}
}
}
?>
login_database model
<?php
Class Login_Database extends CI_Model {
// Insert registration data in database
public function registration_insert($data) {
// Query to check whether username already exist or not
$condition = "user_name =" . "'" . $data['user_name'] . "'";
$this->db->select('*');
$this->db->from('user_login');
$this->db->where($condition);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 0) {
// Query to insert data in database
$this->db->insert('user_login', $data);
if ($this->db->affected_rows() > 0) {
return true;
}
} else {
return false;
}
}
// Read data using username and password
public function login($data) {
$condition = "user_name =" . "'" . $data['username'] . "' AND " . "user_password =" . "'" . $data['password'] . "'";
$this->db->select('*');
$this->db->from('user_login');
$this->db->where($condition);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
return true;
} else {
return false;
}
}
// Read data from database to show data in admin page
public function read_user_information($sess_array) {
$condition = "user_name =" . "'" . $sess_array['username'] . "'";
$this->db->select('*');
$this->db->from('user_login');
$this->db->where($condition);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
return $query->result();
} else {
return false;
}
}
}
?>
employee_model
<?php
class Employee_model extends CI_Model
{
public function insert_employee($data)
{
$this->db->insert('employee_list',$data);
return ($this->db->affected_rows() != 1 ) ? false:true;
}
public function get_employee()
{
$this->db->select('*');
$this->db->from('employee_list');
$this->db->where('status',1);
$query =$this->db->get();
return $query->result();
}
public function delete_employee($id,$data)
{
$this->db->where('id',$id);
$this->db->update('employee_list',$data);
return ($this->db->affected_rows() != 1 ) ? false:true;
}
public function edit_employee($id)
{
$this->db->select('*');
$this->db->from('employee_list');
$this->db->where('id',$id);
$this->db->where('status',1);
$query =$this->db->get();
return $query->result();
}
public function update_employee($data,$id)
{
$this->db->where('id',$id);
$this->db->update('employee_list',$data);
return ($this->db->affected_rows() != 1 ) ? false:true;
}
}
add if statement with logged_in and a redirect to login form if it
is incorrect
public function index()
{
if($this->session->userdata('logged_in'))
{
$this->load->view('header');
$this->load->view('employee');
$this->load->view('login/footer');
}else{
redirect('user_authentication/user_login_show');
}
}
Best Practice is to add the check in the constructor of your controller in CI.
here is the example of mine.
public function __construct() {
parent::__construct();
if (!$this->session->userdata('user_data')) {
return redirect('login');
}
$this->load->model('customer_model');
}
you can add the else statement to redirect to the dashboard or what the resulting page if user is logged in.
Add this line of code to your constructors:
$this->load->library('session');
This will help you.
public function login()
{
$this->load->view('login');
if (isset($_POST['login']))
{
$emailid = $this->input->post('emailid');
$password = $this->input->post('password');
$this->load->model('main_model');
if($this->main_model->can_login('$emailid','$Password'))
{
$session_data = array(
'emailid' => $emailid,
'password' => $password,
'iss_logged_in' => 1
);
$this->session->set_userdata($session_data);
redirect(base_url().'index.php/Hello_cnt/');
}
else
{
$this->session->set_flashdata('error', 'Invalid Username and Password');
redirect(base_url().'index.php/Hello_cnt/login');
}
}
}