I want to use database (currently, MySQL) to store session. But, when I tried to config, table sessions still has no record.
In file config/session.php when I change 'driver' => env('SESSION_DRIVER', 'file'),
to 'driver' => env('SESSION_DRIVER', 'database'),
or 'driver' => env('SESSION_DRIVER', 'redis'),
What should I change for 'connection' => null,?
I tried:
'connection' => 'mysql', with mysql is configured in app/database.php
'connection' => 'database.connections.mysql',
'connection' => Config::get('database.connections.mysql'), got class not found error
All you need to do is specify the right driver in the environment file, and generate the schema as outlined in the documentation.
This line:
'driver' => env('SESSION_DRIVER', 'database')
This is telling the config to get the SESSION_DRIVER variable from your environment file, and use database as a default. I can only assume you have forgotten to update your .env file.
I hade to change SESSION_DRIVER=database in .env file
config/session.php changes didn't work for me
Related
Im currently using laravel queue and works perfectly fine when I only have 1 database connection. But now that I need 2 database connection (one for local, one for production). How can I set the local as my connection for my database queue?
What I tried
queue.php
'database' => [
'connection' => 'mysql_local',
'driver' => 'database',
'table' => 'jobs',
'queue' => 'default',
'retry_after' => 90,
'after_commit' => false,
],
So I've added a connection key but queue still using my other connection.
.env
DB_CONNECTION=mysql
DB_HOST=PRODUCTIONIP
DB_PORT=3306
DB_DATABASE=core3
DB_USERNAME=adm
DB_PASSWORD=secret1234
DB_CONNECTION_LOCAL=mysql_local
DB_HOST_LOCAL=127.0.0.1
DB_PORT_LOCAL=3306
DB_DATABASE_LOCAL=core3
DB_USERNAME_LOCAL=root
DB_PASSWORD_LOCAL=secret
I need to use the mysql_local connection, but no idea on how to use it.
I have read almost 100+ questions in stackoverflow about this. But nothing helped me.
My problem:
Laravel Session::token is re-generated in each HTTP request when I upload my app in my shared hosting. The app works fine when the domain has https://. But if I load the app using http://, no forms work (shows 419 Page Expired). The app also works fine in my localhost (Windows 10 with apache server) even though it's also http://localhost.
I can simply force to HTTPS using .htaccess to ignore the issue. But I want my app to be functioning in non-SSL domains also.
If I hit https://example.com/ 10 times, dd(Session::token()); returns the same token xZYdXs5UuXUSCZ5wHGELad9GuqjimkrPhMlsMepE every time.
And if I hit http://example.com/ 10 times, dd(Session::token()); returns different random tokens each time.
My Hosting Server: Litespeed
Laravel Version: 6.2
PHP Version: 7.4
.env file
SESSION_DRIVER=file
SESSION_LIFETIME=120
config/session.php file
use Illuminate\Support\Str;
return [
'driver' => env('SESSION_DRIVER', 'file'),
'lifetime' => env('SESSION_LIFETIME', 120),
'expire_on_close' => false,
'encrypt' => false,
'files' => storage_path('framework/sessions'),
'connection' => env('SESSION_CONNECTION', null),
'table' => 'sessions',
'store' => env('SESSION_STORE', null),
'lottery' => [2, 100],
'cookie' => env(
'SESSION_COOKIE',
Str::slug(env('APP_NAME', 'laravel'), '_').'_session'
),
'path' => '/',
'domain' => env('SESSION_DOMAIN', null),
'secure' => env('SESSION_SECURE_COOKIE', false),
'http_only' => true,
'same_site' => null,
];
Permission:
/storage 0755
/storage/framework/sessions 0755
Please let me know if I should share any relevant information to make the issue more clear.
Thank you.
Sometimes (about every 20 request) I get this error. But the next (next second), the same request, is fine. I dont know why it failed the first one. Sometimes i can get another error :
No supported encrypter found. The cipher and / or key length are invalid.
My .env database parameters are fine.
I have generated a key using php artisan key:generate
This key is in my .env file under a APP_KEY key
My config/app.php has a key 'key' => env('APP_KEY'), 'cipher' => 'AES-256-CBC'
does anyone have ANY idea how this can happen?
Just cache your config using
php artisan config:cache
Don't forget to do this every time after setting your .env file.
I had this exact same problem for the past few days and I think I solved it:
The settings in .env are not always used for some reason or other and occasionally Laravel will just use the default settings in config/app.php and config/database.php.
config/app.php:
'key' => env('APP_KEY', 'SomeRandomString'),
'cipher' => 'AES-256-CBC',
Change the 'SomeRandomString' to the generated key from your .env
config/database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Change localhost, database, username, password to your actual settings from the .env. This example is for MySQL if you use another database, change those variables instead.
There might be a better solution (more secure?) but this is what so far kept the error from showing up.
You have to delete bootstrap/cache/config.php
I just experienced this after merging in changes from git without updating the .env file.
Basically, the person changed the code so that the application required these keys in the env file:
DB_SOME_DATABASE=something
DB_SOME_USERNAME=something
DB_SOME_PASSWORD=something
But I still had the old credentials in there, so it "looked correct", but Laravel was throwing this error that made it look like the env file wasn't being used.
This would indicate that, if you see this, check very closely if anything is spelled wrong in your .env file that would cause Laravel to attempt to use the default values (ie: look in the folder config/database.php where you see 'forge').
It may not be a typo. Someone may have added a database connection and maybe you haven't updated your .env file yet.
I am using redis as a session driver and I want to clear the cache while keeping the session data, so basically user can stay logged in. Any suggestions regarding restructuring or handling the current situation?
Note: I don't want to use separate redis instance for sessions and other cache data.
Intro
By default, redis gives you 16 separate databases, but laravel out of the box will try to use database 0 for both sessions and cache.
Our solution is to let Redis caching using database 0, and database 1 for Session, there for solving the session clear by running php artisan cache:clear problem.
1. Setting up Session Redis connection
Modify config/database.php, add session key to the redis option:
'redis' => [
'cluster' => false,
'default' => [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
'session' => [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 1,
],
],
2. Make use of the session connection
Modify config/session.php, change the following:
'connection' => null,
to:
'connection' => 'session',
3. Using Redis as session driver
Modify .env, change SESSION_DRIVER:
SESSION_DRIVER=redis
4. Testing out
Execute the following artisan command, then check your login state:
php artisan cache:clear
If the login state persists, voilĂ !
I don't know Laravel, but in general the best two options would be:
Change the format of the cache keys. You should use versioned cache keys so you can do it in the future, i.e. "cache.1." so you can increment and then it makes all your keys irrelevant at once.
Move the cache to a different db number in the same redis instance. That way you can also later do FLUSHDB on that db number to clear the cache.
In both options, after you first do it, if the cache keys are not time-expiring, you should create a script that uses SCAN to remove old keys. See http://redis.io/commands/scan
As a side note, it's usually a bad idea to keep cache and other things in the same redis instance, as in caches you usually use LRU based eviction, and you don't want to mix that with less volatile keys.
https://laravel.com/docs/5.2/redis#configuration
'redis' => [
'cluster' => false,
'default' => [
'host' => '127.0.0.1',
'port' => 6379,
'database' => 0,
],
],
There is 'database' in redis connection options, just select different databases for session and cache. I just hope that redis cache driver uses flushdb not flushall for flushing :).
Laravel Cache::clear() sends the Redis flushall command which will dump everything so not very useful in my experience. You will need to extend the cache class and create a custom set to index the cache data you want to be able to clear. Then build an another function to read the set and issue Redis del() command for each key in the set. Post some working code and will detail further if necessary
Notagolfers suggestion of separation of cache and session into different redis databases isn't a had call but you will still need to extend the cache class to implement the Redis database config switch
For the laravel 9 (it is similar for an older ones):
1.update session configuration
you must put in .env:
SESSION_CONNECTION=session
that configuration is then loaded at config/session.php
'connection' => env('SESSION_CONNECTION'),
That tells laravel to store sessions in separated redis/database connection
2.update redis configuration
add new redis connection at the config/database.php for sessions
'redis' => [
...
'session' => [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
...
]
you can use and database number from 0 to 16. For other connections put different database number.
3.clear cache(without touching sessions)
now you can clear the redis cache with this piece of code:
$redisConnection = \Illuminate\Support\Facades\Redis::connection('default');
$redisConnection->flushDB();
TIP: You can put that code in custom command and run it with the artisan
BONUS: You can see and clear all redis data for each connection with:
$redisSession = \Illuminate\Support\Facades\Redis::connection('session');//session|queue|default
$redisSessionKeys = $redisSession->keys('*');
$redisSession->flushDB();
dd($redisSessionKeys);
BONUS2: You can also add "redis database" for queues and then you have queue jobs, sessions and cache separated and you can clear only one of them
'redis' => [
...
'default' => [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
'session' => [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 1,
],
'queue' => [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 2,
],
]
I have added DB credentials as environment variables (using nginx), so that i can use them like so:
return array(
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => getenv('DB_NAME'),
'username' => getenv('DB_USER'),
'password' => getenv('DB_PASS'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
)
);
The problem is that when I use artisan the environment variables do not seem to be available, so when i run migrate or seed i get errors.
Is there a way around that or should i just write my DB credentials directly in my config file ?
To edit my previous answer (sorry for misunderstandig):
Yes, environment variables are created by server, so they can't be reached or modified from CLI. Before deploying, server is generating those variables, so they can be "injected" into application at runtime.
I am thinking that it is maybe possible to reach those variables through remote Laravel package and SSH ? For example php artisan tail command is reading locally errors from the server side.