Codeigniter: Unable to differentiate between admin and member page - php

This is the login controller code:
public function login_validation(){
$this->load->library('form_validation');
$this->load->model('model_users');
$this->form_validation->set_rules('username', 'Username', 'required|trim|alpha_numeric|xss_clean|strip_tags');
$this->form_validation->set_rules('password', 'Password', 'required|md5|trim|xss_clean|strip_tags');
if ($this->form_validation->run()){
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => 1
);
if($this->model_users->get_status($data['username'])){
$data = array(
'username' => $this->input->post('username'),
'status' => 'member',
'is_logged_in' => 1
);
$this->session->set_userdata($data);
redirect('member');
} else {
$data = array(
'username' => $this->input->post('username'),
'status' => 'admin',
'is_logged_in' => 1
);
$this->session->set_userdata($data);
redirect('admin');
}
} else {
$this->login();
}
}
This is the admin controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Admin extends CI_Controller {
public function __construct(){
parent::__construct();
if (! $this->session->userdata('is_logged_in')){
$status = $this->session->userdata('status');
if ($status == 'member'){
redirect('main/restricted');
}
redirect('main/restricted');
}
}
public function index() {
$data = array(
'title' => 'Admin Page'
);
$this->load->view("header", $data);
$this->load->view("admin");
$this->load->view("nav");
$this->load->view("footer");
}
}
This is the member controller code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Member extends CI_Controller {
public function __construct(){
parent::__construct();
if (! $this->session->userdata('is_logged_in')){
$status = $this->session->userdata('status');
if ($status == 'admin'){
redirect('main/restricted');
}
redirect('main/restricted');
}
}
public function index() {
$data = array(
'title' => 'Member Page'
);
$this->load->view("header", $data);
$this->load->view("member");
$this->load->view("nav");
$this->load->view("footer");
}
}
I am wondering why after the member login, they are able to go to the admin page, and also after the admin have logged in they are also able to access the member page.
I would want it to work as, after the member log in the member will only be able to access the member page. After the admin log in the admin will only be able to access the admin page.
Can someone help me please.

if (! $this->session->userdata('is_logged_in'))
I think your if condition returns false.
You did not write anything for elsecondition.Write some code for else.
If your if condition is false everyone can have access for member and admin controller.
Make sure your if condition returns true
i think your code may be like this
if (! $this->session->userdata('is_logged_in'))
{
//write code for not loged user
}
else
{
$status = $this->session->userdata('status');
if ($status == 'member'){
redirect('main/restricted');
}
}

You need to compare session status with 'member' or 'admin' instead of just checking if it is set.
In Admin controller:
$status = $this->session->userdata('status')
if ($status == 'member'){
redirect('main/restricted');
}
and similarly compare with 'admin' in member controller.

Related

i can't update database using code igniter

i'm using controller to call update function from model, but when i try to update for column name, that's no working.
---- Controller ----
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class editprofile extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('user');
}
public function index()
{
$this->form_validation->set_rules('email', 'email', 'required|valid_email');
$this->form_validation->set_rules('namalengkap', 'namalengkap', 'required');
$this->form_validation->set_rules('password', 'PASSWORD', 'required');
if ($this->form_validation->run() == false) {
$this->load->view('v_profile');
} else {
$row = $this->session->userdata('id');
$data = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'password' => $this->input->post('password'),
'phone' => $this->input->post('phone'),
'address' => $this->input->post('address'),
'kecamatan' => $this->input->post('kecamatan'),
'kelurahan' => $this->input->post('kelurahan'),
'rt' => $this->input->post('rt'),
'rw' => $this->input->post('rw'),
'zip code' => $this->input->post('zip code'),
'city' => $this->input->post('city'),
'province' => $this->input->post('province'),
);
$result = $this->user->update($data, $row);
if ($result > 0) {
$this->updateProfil();
$this->session->set_flashdata('msg', show_succ_msg('Data Profile Berhasil diubah, silakan lakukan login ulang!'));
redirect('c_login/profile');
} else {
$this->session->set_flashdata('msg', show_err_msg('Data Profile Gagal diubah'));
redirect('c_login/profile');
}
}
}
}
----- model -----
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Model
{
private $uid = 'id';
private $tabel = 'users';
public function __construct()
{
parent::__construct();
}
public function login($email, $password)
{
$this->db->where('email', $email);
$this->db->where('password', $password);
return $this->db->get($this->tabel);
}
function update($data, $id)
{
$this->db->where($this->uid, $id);
$this->db->update($this->tabel, $data);
}
public function get_by_cookie($kue)
{
$this->db->where('cookie', $kue);
return $this->db->get($this->tabel);
}
}
----- Core / MY_Controller -----
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('user');
}
public function updateProfil()
{
if ($this->userdata != '') {
$data = $this->user->select($this->userdata->email);
$this->session->set_userdata('userdata', $data);
$this->userdata = $this->session->userdata('userdata');
}
}
}
In your update function you have not set any values. As set method required array values you should give if. Good practice is using set before update and finally update of table. Follow the following codes for updating your users table.
function update($data, $id)
{
$this->db->set($data);
$this->db->where($this->uid, $id);
$this->db->update($this->tabel);
}
use this code. I hope it will work well.
in your controller
$result = $this->user->update($data, $row);
variable data is array
so in your model:
function update($data=array(), $id){
$this->db->where($this->uid, $id);
$this->db->update($this->tabel, $data);
}

If I already logged in then redirect dashboard page using codeigniter

Login Controller:
class Login extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->library('session');
$this->load->library('form_validation');
$this->load->model('login_model');
}
public function index() {
$this->form_validation->set_rules('username', 'UserName', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run() == FALSE) {
$this->load->view('admin/login');
} else {
$result = $this->login_model->select_login($_POST); //check username and password
if ($result) {
$user = array(
'id' => $result['id'],
'username' => $result['username'],
'password' => $result['password']
);
$this->session->set_userdata($user);
redirect('admin/Dashboard');
} else {
$data['msg'] = 'Invalid UserName and Password';
$this->load->view('admin/login', $data);
}
}
}
}
Dashboard Controller:
class Dashboard extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->library('session');
}
public function index() {
$session_id = $this->session->userdata('id');
if (!empty($session_id)) {
$data = array(
'page_title' => 'Dashboard',
'page_name' => 'dashboard/dashboard',
'admin_username' => $this->session->userdata('username')
);
$this->load->view('admin/template', $data);
} else {
redirect('admin/Login');
}
}
}
Dashboard View:
<html>
<body>
<h1>Helllo Admin</h1>
</body>
</html>
Question
How can I redirect my Dashboard if I'm already logged in?
what is admin in "redirect('admin/Dashboard');". Did you made any changes to url in route file?
When you do this
if ($result) {
$user = array(
'id' => $result['id'],
'username' => $result['username'],
'password' => $result['password']
);
$this->session->set_userdata($user);
redirect('admin/Dashboard');
You are setting your password in your session cookie. All you really need to do is set your username.
You should also do something like this in Login function index()
if(result){
$user = array(
'name' => $result['username]',
'is_logged_in' => TRUE //add this to your session data
);
$this->session->set_userdata($user);
}
Then create a method in whatever controller you are using
public function is_logged_in()
{
$is_logged_in = $this->session->userdata('is_logged_in');
if (!isset($is_logged_in) || $is_logged_in != true)
{
redirect("admin/Login");
}
}
When you are in your Dashboard you can use $this->is_logged_in(); to make sure no one else gets into your Dashboard or forms. Use it at the top of your methods for forms or just getting into your dashboard
Then use something like this on your pages
if($this->session->userdata('username') == true){
echo "Hello " . $this->session->userdata('username'). nbs(3), anchor("admin/Dashboard", " Go To Dashboard "); //this is all you need to get to your Dashboard if you are logged in.
}else {
echo " "; //doesnt show up if not logged in
}?>
That acts as a link on your pages that only shows up if you are logged in. At the top of all your Dashboard controller methods add the $is_logged_in();

Codeigniter show extra data by privilege

Okay, I have used someones CodeIgniters Register/Login form. But i have a menu with projects where some options should only be shown if your privilege == 1. i took the existing database and added a privilege column, when you make a new user this value is 0, if your admin this will be changed to 1.
My question:
I have this menu
<div class="menu_rechts">
<ul>
<?php
error_reporting(-1);
ini_set('display_errors',1);
$this->load->model('user_model');
if($this->session->userdata('privilege')== "1") {
echo "test";
} else {
echo "test2";?>
<li>Login/Register</li>
</ul>
</div>
It gets included in a header
Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->model('user_model');
}
public function index()
{
if($this->session->userdata('logged_in')=== TRUE)
{
$this->welcome();
}
else{
$data['title']= 'Home';
$this->load->view('header',$data);
$this->load->view("registration.php", $data);
$this->load->view('footer',$data);
}
}
public function welcome()
{
$data['title']= 'Welcome';
$this->load->view('header',$data);
$this->load->view('welcome.php', $data);
$this->load->view('footer',$data);
}
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']= 'Thank';
$this->load->view('header',$data);
$this->load->view('thank.php', $data);
$this->load->view('footer',$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' => '',
'privilege' => '',
'logged_in' => FALSE,
);
$this->session->unset_userdata($newdata );
$this->session->sess_destroy();
$this->index();
}
}
?>
And finally the 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,
'privilege' => $rows->privilege,
'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);
}
}
?>
i want to echo test, only if the privilege of the logged in user == 1

Where to find table in codeigniter?

I am new with codeigniter so this feels like a silly question but I have been looking all over. I have created a log in and account registration page using codeigniter. After submitting a form, I get this:
Error Number: 1146
Table 'users.user' doesn't exist
INSERT INTO `user` (`email`, `firstname`, `lastname`, `username`, `password`, `hint`) VALUES ('blah', 'blah', 'blah', 'blah', 'fa348efcd3bb1a1fc6ba5c2c912cf402', 'Brown')
Filename: F:\htdocs\system\database\DB_driver.php
Line Number: 330
The problem is that the table is called "users" not user. My question is how do I find the table name so I can change it to "users" instead of "User".
Here is the controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->model('user_model');
}
public function index()
{
if(($this->session->userdata('username')!=""))
{
$this->welcome();
}
else{
$data['title']= 'Home';
$this->load->view('header_view',$data);
$this->load->view("registration_view.php", $data);
$this->load->view('footer_view',$data);
}
}
public function welcome()
{
$data['title']= 'Welcome';
$this->load->view('header_view',$data);
$this->load->view('welcome_view.php', $data);
$this->load->view('footer_view',$data);
}
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']= 'Thank';
$this->load->view('header_view',$data);
$this->load->view('thank_view.php', $data);
$this->load->view('footer_view',$data);
}
public function registration()
{
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('username', 'User Name', 'trim|required|min_length[4]|xss_clean');
$this->form_validation->set_rules('email', '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('firstname', 'First Name', 'trim|required|min_length[3]|max_length[32]');
$this->form_validation->set_rules('lastname', 'Last Name', 'trim|required|min_length[2]|max_length[32]');
$this->form_validation->set_rules('hint', 'hint', 'trim|required|min_length[2]|max_length[32]');
if($this->form_validation->run() == FALSE)
{
$this->index();
}
else
{
$this->user_model->add_user();
$this->thank();
}
}
public function logout()
{
$newdata = array(
'user_id' =>'',
'username' =>'',
'user_email' => '',
'logged_in' => FALSE,
);
$this->session->unset_userdata($newdata );
$this->session->sess_destroy();
$this->index();
}
}
?>
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("users");
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(
'email'=>$this->input->post('email'),
'firstname'=>$this->input->post('firstname'),
'lastname'=>$this->input->post('lastname'),
'username'=>$this->input->post('username'),
'password'=>md5($this->input->post('password')),
'hint'=>$this->input->post('hint')
);
$this->db->insert('user',$data);
}
}
?>
Your insert method is trying to insert into the table user. Change user to users
public function add_user()
{
$data=array(
'email'=>$this->input->post('email'),
'firstname'=>$this->input->post('firstname'),
'lastname'=>$this->input->post('lastname'),
'username'=>$this->input->post('username'),
'password'=>md5($this->input->post('password')),
'hint'=>$this->input->post('hint')
);
$this->db->insert('user',$data);
}
Your code written
$query=$this->db->get("users");
change to user
*you should have to learn about the writting style of CI.

echo user in view from sessions code igniter

I am new in codeigniter. I have implemented a simple login system. I want to print out a username on my view page which is stored in sessions.
here is my controller
class LoginController extends CI_Controller {
function index(){
$new['main_content'] = 'loginView';
$this->load->view('loginTemplate/template', $new);
}
function verifyUser(){
//getting parameters from view
$data = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password')
);
$this->load->model('loginModel');
$query = $this->loginModel->validate($data);
if ($query){
//if the user c validated
//data variable is created becx we want to put username in session
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('sessionController/dashboard_area');
}
else
{
$this->index();
}
}
function logout()
{
$this->session->sess_destroy();
$this->index();
}
}
?>
sessionController
<?php
class SessionController extends CI_Controller {
function __construct()
{
parent::__construct();
$this->is_logged_in();
}
function dashboard_area(){
$data['main_content'] = 'dashboardView';
$this->load->view('dashboardTemplate/template', $data);
}
function is_logged_in()
{
$is_logged_in = $this->session->userdata('is_logged_in');
if(!isset($is_logged_in) || $is_logged_in != true)
{
echo 'You don\'t have permission to access this page.';
die();
//$this->load->view('login_form');
}else {
return true;
}
}
}
?>
how can i stored a username in sessions and then print out in a page ... i dont want to save username in every controller
if any one have a better suggestions to implement then please share it to me ..
$username = $this->session->userdata('username');
//Pass it in an array to your view like
$data['username']=$username;
$this->load->view('test',$data);
//Then in you view you can display it as:
echo $username;
controller file
$data = array( 'username' => $result->name, );
view file
<?php echo $this->session->userdata('username')?>
Just store the user name in the userdata (you've already done this!)
$this->session->set_userdata('username') = 'John Doe';
And retrieve it just like that
$username = $this->session->userdata('username');

Categories