Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - How decrypt value of cookie

Hello i need to decrypt value of cookie. My code to create and destroy:

  public function setSession($id){
      Cookie::queue('userId', $id, 10000);
  }

  public function destroySession(){
      Cookie::queue(Cookie::forget('userId'));
  }

But i need to get value of cookie without encrypt.

like image 736
J. Defenses Avatar asked Nov 29 '22 13:11

J. Defenses


1 Answers

In web request context cookies are usually automatically encrypted and decrypted by the EncryptCookies middleware. So easiest option would be just to enable this middleware (and it's enabled by default in Laravel).

If you need to decrypt any value manually, the following will do the trick:

// get the encrypter service
$encrypter = app(\Illuminate\Contracts\Encryption\Encrypter::class);

// decrypt
$decryptedString = $encrypter->decrypt($encryptedString);

Check the code of the EncryptCookies middleware to learn more about what it does internally.

like image 143
jedrzej.kurylo Avatar answered Dec 04 '22 03:12

jedrzej.kurylo