session::set and session::get is not working in laravel
Session is working in local machine. But when I test in live server, it is not working.
Scenario: While logging into an application Auth::attempt returns true. But again it redirected to login page. This is an exact issue.
Auth::check method return true under the auth::attempt condition.
But in login page and filter.php, Auth::check method return false.
I don't know why :(
I tested it with a laravel session variable. I assigned a value in a session variable and redirected the page to another page where I have echoed the session. It return empty.
Additional Detail: It does not retain the session flash message in server. It is working in local.
Please advice.
If your session driver is file, then make sure your (storage) path is writable by PHP. Look for this in your session.php file:
'driver' => 'file',
....
'files' => storage_path().'/sessions',
Related
I have previously transferred laravel from server to another by copying the laravel project folder, now when i try to login using default laravel auth page , if i enter wrong auth value i get error auth.failed
but when i enter a valid auth value the page refresh but nothing happen.
I notice from browser view source the value of CSRF token is changing one i enter the valid auth value
I tried to delete the session files /storage/framework/sessions
and tried
php artisan view:clear
php aritsan cache:clear
but nothing happen.
How can i fix this issue.
Use Laravel Debugbar for easily debugging such kind of cases.
It's good, simple and sweet ^
I fixed the issue.
In the previous server i was using SSL for my domain so the cookies setting in /config/session.php was true:-
'secure' => env('SESSION_SECURE_COOKIE', true),
Since, the current new server i am not using https when i open my website, so i set the value to false
'secure' => env('SESSION_SECURE_COOKIE', false),
Now everything working fine.
Both of the files I put together in web.php so that they share the same web middleware. But the session does not Store
I am create a SMS OTP system. In the API\SmsController#create function I run
$rand = rand(100000,999900);
session()->put('otp_test', $rand);
session()->keep(['otp_test']);
After the API request, I can see from Laravel Debugger in the SESSION tab have the session variable.
However, when I refresh the page, the SESSION variable is not there . It only leave
_token Zv3IpiLwwIXTUMc4tMW1J9eJA5lJCliGtdwEvx0e
_previous array:1 [ "url" => "http://sms.test/register" ]
_flash array:2 [ "old" => [] "new" => [] ]
url array:1 [ "intended" => "http://sms.test" ]
PHPDEBUGBAR_STACK_DATA
Also when I do the Form Post, session('otp_test') also NULL.
After I refresh the page. There is opt_test session there.
I tried file, database in session.php both also not persist when I refresh or go to next page.
It seem like flash it after all.
The keep method is for flashed session data. Flashed session variables will end up getting removed. You just turned your regular session variable (that would exist until you remove it, the session is flushed or dies) into a flashed variable that will get removed automatically by calling keep like that.
Laravel 6.x Docs - Sessions - Flash Data
I have a problem regarding sessions in Laravel. I try to redirect the page and at the same time sending some session variables using the with() method:
return Redirect::To('/')->with('foo','bar');
But when the page comes up, the only session variables set are _token and locale, 'foo' and 'bar' do not appear. Running {!! var_dump(Session::all()); !!} gives:
array(2) { ["_token"]=> string(40) "l5NawtJdHJtanTErsya440UvPQIgqNExiryJIkIO" ["locale"]=> string(2) "se" }
The session stored in storage/framework/sessions strangely has other variables set, such as url and PHPDEBUGBAR_STACK_DATA that don't show up when redirecting.
Now, here's the real twist: It works perfectly when run on a different computer.
We tested with the same repository, same code, a fresh installation of laravel, same web browser, same OS (Mac) and same program for running the server locally (MAMP). On another computer it works fine, and on a third computer, but not on mine.
The application is in debug mode and I have tried clearing all caches in Laravel and in the browser nothing changed.
Does anyone have a clue on how this can be resolved?
with() method use to pass data to a view. If you want to add something to session use session()->flash->('foo', 'bar');(automatically erase after next request) or session()->put('foo', 'bar');
Apparently in config/session.php the variable domain was set to the production domain. So when using localhost on my computer, the cookie laravel_session couldn't be read or written.
It worked by using:
'domain' => null,
Alright so this is an odd one. I initially ran into some problems setting session values, but it turned out that it was because I wasn't returning anything from the controller method that was setting the session value.
I resolved that and got my user controller to work, and it's been working just fine.
I'm setting like this in my UserController. It seems like the docs reference a few different ways to interact with the session, but the session() method seems to be the more standard way - at least based on the lumen docs (http://lumen.laravel.com/docs/session).
session([
'is_logged_in' => true,
'username' => $user->getUsername(),
'user' => $user,
]);
And I'm doing my gets like so:
$user = session('user')
Now I'm trying to introduce some new functionality in a separate controller AdminController. I want to set an additional variable in there:
session(['new_variable' => 1])
But that session variable isn't actually saving to the session.
Now here's where it gets weird. I'm doing this with the cookie driver currently. If I change to the file driver then everything works completely as expected.
Also, with the cookie driver, if I set that variable from within the original UserController that's logging them in, as opposed to in the AdminController, then it also properly persists the session data.
But setting that session data from the AdminController using the cookie driver just doesn't work.
I thought perhaps this might have to do with the path setting on the cookie, but it seems that all of the cookie paths are set to '/' (as you would expect). Also there isn't any different domain being used for this other controller - it's on the same domain.
I am working in cakephp. My all code working fine but when I upload the code on server then session functionality does not work.
There are no error on server.Just session does not write or read.
I am using go Daddy hosting.
my core file settings are:-
Configure::write('Session', array(
'defaults' => 'cake'
));
// Session cookie now persists across all subdomains
ini_set('session.cookie_domain', env('HTTP_BASE'));
and temp folder is writable and session id and other values in session are shown in temp folder file.but session still not working.
I got my answer that there is a space problem after php closing tag in my one cotroller so just removed that space like
(?> ) to (?>)