Cant set session after unset session in codeigniter - php

after unset the session or log out. I cant log in another session.
this is my session set
if($check){
$arr = array(
'username' => $username,
'password' => $password
);
$this->session->set_userdata("check",$arr);
}else{
echo "nothing";
return FALSE;
}
and this is my unsetting the session
public function unset_sesssion_data(){
//removing session data
$this->session->unset_userdata('username');
$this->session->unset_userdata('password');
redirect('/log_in', 'refresh');
}
and in my HTML this my code
if($this->session->userdata('username') == TRUE){
echo "WELCOME ".$this->session->userdata('username');
}else{
echo "Please Log In";
}
for logging out
Log Out
any idea where did I get wrong and I cant set another session to log in another user
edited
I get the correct answer I just remove the check in $this->session->set_userdata("check",$arr); but why does not make a new set of session?

Try this, to set the user data
if($check){
$arr = array(
'username' => $username,
'password' => $password
);
$this->session->set_userdata($arr);
} else {
echo "nothing";
return FALSE;
}
and to unset the user data
public function unset_sesssion_data(){
$this->session->sess_destroy();
redirect('/log_in', 'refresh');
}

Related

Second tab redirect to login page when i am already logged in, In codeigniter Application

function auth(){
$nic = $this->input->post('nic',TRUE);
$password = md5($this->input->post('password',TRUE));
$this->load->model('Login_model');
$validate = $this->Login_model->validate($nic,$password);
$this->load->helper("cookie");
$autoLogin = $this->input->post("autologin",true);
if($validate->num_rows() > 0){
$data = $validate->row_array();
$nic = $data['user_nic'];
$name = $data['user_name'];
$email = $data['user_email'];
$level = $data['user_level'];
$sesdata = array(
'nic' => $nic,
'username' => $name,
'email' => $email,
'level' => $level,
'logged_in' => TRUE
);
$this->session->set_userdata($sesdata);
if($level === '1'){
redirect('Forget/dashboard');
// access login for staff
}elseif($level === '2'){
redirect('welcome/officer_dashboard');
// access login for author
}elseif($level === '3'){
redirect('User/User_Dashboard');
}
}else{
echo $this->session->set_flashdata('msg','Username or Password is Wrong');
redirect('welcome');
}
}
}
This is my user auth function which redirects to ages upon user levels. I have many views as per the User levels.
But whenever I have loggedin, the second tab base url redirects to Login page again.
TIA
Debug the session data(using print statements) and make sure that you have added proper conditions in each page based on session data
I Welcome controller, Check the session data is_logged in, If data available act according to it
public function index()
{
$session_id = $this->session->userdata('nic');
if (!empty($session_id))
{
$this->load->view('login');
}
else
{
redirect('welcome/auth');
}
}

session works on localhost but sometimes not working on server

this is a controller where i'm creating a session :
public function login(){
if(isset($_POST)){
$res = $this->register_model->loginUser();
if($res['result'] === true){
// declare session variables
$user = $res['info']; // array containing user information
// set session variables
$_SESSION['storeId'] = $user->str_id;
$_SESSION['sName'] = $user->str_nme;
$_SESSION['sId'] = $user->str_identifier;
$_SESSION['hash'] = $user->hash;
$res['info'] = null;
}
} else {
$res = array('result'=>false,'msg'=>'Login failed. Please try again');
}
echo json_encode($res);
}
and this is a controller where i retrieve a session
Try this
public function login(){
if(isset($_POST)){
$res = $this->register_model->loginUser();
if($res['result'] === true){
// declare session variables
$user = $res['info']; // array containing user information
// set session variables
$this->load->library('session');
$newdata = array(
'storeId' => $user->str_id,
'sName' => $user->str_nme,
'sId' => $user->str_identifier,
'hash' => $user->hash,
'info' => null,
'logged_in' => TRUE,
);
$this->session->set_userdata($newdata);
}
} else {
$res = array('result'=>false,'msg'=>'Login failed. Please try again');
}
echo json_encode($res);
}

Codeigniter 2.2 failed create session

I use CI 2.2 to build a simple login system. But I get problem when I try to generate session. Of course I have properly set up libraries (database, session) and User_M. When I retrieve data from database (without session), that's work fine. This is my Controller code:
public function verify()
{
// Define variable
$user = $this->input->post('username');
$pass = $this->input->post('password');
// Check input data
if (!empty($user) AND !empty($pass))
{
// Check match data from db
$checks = $this->User_M->check_user($user, $pass);
if($checks->num_rows() == 1)
{
foreach ($checks->result() as $check)
{
$sess = array (
'username' => $check->username,
'logged_in' => TRUE
);
$rest = $this->session->set_userdata($sess);
if ($rest)
{
echo "working";
} else {
echo "not working";
}
}
} else {
echo "Not found";
}
} else {
echo "You've empty field";
}
}
Additional explain, I check the result with if ($rest)...bla..bla..bla, that's echoing Not Working. Please let me know where's my mistakes?
Thank in advance
I think the problem is with this statement
$rest = $this->session->set_userdata($sess);
the set_userdata() does not return anything, so according to your condition it will always execute the else part.
Try to change your condition like this
if (!empty($this->session->userdata("username"))){
echo "Working";
}else{
echo "Not Working";
}

How to Check either Session is Set or Not in Codeigniter?

I know how to create session in core PHP, and I have understand how to do this in codeigniter, but I am unable to understand how to check, if the session is set or not? I have tried to check this through View but it always give me the meesage Please Login.
Kindly tell me how can I check whether the session is Set or not Set
Controller
if ($user_type=='Student')
{
if ($LoginData= $this->loginmodel->studentLogin($username,$password))
{
foreach($LoginData as $UserId)
{
$currentId= $UserId->StudentId;
}
//[[session]]
$data['students_data']= $this->loginmodel->student_profile($currentId);
$this->session->userdata('$data');
$this->load->view('students',$data);
}
else
{
//$data['message']= array('Invalid Username or Password');
$this->load->view('Login');
echo "Invalid Username or Password";
}
}
elseif ($user_type=="Faculty")
{
if($data['faculty_data']=$this->loginmodel->faculty_admin($username, $password))
{
$this->session->userdata('$data');
$this->load->view('faculty');
}
else
{
$this->load->view('Login');
echo "Invalid Username or Password";
}
}
VIEW
<?php
if (!$this->session->userdata('$data'))
{
echo "Please Login";
}
else
{
}
?>
<!DOCTYPE
Creating Session:
$newdata = array(
'username' => 'johndoe',
'email' => 'johndoe#some-site.com',
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
or
$this->session->set_userdata('some_name', 'some_value');
But before that ensure that you have the session library included.
$this->load->library('session');
Get Session Data:
if($this->session->userdata('whatever_session_name_is')){
// do something when exist
}else{
// do something when doesn't exist
}
if ($this->session->userdata('variable') !== FALSE) {
echo 'Variable is set';
} else {
echo 'Variable is not set';
}
More info
Demo code
Using has_userdata
if ($this->session->has_userdata('username')) {
return TRUE;
} else {
return FALSE;
}

codeigniter set_userdata works but doesnt output set_flashdata, in the same code

I have a user controller that checks to login a user. It has this snippet of code. I can set set_userdata and echo it in my view, but this doesnt work for my flash data as shown below.
public function login()
{
if($this->input->post('submit_login'))
{
// Get Posted Vars
$username = $this->input->post('username');
$password = md5($this->input->post('password'));
// Run query to return 1 result if TRUE
$query = $this->user_model->check_login($username, $password);
if ($query == FALSE)
{
$this->session->set_flashdata('flashdata', 'Incorrect Username or Password doesnt show');
$this->session->set_userdata('userdata', 'userdata works');
// Query was wrong, set flash data and redirect to login
$data['main_content'] = "user/login";
$data['title'] = "Login";
$this->load->view('template', $data);
}
else // Else Query returned 1 item
{
// Get the Variables
$user_id = $query->row(0)->id;
$username = $query->row(0)->username;
$user_email = $query->row(0)->email;
$user_type = $query->row(0)->type;
// Load up the userdata array
$userdata = array(
'logged_in' => TRUE,
'user_id' => $user_id,
'username' => $username,
'user_email' => $user_email,
'user_type' => $user_type
);
// Execute the set_userdata, to our session
$this->session->set_userdata($userdata);
$data['main_content'] = "Admin/index";
$data['title'] = "Administrator";
$this->load->view('template', $data);
}
}
elseif ($this->session->userdata('logged_in') == TRUE)
{
$this->index();
}
else
{
$data['main_content'] = "user/login";
$data['title'] = "Login";
$this->load->view('template', $data);
}
Then in my view:
<form action="<?php echo base_url(); ?>user/login" method="POST" id="login_form"/>
<p>Please Login Below</p>
<span class="notice"><?php echo $this->session->flashdata('flashdata'); ?></span>
<span class="label">Username:</span><input type="text" name="username"/><br/>
<span class="label">Password:</span><input type="password" name="password"/><br/>
<input type="submit" name="submit_login" value="Login"/>
</form>
<?php echo $this->session->flashdata('flashdata'); ?>
<br/>
<?php echo $this->session->userdata('userdata'); ?>
Why doesnt flashdata show?
flashdata in codeigniter only exists in the session for one additional request and then is destroyed. If you are inadvertently trying to use it past a single request, it will not exist by the time you try and access it. userdata though exists as long as the session exists.
UPDATE
After looking at more of your code, it looks like this happens because you're not actually redirecting (creating the next request) here, you're just loading a view...
if ($query == FALSE)
{
$this->session->set_flashdata('flashdata', 'Incorrect Username or Password doesnt show');
$this->session->set_userdata('userdata', 'userdata works');
// Query was wrong, set flash data and redirect to login
$data['main_content'] = "user/login";
$data['title'] = "Login";
$this->load->view('template', $data); //<---- See?
}
Instead do this...
if ($query == FALSE)
{
$this->session->set_flashdata('flashdata', 'Incorrect Username or Password doesnt show');
$this->session->set_userdata('userdata', 'userdata works');
// Query was wrong, set flash data and redirect to login
redirect("uri/path/to/login");
}
The value of :
echo $this->session->flashdata('flashdata');
Will get on 2nd refresh of the page.
Actually, this value is stored in cookies.

Categories