this is my controller:-
public function home() {
if($this->session->userdata('id')) {
$this->header();
$this->load->model('admincon');
$e = $this->admincon->select();
$data['e'] = $e;
$this->load->view('admin/home',$data);
} else {
$this->index();
}
}
in the view page of function home() there is a form which submitted through function subques() the function is here:-
public function subques() {
if($this->session->userdata('id')) {
$tp=$this->input->post('box1',TRUE);
$s=$this->input->post('box',TRUE);
$t=$this->input->post('t',TRUE);
$a=$this->input->post('a',TRUE);
$b=$this->input->post('b',TRUE);
$c=$this->input->post('c',TRUE);
$d=$this->input->post('d',TRUE);
$n=$this->input->post('n',TRUE);
$this->load->model('admincon');
$this->admincon->subque($s,$t,$a,$b,$c,$d,$n,$tp);
$this->home();
} else {
$this->index();
}
}
but after submitting the values to the database when user redirected to the page if they click on refresh button the previous data store into the database another time. how to solve this problem.
i mean how to clear the variables after use them.
User redirect() instead of $this->home()
public function subques()
{
if($this->session->userdata('id'))
{
$tp=$this->input->post('box1',TRUE);
$s=$this->input->post('box',TRUE);
$t=$this->input->post('t',TRUE);
$a=$this->input->post('a',TRUE);
$b=$this->input->post('b',TRUE);
$c=$this->input->post('c',TRUE);
$d=$this->input->post('d',TRUE);
$n=$this->input->post('n',TRUE);
$this->load->model('admincon');
$this->admincon->subque($s,$t,$a,$b,$c,$d,$n,$tp);
redirect('controller_name/home');
}
else
{
redirect('controller_name/index');
}
}
without this $this->home(); use
redirect(base_url() . 'controller_name/home');
Related
I want to prevent the user to go back after i logged in, how can i do that.. please help im using php and codeigniter
I have this code on my controller
function __construct()
{
parent::__construct();
if ($this->session->userdata('LoggedIn') == FALSE)
{
session_destroy();
redirect('login','refresh');
}
}
I am open to all your help and suggestions guys
if ($this->session->userdata('LoggedIn') == TRUE)
{
redirect('dashboard');
}
Place this in your index function on login controller.
You should try like as below
function __construct()
{
parent::__construct();
if ($this->session->userdata('LoggedIn') == FALSE)
{
session_destroy();
redirect('login','refresh');
}else{
redirect('home','refresh');
}
}
Add below code on your login page controller, So if user came back, It will redirect again.
if ($this->session->userdata('LoggedIn') == TRUE)
{
redirect('homepage');//Your page after login
}
You mean after successful login ,user not allow to load login page again!
So you want to check each login function call the user is exists or not
function __construct()
{
parent::__construct();
if ($this->session->userdata('LoggedIn') == FALSE)
{
session_destroy();
redirect('LoginPage','refresh');
}
}
function yourLoginfun()
{
$LoggedIn = $this->session->userdata('LoggedIn');
if(!empty($LoggedIn))
{
redirect('/user_homepage.php', 'refresh');
}
else
{
$this->load->view('LoginPage');
}
}
How can I create logout button if my login controller with session is like this?
function login_user() {
$user_login = array(
'username'=>$this->input->post('username'),
'password'=>$this->input->post('password')
);
$data=$this->Infoserbilis_model->login_user($user_login['username'],$user_login['password']);
if($data) {
$session_data['logged_in'] = TRUE;
$this->session->set_userdata($session_data);
//$this->session->set_userdata('logged_in', $session_data);
redirect('Infoserbilis/admin_page', 'refresh');
} else {
echo '<script>alert("Invalid Username or Password");</script>';
redirect('Infoserbilis/index', 'refresh');
}
}
I have tried $this->session->sess_destroy(); on my function logout but to no avail. Thanks in advance
public function logout() {
// Removing session data
$this->session->sess_destroy();
echo '<script>alert("Bye!");</script>';
redirect('Infoserbilis/index', 'refresh');
}
Ok, base on your message, $this->session->sess_destroy() just only destroy the current session, you need redirect page manually. Write the specific route you want to redirect to in redirect() function
/**
* logout
*/
public function logout()
{
$this->session->sess_destroy();
redirect('/');
}
function logout() {
$this->session->unset_userdata('is_searched');
redirect('CONTROLLER/login');
}
You are redirecting to index function in Infoserbilis controller. Check session is active or not in that function.
Here is an example :
public function index() {
if($this->session->userdata('is_logged_in') == true){
//load the required view
}
else {
redirect('controller/login');
}
}
or you can redirect to login from logout function
public function logout() {
$this->session->sess_destroy();
redirect('controller/login');
}
I am new learner of CI.
I want to display data from database. but it doesn't work.
Code in controller :
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->model("user_model");
$this->lang->load('basic', $this->config->item('language'));
// redirect if not loggedin
if(!$this->session->userdata('logged_in')){
redirect('login');
}
$logged_in=$this->session->userdata('logged_in');
if($logged_in['base_url'] != base_url()){
$this->session->unset_userdata('logged_in');
redirect('login');
}
}
public function index($limit='0')
{
$logged_in=$this->session->userdata('logged_in');
if($logged_in['su']!='1'){
exit($this->lang->line('permission_denied'));
}
$data['limit']=$limit;
$data['title']=$this->lang->line('userlist');
// fetching user list
$data['result']=$this->user_model->user_list($limit);
$this->load->view('header',$data);
$this->load->view('user_list',$data);
$this->load->view('footer',$data);
}
public function teachercontrol()
{
$logged_in=$this->session->userdata('logged_in');
$sut='2';
$this->db->where('su',$sut);
// fetching user list
$data['teacher']=$this->user_model->teachermodel();
$this->load->view('header',$data);
$this->load->view('user_list',$data);
$this->load->view('footer',$data);
}
my model :
function teachermodel(){
$this->db->order_by('savsoft_users.uid','desc');
$this -> db -> join('savsoft_group', 'savsoft_users.gid=savsoft_group.gid');
$query=$this->db->get('savsoft_users');
return $query->result_array();
}
I try tro print in view, but it doesn't show any data :
<?php print_r($teacher)?>
What is the mistake?
while, the index function in controller (above the teachercontroller function) works well
Thanks
Put where condition in modal from teacher controller:
public function teachercontrol()
{
$logged_in=$this->session->userdata('logged_in');
$sut='2';
// fetching user list
$data['teacher']=$this->user_model->teachermodel($sut);
$this->load->view('header',$data);
$this->load->view('user_list',$data);
$this->load->view('footer',$data);
}
my model :
function teachermodel($sut = ""){
if($sut != "") {
$this->db->where('savsoft_users.su',$sut);
}
$this->db->join('savsoft_group', 'savsoft_users.gid=savsoft_group.gid', 'left');
$this->db->order_by('savsoft_users.uid','desc');
$query=$this->db->get('savsoft_users');
return $query->result_array();
}
I am struggling with flash data in codeigniter. My edit function is working properly. But problem is with delete function.My controller is as follows
public function rmRole()
{
$data = array();
$roleId = $this->uri->segment(4,0);
if ($this->_delete($roleId)) {
$this->session->set_flashdata('flash_message' ,'data_deleted');
redirect('adminroles');
}
}
My model includes
public function _delete($id) {
$table = $this->get_table();
$this->db->where('id', $id);
$this->db->delete($table);
}
My data is got deleted. But it is redirecting to a blank page with the url. What shall i do to display a flash data like data deleted successfully and redirect to adminroles module?
try this in model
function delete($id) {
if ($id)
{
if ($this->db->where("id", $id)->delete("table name")){
return TRUE;
}
else{
return FALSE;
}
}else{
return FALSE;
}
}
I already registered session with my code and preventing the the profile direct access with following code.
public function profile() {
if ($this->session->userdata('logged_in')) {
//var_dump($this->session->userdata['logged_in']);
$session_data = $this->session->userdata('logged_in');
$data['nric_number'] = $session_data['nric_number'];
$this->load->view('templates/header');
$this->load->view('layouts/employee/profile', $data);
$this->load->view('templates/footer');
} else {
//If no session, redirect to login page
$base = base_url();
redirect($base . 'checkinout', 'refresh');
}
}
Suppose, I am going prevent another controller named allusers. So, Script will be something like below-
public function allusers() {
if ($this->session->userdata('logged_in')) {
$this->load->view('layouts/employee/allusers');
} else {
//If no session, redirect to login page
$base = base_url();
redirect($base . 'checkinout', 'refresh');
}
}
But, I would not like to use following condition for each method. Can I skip actually?
if($this->session->userdata('logged_in')) {
} else {
}
Just put the code in your constructor of class, in this way you dont need to check for the session in all methods!
if(!($this->session->userdata('logged_in'))) {
$allowed = array(
'method1',
'method2'
);
if ( ! in_array($this->router->fetch_method(), $allowed) {
redirect('login');
}
}