3

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.

kunal
  • 255
  • 3
  • 17
  • Why "_must that not happen_"? That sounds like expected behaviour. Have you coded anything to prevent that behaviour? – brombeer Aug 27 '18 at 09:47
  • no i don't done anything can you guide me how can i resolve this thing please. – kunal Aug 27 '18 at 09:49
  • or any useful link that can help me.. @kerbholz – kunal Aug 27 '18 at 09:54
  • if I understand correctly, you want to show users different record by their browser. For that, you need to check users browsers and store the values with different keys which depends by browsers. Redis works in your server, not in browser, so if you're saving values with same key in your redis driver. it's gonna show same value on all browser, because value coming from your server not user browser – Teoman Tıngır Aug 27 '18 at 10:06
  • @HasanTıngır thanks fr your reply. I have connected to one network then why same redis session is working on another system? – kunal Aug 27 '18 at 10:14
  • Probably they are sharing same server, and using same user for connecting redis server. Did you configured before? Also check my answer, You can use below way to do what you want – Teoman Tıngır Aug 27 '18 at 10:18
  • 1
    Redis is run server-side, like MySQL. You might want to look into `Local Storage` if you want to save/store client-specific values on the client. – brombeer Aug 27 '18 at 10:31

4 Answers4

1

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

Gavin Kwok
  • 98
  • 6
1

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;
}
FULL STACK DEV
  • 15,207
  • 5
  • 46
  • 66
0

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.

0

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

Teoman Tıngır
  • 2,766
  • 2
  • 21
  • 41
  • thanks fr your reply. I have connected to one network then why same redis session is working on another system? – kunal Aug 27 '18 at 10:18
  • Probably they are sharing same server, and using same user for connecting redis server. Did you configured before? – Teoman Tıngır Aug 27 '18 at 10:18
  • Dear Tıngır as you told to get browser to make unique but how can i make sysytem unique if it connects to same network? – kunal Aug 27 '18 at 10:25
  • Well, you want to make system unique, using directy redis is not good practise for it. I suggest you to use session driver because per user has their own session – Teoman Tıngır Aug 27 '18 at 11:00
  • @kunal you can change session driver as redis then ? `'driver' => env('SESSION_DRIVER', 'file'),` change file as redis – Teoman Tıngır Aug 27 '18 at 11:27
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/178850/discussion-between-kunal-and-hasan-tingir). – kunal Aug 27 '18 at 11:31
  • @kunal, I lost my connection,.. in my opinion, it's shouldn't be a problem but you may want to consult another users – Teoman Tıngır Aug 27 '18 at 11:40