Unable to store session in Database in laravel 5.2 - laravel

I recently upgraded my application from laravel 5.1 to 5.2 version. Also I am trying to store session in Database which is currently stored in file.
I have changed .env file to include SESSION_DRIVER=database. Also I have changed session.config file to include 'driver' => env('SESSION_DRIVER', 'database') and generated required session table.
But when I tried to login to the application I am getting TokenMismatchException in VerifyCsrfToken.php line 67: If I change SESSION_DRIVER=file then application is working as expected.
But this was not the case in Laravel 5.1 version.
What is changed in Laravel 5.2 version and what steps are required to store user session in database?

I had to deal with this issue myself as well. So the initial steps are as you had already mentioned in your question.
On fresh install of laravel and verify that everything works, change the SESSION_DRIVER=file to SESSION_DRIVER=database
Run the following commands IN ORDER in your console within the directory of your app:
php artisan session:table
composer dump-autoload
php artisan migrate
(This step might be different from what you did) In your config\session.php file search for:
'driver' => env('SESSION_DRIVER', 'file'),
and change it to the following
'driver' => env('SESSION_DRIVER', 'app.database'),
Within the same config\session.php file change the encrypt configuration to true
Set your connection and table name settings appropriately
'connection' => 'database_name_here',
'table' => 'sessions', //here you can change but sessions is recommended until you get more comfortable with how it all works
Attempt to login to your application and check your database to see if it all works.
If more help is needed you can also check a few resources that have helped me solve the issue. Keep in mind these resources are of people having similar issues but maybe your problem is similar to theirs. Only way to find out is to investigate. Hope this helps! =) :
https://laracasts.com/discuss/channels/laravel/how-to-store-users-id-in-session-table-laravel-5
https://laravel.com/docs/5.2/session
(Video tutorial on Laravel Sessions, VERY good place to help you get to where you need to be!)
https://www.youtube.com/watch?v=-FMecyZs5Cg

look it's ok to go with it manual but why don't u use the php artisan make:auth
it creates every thing u need for authentication and also helps u keeping the session instead of doing it manual ?

Related

When and how do Laravel daily logging files get deleted?

I'm running Laravel 5.8 and using the built-in Laravel Log functionality. And I've created a few custom logs.
I set each of them to store the daily log files for 30 days, like so:
'reconciliation' => [
'driver' => 'daily',
'path' => storage_path('logs/reconciliation/reconciliation.log'),
'days' => 30
]
However, the logs don't stay for 30 days, like I've set them to. They stay for 7.
I've heard that there is another configuration called log_max_files, which might be what I'm looking for.
But all the references to log_max_files are from Laravel 5.4 and below. And I know that the logging functionality was revamped in 5.6. So I'm not sure if log_max_files even works anymore and I'd like to test it.
I've created a new log file with a date far outside the current 7-day range and I want to confirm that it gets deleted when the logs get cleared.
I'm just not sure when Laravel clears out the old logs. I couldn't find any in-depth information like that in the docs and my Google searching hasn't turned anything up.
I've tried restarting my server, running php artisan config:clear and php artisan cache:clear with no luck.
Does anyone actually know the process by which these log files get deleted and when that process runs?

How to hide .env passwords in Lumen when you leave debug as false?

How can I hide my passwords and other sensitive environment variables on-screen in Lumen (Not Laravel)?
Sometimes we either forget or test smth in development and make debug=false in the .env file. We maybe don't want other people to see such information even in development phase.
Also for some people who don't know this, if an exception is thrown while opening a page or making a request, everything that is in the .env file is shown in the browser, including db passwords etc - "and this is how you debug"!
I have found a solution for Laravel but I need it for Lumen also!
Solution for Laravel: How to hide .env passwords in Laravel whoops output?
It would be best if we had this as a default setting offer by Laravel team, but after some discussions in Laracast I'm not quite optimistic!
I HOPE DEVELOPERS KNOW THIS ISSUE AND BE VERY VERY CAREFUL BECAUSE FORGETTING IT OR EVEN TESTING IT IN DEVELOPMENT HAS A HUGE HUGE RISK OF BEING HACKED!
Thank You!
You can hide the sensitive information by adding following code in config/app.php
'debug_blacklist' => [
'_ENV' => [
'APP_KEY',
'DB_PASSWORD',
],
'_SERVER' => [
'APP_KEY',
'DB_PASSWORD',
],
'_POST' => [
'password',
],
],
2 options:
Don't install dev dependencies: composer install --no-dev
Use a professional MVC framework
you can totally customize debug output in the ÈxceptionHandler class.
its up to you, how you want the exception to be transformed into a json response

How to stop Laravel 5 from caching configurations?

It is written in Laravel 5 documentation that php artisan config:cache stores all the app configuration into one single file which makes the application load faster.
I want to know two things:
The first thing is, how to force Laravel to stop caching my app configurations? For example, I want Laravel to read the name of my database and the password from the .env file or from the database.php configuration file, not from the cached data.
The second thing is, where does Laravel store this cached configuration file, so that I can delete it when needed? I know there is an Artisan command for that, which is php artisan config:clear, but I want to know where the file stored.
You can't stop the caching of config files since it doesn't happen automatically. The only thing you need to do, is not call config:cache.
The file itself can be found in bootstrap/cache/config.php.
Note that the location of the compiled config file has changed recently. Yours might also be in vendor/config.php or storage/framework/config.php. With a fresh install or if you run composer update the file should be in bootstrap/cache though.
As per the post here: https://github.com/laravel/framework/issues/2501
"Change cache driver to array for the local environment."
For me this involves adding CACHE_DRIVER=array to my .env file. This is then picked up by my config/cache.php file which includes the line:
'default' => env('CACHE_DRIVER', 'file'),
Obviously you could change it directly in the config/cache.php but I'd prefer to use the .env file so I can disable it for development sites and enable for production.

Laravel4 Session handling issue?

I want to use Laravel Database Session. I have changed
'driver' => 'database',
in session.php.
When I try to access application i.e during landing page or before login
I am getting this error
null value in column "data" violates not-null constraint DETAIL
But if this error comes after login, then it is fine. Because that is when session is created. I did not understand why it is coming during application load.
Please let me know what is the issue or Please let me know the tutorial link for session handling in Laravel4
Thanks all
The "data" value you reference doesn't exist in the implementation of the Laravel session handler. The fields are id, payload and last_activity.
If you're trying to use Laravel's session handler via database, you're going to need to run the Laravel migrations.
Go to your command line and run artisan. php artisan session:table will automatically create the Laravel session migrations.
Then run composer dump-autoload or composer.phar dump-autoload (depending on your implementation).
Next, php artisan migrate and your Laravel session table will be created! You'll now be able to use laravel's built-in session handler.
Documentation here: http://laravel.com/docs/session#database-sessions

Laravel 5.1 - how to split logging based on CGI or CLI?

I have Laravel 5.1. It logs CGI (web stuff) with the apache account and that's fine. The problem is if I need to get on there to run an artisan command that I created (that also uses the Log class) it fails because it doesn't have permission to write to the log file that apache created. Is there a way to tell laravel to log to different places based on whether it's via web or cli?
Heading to your config/app.php file you can choose the Log driver according to the executing user.
config/app.php
/*...*/
'log' => php_sapi_name() === 'cli' ? 'syslog' : 'daily',
/*...*/
You can also change the log detail level if you want to mute it completely.

Resources