Laravel Redis behavior for different browsers

Viewed 472

I am facing problem in Redis in laravel framework. Actullay. I have done almost everything. I am putting and getting data in Redis like this:-

use Illuminate\Support\Facades\Redis;
public function redisSet(){
    Redis::set('name', 'Taylor');
    echo "redis set successfully"; die;
}
public function redisget(){
    echo Redis::get('name'); die;
}

Now there are two urls like below:-

http://localhost:8000/redis-set
http://localhost:8000/redis-get

Both above url working fine. Now problem is when i hit the set url in Google chrome and trying to get in mozilla firefox its also printing in mozilla firefox. that must not happen. If set redis in Google chrome its must be get in google chrome only not other browser. See images below:- enter image description here

Now when i hit the get url in uc browser. its data is showing. but it must not happen. because i have set the redis in google chrome. enter image description here

Below is my database.php file :-

'redis' => [

    'client' => 'predis',

    'default' => [
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', 6379),
        'database' => 0,
    ],

],

My env file:-

BROADCAST_DRIVER=log
CACHE_DRIVER=redis
SESSION_DRIVER=redis
SESSION_LIFETIME=120
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

I have also installed the prdeis of larvel. Please help me how to resolve this issue. My system is connected with network when i access the same url in other system its also showing the redis data. Please help me to resolve this issue.

4 Answers

Redis is a server side storage service just like mysql. It communicate with php not browser, and gives you back what you stored before.

If you want different data save for different user, try session and use Redis as session driver. HTTP Session

For this kind of behaviour that you are looking for use Session instead of Redis. because Redis is a database which can be used as a session driver in Laravel

public function redisSet(){
    Session::set('name', 'Taylor');
    echo "redis set successfully"; die;
}
public function redisget(){
    echo Session::get('name'); die;
}

Redis is a server-side service, so there is no matter what browser you are using. You can use $request->header('User-Agent'); to determine what browser is used, but this is not the best way. Instead of using user-agent header i recommend you to use cookies/session, coz it's independend for each browser. Then you will be able to work with redis data given their source.

As I mentioned on my comment, redis works on your server, not on users browsers. If you want to store different values for different browsers. You need to check users browser first and store them with different key.

I suggest you to use this browser detect package. You can easly install via composer.

after installed the package;

switch(Browser::browserFamily()){
        case "Chrome":
            Redis::set('chrome', 'Taylor');
            break;
        case "Firefox":
            Redis::set('firefox', 'Hasan');
            break;
        case "Opera":
            Redis::set('opera', 'Kunal');
            break;
            // etc
}

Then you can easly to access these values using their keys

Related