how to use session destroy in CodeIgniter 3 - session

I am working with ci 3 i am trying to destroy session using all possible ways but my session remains same out of that function . it only show right out put on that function, but out function after redirect it is set.
$this->load->library('session');
$this->session->unset_userdata('session_alive');
$name = $this->session->userdata('session_alive');
session_destroy();
Print_r($name);
Print_r($_SESSION);

Related

use laravel session value from function to another in the same controller in api or web route

When i'm trying to set session value in function then using this value it's fine but when i'm trying to reuse the same value which saved in the session with it's key i got undefined key in the array of the session why that's happen and how to handle that i tried to set middleware in the route but it gave me the same error
note: i redirect from my domain to another domain i know that's the problem occur after redirecting how to handle that i need standard solution i used $_cookie and it's worked but i want to use session ???
I'm tring to use the session values from function to another in the same controller after redirecting to another domain and back to my domain can someone tell me how to handle that and you must know that i don't controll the response from the external domain and this domain don't return the data that i want ???
the question here i need session to not expired if i go to this domain then back to my domain ?
try to capture the id after the creation
public function setCaptureOrderId($id) {
Session::put('catpureId', $id);
Session::save();
}
can use this getCaptureOrderId function
public function getCaptureOrderId() {
return $_SESSION['orderID'];
}
then redirect
public function http_create(){
return Redirect::to($url);
}
finally after the redirecting
getCaptureOrderId don't return anything and undefined key

Sessions in laravel 5.3 don't start

I am having problems with storing values in a session. I am using laravel 5.3 on xampp local host. I have tried the following but when I try to access the session is empty:
//using global session helper method
$variable = session(['exam'=>$exam]);
$exam = session('exam');
echo $exam; //exam is always empty
//I have also tried this
public function myControllermethod(Request $request)
{
$exam = $request->session()->put('exam',$exam);
//get the session
$exam = $request->session()->get('exam');
echo $exam // this approach returns the following exception
}
RuntimeException in Request.php line 905:
Session store not set on request.
I have tried both the file and database drivers without lack. Sessions are actually not being registered at all. Session::get('key') also do not work,
How can I start sessions in laravel 5.3. Any Help?

CodeIgniter sess_destroy does not delete user_data session?

I am creating test case for my CodeIgniter app. However I just found something that I thought should not be happen :
in login.php controller :
public function logout()
{
$this->session->sess_destroy();
redirect('/');
}
So I just created a test to just make sure that session is really destroyed :
public function test_logout()
{
$this->CI = set_controller('login');
// make sure that all session is destroyed
$this->CI->session->set_userdata('test_session', 'some_value');
$this->CI->logout();
// userdata 'test_session' should be removed!
$this->assertTrue(($this->CI->session->userdata('test_session')==null || $this->CI->session->userdata('test_session')==''));
}
However I find that upon running the test case, my test case fails! Upon debug on the last line of test case, I found that the userdata is still exist with value = 'some_value'. I thought that sess_destroy should also delete all the set user data, as per what they described in their website documentation:
This function should be the last one called, and even flash variables will no longer be available. If you only want some items destroyed and not all, use unset_userdata().
I am using Kenji's CIUnit for unit testing.
Is this the correct behaviour or is there something that I missed?
Just found that CIUnit routes the Session to CIU_Session instead of original CodeIgniter's CI_Session. It miss a line that CI_Session does :
$this->userdata = array();
So turns out this is CIUnit's issue instead of CodeIgniter's. Create an issue in their bitbucket page.

Codeigniter loading library after session is set

I am using codeigniter with sqlite and I have created a new library for connecting the sqlite database. The sqlite database will connect only if the session is set.
LIBRARY (userdb)
$CI =& get_instance();
if( $CI->session->userdata('user') ){
$userId = $CI->session->userdata('user');
$this->conDb = new PDO("sqlite:" . BASEPATH . "sqlitedb/" . $userId . "/data.db");
}else{
$userId = '';
}
And I execute queries by using $query = $this->userdb->query($sql);
It works fine but the problem is that query only works if the page is refreshed after session is created otherwise it returns Call to a member function query() on a non-object error.
CONTROLLER
$sessiondata = array('user' => $userid);
$this->session->set_userdata($sessiondata);
$insert = $this->user_model->insert($data);
All this seems legit, if you set your userdata while the page/controller is loading it means that the librairies and other assets have been loaded already. So when your userdb library is loaded there is no sessions set yet.
You have two ways to fix this : Try to Load your library after loading your controller but for a db library this would be better to be in your config file so that it is auto loaded.
The other way would be to reload or redirect your current PAGE after the session isset. So that you can use your db library
Hope that helps You
NwK

core php session is lost in codeigniter when redirecting controller to controller

core php session is lost in codeigniter when redirecting from one controller to another.
In the first controller, it works perfectly when we assign value to it. Also, in the first controller, the session is correctly handled but after a redirect to another controller, the session is lost.
Here is first controller:
$_session['user'] = $data[0]['u_name']; // assign value to session
echo $_session['user'] // works fine here and print user name
redirect("useraccount",'refresh'); // redirection to user account
its not working in useraccount.php controller
here second controller
public function index() {
if(isset($_session['user']))
echo $_session['user'];
else
echo "no session";
//$data['main'] = 'users/dashboard';
//$this->load->view('index',$data);
}
it is printing no session. How do I pass the session to 2nd controller after redirecting
to second controller?
note: i want to use core php session and not codeigniter session.
Is there something wrong with:
$this->session->set_userdata('user', $data[0]['u_name']);
PHP variable names are case sensitive. it should be
if (isset($_SESSION['user'])) {
^^^^^^^--- note: all CAPS
Use session_start() property to access core php session
Try this
public function index()
{
session_start();
if(isset($_session['user']))
echo $_session['user'];
else
echo "no session";
//$data['main'] = 'users/dashboard';
//$this->load->view('index',$data);
}

Resources