Laravel 5.8 cookie value is different from what I set - php

I am trying to upgrade from Laravel 4.2 to 5.8, I saw that I can still use Cookie::forever and Cookie::get to work with cookie so I didnt had to change my code but the weird thing is that the cookie is set with the correct name but his value is very different (encrypted I guess) from what I set, I am doing this :
// login page
$cookie = Cookie::forever('session_id', 'test');
return redirect('/')->withCookie($cookie);
// index page
$cookie = Cookie::get('session_id');
echo $cookie; // return something like eyJpdiI6ImZqdnF2NkVkbVc4VGJiS3FSYzk4VWc9PSIsInZhbHVlIjoiTkgrVW
This was working fine on 4.2, did I miss something ?

Found a solution here https://stackoverflow.com/a/55223542/561670, its a decrypt issue, this is working :
\Crypt::decryptString(Cookie::get('session_id'))

Related

Laravel - DecryptException: 'The MAC is invalid'

In laravel for registration I'm using encrypt algorithm for password instead of inbuilt bcrypt function in Laravel because to get password and send it to mail when password is forgot.
But decrypt it is showing a error like
DecryptException The MAC is invalid in Encrypter.php (line 184)
This , when I run this code it is working on local but server itself it is not working below i have mentioned the code , can anyone please help
public function forgotpassword(Request $request)
{
$email=$request->email;
$selectemail = User::select('email','password','name')
->where('email',$email)
->first();
if($selectemail)
{
$password=decrypt($selectemail->password);
$data = array( 'email' => $selectemail->email,'password' => $password , 'name' => $selectemail->name);
Mail::send('email.resetpassword',$data,function($message) use ($email)
{
$message->to([$email])->subject('Forgot Password Letgo');
});
echo "Mail has sent successfully";
} else {
echo "This email is not yet registered";
}
}
The problem is you generated a new APP_KEY, then if you try to decrypt the old encrypted data it will show the DecryptException: The MAC is invalid.
If you want to decrypt the old data you need to restore your old APP_KEY.
After realizing that, now, adding a new problem there, if you stored new data with another APP_KEY or another encryption method you have a problem on the data because they are mixed on the table.
In case you don't know when do you started with the new encrypt method or differentiate the new encrypted entries, the fastest solution would be reset all the passwords with the new encrypt method.
You can learn more about how Laravel encryption works on the official Laravel docs.
I copied the APP_KEY from the environment it was working dev to the production and the issue was solved. you may want to try it.
In case none of the above helped you, as it was in my case, well, some people mention clearing the cookies, sadly that is ambiguous to say the least.
I tried everything from the above, clear cache in laravel and the browser, hard reload and all..With no success!
SOLUTION: just CLOSE the browser entirely, and reopen it. In my case, I was using both Chrome and Opera, and they were both messing up. I had to close them BOTH, then reopen them for the MAC problem to disappear.
To avoid this, use a custom key instead. The default key is APP_KEY, but you can provide one so your decrypt is not linked with new or old APP_KEY.
I use the following code to resolve it, and it worked in different APP_KEYs.
function customCrypt($vWord){
$customKey = "blabla_key_with_correct_length";
$newEncrypter = new \Illuminate\Encryption\Encrypter( $customKey, Config::get( 'app.cipher' ) );
return $newEncrypter->encrypt( $vWord );
}
function customDecrypt($vWord){
$customKey = "blabla_key_with_correct_length";
$newEncrypter = new \Illuminate\Encryption\Encrypter( $customKey, Config::get( 'app.cipher' ) );
return $newEncrypter->decrypt( $vWord );
}
Important for key length : if $cipher == 'AES-128-CBC' use $length === 16, if $cipher == 'AES-256-CBC' use $length === 32). Check in config/app.cipher which cipher your app uses.
If you run multiple project and passes one encryption key to another project, Just make sure you have made same APP_KEY to your both project.
App key matters in encryption and decryption. I was having 2 sub
domains with different projects in which I was encrypting value on sub
domain and 1 and trying to decrypt on sub domain 2. Issue was resolved
when both projects were having same appkey. Note: No projects should
have same appkey!!!
If you have imported DB form one environment to another, most likely you will face this error. Its recommended to have same APP_KEY as data source application in order to fix bug.
If you have run 2 different Laravel applications locally in the same browser, cookies can be saved in your browser. Please clear the cache and cookies before loading the 2n app.

I can´t get my SESSIONS variables in laravel

In a controller, I have my method post_sessions(), and I save some variables:
session_start();
$_SESSION['sessions'] = $sessions;
$_SESSION['sessions2'] = $sessions2;
session_write_close();
This method works fine, if I make a return of $_SESSION['sessions'] it will show my variable.
The problem is when I want to get the session back, in other function I have this:
session_start();
$var = $_SESSION['sessions']
But if I make this, I get error 500...
I have put in all htmls... but it doesn´t work. Also I tried with Session::put('key', 'value') of Laravel 5, but it doesn´t work too.
Someone can helpme?

Can't create cookie in Laravel5

I'm having problems to create cookie in Laravel5. The code is below.
$input = $request->input('first');
if($request->hasCookie('first'))
{
return response()->json('ok');
}
else
{
Cookie::make('first', 'first', 1);
return response()->json('no');
}
I also tried this:
public function createCookie(CookieJar $cookieJar)
{
$cookieJar->make('first', 'first', 1);
}
Nothing works for me, can someone illuminate me?
Thanks in advance. When i try to var_dump and get the cookie value it gives me NULL.
By doing this Cookie::make('first', 'first', 1); , you are creating a cookie it doesn't mean it is already set. you have to send cookie with response.
use below code:
$cookie=Cookie::make('first', 'first', 1);
return response()->json('no')->withCookie($cookie);
Cookies are sent only a request after you created them.
An easier way to check if cookie exist or not is to check http headers in the browser or resources->cookies in Chrome.
Also you didn't provide the dd/var_dump code you used or neither why you need to use a new cookie instead of using Laravel Session::flash

How to retrieve http get parameters in link in Laravel?

I am redirecting users from one laravel website to another for single authentication.
This is the redirect code in the website 1
return Redirect::to('http://example.com/login?param1=paramValue');
In website 2, I'm trying to get value of param1
$param1 = Input::get('param1');
but get() function is not returning the value.
IF I try Input::has('param1') , it returns false.
Can someone please guide me on what's going wrong with my code?
The code is working fine for me,
$param1 = Input::get('param1');
return $param1;
Just try to update your framework using composer update

Symfony2 not saving sessions properly

I'm having a problem with Symfony creating a new session on each page load, rather than carrying data across requests. The auto_start in the session section in the config.yml is set to false, and regular php sessions work fine. It's only when running in symfony that I get the problem.
For example, I created the test action:
public function sessionTestAction()
{
$s_Response = '<html><head></head><body><p>Foo</p></body></html>'; //Initialize response and headers
$a_Headers = array();
$i_StatusCode = 200;
$oSession = $this->get('session');
var_dump($oSession->all());
if(!$oSession->has('Test'))
{
$oSession->set('Test', 'Bar');
}
$oSession->save();
return new Response($s_Response, $i_StatusCode, $a_Headers);
}
The expected action is, that on the first page load, the var_dump will yield nothing, and that on any subsequent executions, it will contain Test=>Bar. However, it never gets that data across requests.
In addition, it creates a new session id for each request.
I am using Symfony v2.0.15, and PHP v5.4
Anyone have any ideas?
Edit:
I made some progress, I think. I made the following changes to the test action:
public function sessionTestAction()
{
//Initialize response and headers
$oRequest = $this->get('request');
$a_Headers = array();
if (isset($oRequest->headers->all()['cookie']))
{
$a_Headers['Set-Cookie'] = $oRequest->headers->all()['cookie'];
}
$i_StatusCode = 200;
$oSession = $oRequest->getSession();
$oSession->start();
$s_Response = print_r($oSession->all(), true);
if(!$oSession->has('Test'))
{
$oSession->set('Test', 'Bar');
}
$oSession->save();
$oResponse = new Response($s_Response, $i_StatusCode, $a_Headers);
return $this->render('Bundle:Default:index.html.twig', array('response' => $s_Response), $oResponse);
}
Where that twig file has just {{response|raw}}. It now holds the session for 2 out of 3 of the requests. However, on the third request, it's cleared.
Turned out the problem was, someone added a line to set a session cookie whenever the app.php was run, not knowing that symfony handled sessions itself, I guess. Problem solved.
I got this problem a couple times, its very annoying. So, let me describe possible solution.
Open dev environment - yourdomain.com/app_dev.php/ Try to refresh page a couple times. If you see session ID changed each time - it means that sessions are broken.
If you are using chrome (if not - you should, its the best for developers ;) ) - you can open developers tools (click F12).
Next, check Network tab, refresh page and locate your main request.
Check headers for your request - if should see "Cookie:PHPSESSID".
If you dont see - something wrong with cookies. In my case it was
framework:
session:
cookie_domain: mydomain.com

Categories