The sessions are dying on redirect - laravel

The sessions are no longer working on redirect, if i set a session with session([ 'key' => 'value' ]); is working on the same page, but if i set the session in controller, after redirect is NULL, also, when i redirect, the values passed ->with(array) are NULL:
return redirect()->route('account.login', ['subdomain' => 'account'])->with(
[
'request-tfa' => true,
'requested-email' => $validatedData['email'],
'requested-password' => encrypt($validatedData['password'])
]);
When i get the value using Session::get('request-tfa') is NULL, until now, this worked perfectly.
Do anyone have any idea how to fix it?
Should i try to use a database as session storage? but i don't know how to setup the database to store the sessions.
EDIT: I changed the session driver to database and is still not working, but the sessions are stored.
And even \Auth::attempt is not working, like, if i use:
if(\Auth::attempt(['email' => $validatedData['email'], 'password' => $validatedData['password']]))
{
return \Auth::id();
}
it working, but if i have return redirect i am guest again.
EDIT X2
Well, i changed my virtual host domain and restarted my pc and now is working...

If you want to have a session after redirect you should use:
session()->put('something')

Related

Two CodeIgniter App Session Overiding Eachother

so i have 2 ci app on my server(different project folder) and the session overiding each other when accessed with same browser.
i just notice this, because i think its gonna be different by default even the session variable name is same.
i find some problem here when im set_userdata and sess_destroy it applying to both of the app.
i use pretty much same variable on each other
$data_session = array(
'EMP_ID' => $data['EMP_ID'],
'EMP_FULL_NAME' => $data['EMP_FULL_NAME'],
'EMP_F_NUM' => $data['EMP_F_NUM'],
'EMP_EMAIL' => $data['EMP_EMAIL'],
'LEAD_STATUS' => $data['LEAD_STATUS'],
'BRANCH_ID' => $row_br['BRANCH_ID'],
'BRANCH_NAME' => $row_br['BRANCH_NAME'],
'BRANCH_CODE' => $row_br['BRANCH_CODE'],
'DEPART_ID' => $row_dep['DEPART_ID'],
'DEPART_NAME' => $row_dep['DEPART_NAME'],
'STATUSLOGIN' => "LOGIN",
'CONTINUE' => 'NEW'
);
$this->session->set_userdata($data_session);
how do i make it different from each other? do i need to make a different variable when set_userdata on each app?
My guess is you are probably using the copy of an old project to create a new project. In doing so you also need to change the Config variables for sessions in the application/config/config.php file.
So you need to update one session name variable.
$config['sess_cookie_name'] = 'second_project_session';

Testing setting she session used in the intended()-method in Laravel

Laravel has a intended()-method used for redirecting users after a login. It uses the session key url.intended, as seen here. However, when testing it, doesn't seem to work. I set the session like this: session(['url.intended' => url()->previous()]);
Then this is how I test the session:
$this->app['session']->setPreviousUrl('some-url');
$response = $this->get(route('login'));
$response->assertSuccessful();
$response->assertSessionHas('url', 'some-url'); //fails
$response->assertSessionHas(['url' => ['intended' => 'some-url']]); //fails
When not using dot-notation as key, it works. Meaning, I can assert a session with key urlIntended exists.
How do I go about this?
I just realized that using the intended()-method actually returns AND removes the session since it is using pull().
Here is how it is used more specifically: $path = $this->session->pull('url.intended', $default);

remember me functionality in codeigniter

I have implemented remember me functionality as this question
How to create "remember me checkbox" using Codeigniter session library?
first answer.
I created a cookie with a random number code as value and it stored in db(user table). On login, db code checks with cookie value.It works fine on my localhost server. But in live server which has a subdomain url has problem.Also I tested it with another server with ip address as url. There also it is not working. In both cases cookie created but cant read the cookie. Please help me.
cookie set by
$auto_login_hash_code = uniqid();
$domain = $_SERVER['SERVER_NAME'];
$cookie = array(
'name' => 'rememberMe',
'value' => $auto_login_hash_code,
'expire' => 31536000,
'domain' => $domain,
'path' => '/'
);
$this->input->set_cookie($cookie);
and reading cookie by
if (get_cookie('rememberMe')) {
$hashcode = $this->CI->input->cookie('rememberMe');
$this->CI->load->model('loginmodel', '', true);
$username = $this->CI->loginmodel->get_username_by_hashcode($hashcode);//in this function setting session variables
}
Thanks in advance
iijb
you are getting library for that on github.
search remember me on github, load it and just follow below steps.
Verify cookie if token is present in database go to home page
$this->load->library('rememberme');
$cookie_user = $this->rememberme->verifyCookie();
if ($cookie_user)
{
$this->load->view('search_view');
}
else
{
// If checkbox is checked it return true either false
$checked = (isset($_POST['Checkbox1']))?true:false;
if($checked== true)
{
//$this->load->view('tested');
$this->load->library('rememberme');
$this->rememberme->setCookie($this->input->post('loginemil'));
//$this->rememberme->setCookie('set cookie here');
}
else{
dont set anything
}
}
Also this can be done by editing/extending system Session library.
First: In user login function add remember me check-
if($remember)
{
$data['new_expiration'] = 60*60*24*30;//30 days
$this->session->sess_expiration = $data['new_expiration'];
}
$this->session->set_userdata($data);
Second: Edit system Session library [I am not sure whether extending Session will work or not]
Go to this line in sess_read() method
if (($session['last_activity'] + $this->sess_expiration) < $this->now)
Before that line add following code
if(isset($session['new_expiration'])){
$this->sess_expiration = $session['new_expiration'];
}
This works fine for me.

Session data gone after redirect in CI

i need your help.
I used the session to record the user selected business type in CI. For example,$this->ci->session->set_userdata('biztype','food'). When user login,it works ok. However, once the user logout, session will be destroyed in the function logout().So i set the userdata again in the function logout().You can view the code below:
function logout()
{
$biztype = $this->ci->session->userdata('biztype');
$this->delete_autologin();
$this->ci->session->set_userdata(array('user_id' => '', 'username' => '', 'status' => ''));
$this->ci->session->sess_destroy();
$this->ci->session->set_userdata('biztype',$biztype);
//echo $this->ci->session->userdata('biztype'); //here, i can get biztype that i want
}
However,when i logout and redirect to homepage, i cant get the userdata('biztype') and my session_id have changed.
Thanks for the help.
This is straight from CodeIgniter User Guide:
Destroying a Session
To clear the current session:
$this->session->sess_destroy();
Note: 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().
So no, you cannot destroy a session then add user_data to it, you need to reload / redirect then once the NEW session is established add data.
Try using cookies for peristance, or use the mentioned unset_userdata() fn.
$this->session->sess_destroy() ;
This function should be called only at the end of the execution. For unsetting data (as you're trying to do) it's better to use unset_userdata method. See how you should implement that:
$unset_items = array('user_id' => '', 'username' => '', 'status' => '') ;
$this->ci->session->unset_userdata( $unset_items ) ;
$email = "abc#gmail.com";
///set the session
use the set_userdata function and include the session library
$this->load->library('session');
$this->session->set_userdata('session name',Value);
i.e.
$this->session->set_userdata('email', $email);
//unset the session
$this->session->unset_userdata('session name');
i.e.
$this->session->unset_userdata('email');

Problems with cookies / MAMP / CodeIgniter

I'm having a problem with reading cookies on localhost, using MAMP and Codeigniter.
I'm trying to ude a cookie to authenticate acess to an admin area. I can set the cookie (I see it on my browser - Chrome) but i'm not able to read it after doing that to grant the acess. I've already tried lots of configurations, but none works. I really need some help on this.
Those are the essencial parts of my code:
Here I set the cookie
$cookie = array(
'name' => 'login',
'value' => 'true',
'expire' => '0',
'secure' => TRUE
);
set_cookie($cookie);
Here I redirect the user to login page if there is no cookie and to control panel if the cookie is set
function login_redirect() {
$this->load->helper('cookie');
if (uri_string() == 'admin/controlpanel') {
if ($this->input->cookie('login')) {
} else {
redirect('admin/');
}
}
if (uri_string() == 'admin') {
if ($this->input->cookie('login')) {
redirect('admin/controlpanel');
}
}
}
OBS: all this code is in the admin_model
Any tips?
Thanks and sorry about my english. I hope I've made myself clear.
Codeigniter has some problems with the Cookie and Session libraries when run on some localhost configurations. You'll spend hours trying to find out the particular problem with your setup. The best bet is to use generic PHP cookie/session when on localhost and use another library when in testing.
I appreciate that this is by no means the best solution but it's the best advice I can offer.

Resources