Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Redis for storing Sessions in Laravel 5.6 but facing browser issue #5275

Open
amitrtpl opened this issue Aug 24, 2018 · 6 comments
Open

Comments

@amitrtpl
Copy link

I am using Redis in Laravel 5.6 for storing Sessions but its updating in all browsers like when I add session_id variable and sets its value in Redis then it not only assigned and displays in one browser but also to other.

Like if I assign some value to Redis variable in Chrome browser then it also shows in Firefox browser as well.

But I want to maintain separate Redis Session in separate browser like it happens with standard PHP Sessions.

Please check my below code where I assign the value to Redis session_id variable if its empty.

$session_id = Redis::get('session_id');
if(empty($session_id)){
$session_id = str_random(40);
Redis::set('session_id',$session_id);
}

Please help me to modify this code to make it browser specific.

How do I assign the value to Redis Session variable that can be read by one browser only, not by other.

Hope someone can help me! Thanks.

@oatmi
Copy link

oatmi commented Aug 24, 2018

may be php builtin function session is what you wanted.

set your php.ini correctly:

[Session]
session.save_handler = redis
session.save_path = "yourredishost.com:6379"

hope this will help

@amitrtpl
Copy link
Author

Thanks for your reply but still the issue is there even after making changes in php.ini file.

Please suggest any other alternative.

@oatmi
Copy link

oatmi commented Aug 24, 2018

You are using $_SESSION now right ?

session_start();
$session_id = $_SESSION['session_id'];
if (empty($session_id)) {
    $_SESSION['session_id'] = str_random(40);
}

@jcstanaway
Copy link

jcstanaway commented Aug 24, 2018 via email

@oatmi
Copy link

oatmi commented Aug 24, 2018

A common pattern is to define a key namespace for each
client instance where each key has some client specific prefix such as
'browser1:session_id' and 'browser2:session_id'.

I think that is exactly why people use $_SESSION, while user request a server from one browser, he will get one uniq session_id, which you can see in you browser:
image

And when you use $_SESSION['some_key'] = 'foo' the some_key => foo will be save to redis under key "vBpFLcN_0Bgjfz-PzuukBbEZyHlOlvO...."

data from my own redis server:

127.0.0.1:6379> keys *
1) "PHPREDIS_SESSION:44frugm6og7bduvcaou5smk2b0"
127.0.0.1:6379> get "PHPREDIS_SESSION:44frugm6og7bduvcaou5smk2b0"
"Yii.CCaptchaAction.2c8775f8.site.captcha|s:4:\"nxwu\";Yii.CCaptchaAction.2c8775f8.site.captchacount|i:1;"

In your case, If you set
$_SESSION['session_id'] = 'AAA' for user one, and
$_SESSION['session_id'] = 'BBB', in redis, it would be:

PHPREDIS_SESSION:44frugm6og7bduvcaou5smk2b0 : ['session_id' => 'AAA'] # user one
PHPREDIS_SESSION:djasdjlasjdkaskdjslakjdlks : ['session_id' => 'BBB'] # user two

@itamarhaber
Copy link
Member

Hello @amitrtpl

This appears to be a question rather than an actual issue - please close it.

Keep in mind that this issue tracker should be used for reporting bugs or proposing improvements to the Redis server. Questions should be directed to the community:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants