Issue in cookie

I’m using cakephp3.8.
When page load below code successfully create cookie

$this->response = $this->response->withCookie(‘user_temp_cookie’, [‘value’ => json_encode(time().rand(111,999)), ‘path’ => ‘/’, ‘httpOnly’ => true, ‘secure’ => false, ‘expire’ => strtotime(’+1 year’)]);

But when want to create cookie through a ajax it is not working.

Shows

[ Deprecated (16384)](javascript:void(0);): Cake\Http\Response::withCookie(string $name, array $data) is deprecated. Pass an instance of \Cake\Http\Cookie\Cookie instead. ]

Help me how can i fix this.

Presumably, your other code is also causing that warning, it’s just not showing up anywhere. And the one that’s giving the warning is presumably still setting the cookie correctly, just that it’s also giving this warning.

You can fix the issue by passing an instance of \Cake\Http\Cookie\Cookie instead of a string.

Yes passed new Cookie instance, but cookie not created in function passed in jquery ajax url

Do you have updated code demonstrating this, or are you saying that this is what the code above is doing?

Yes, i’m already updated code.

 <script>
$.ajax({
                 url: HTTP_ROOT + 'ajaxs/profilePicture',
                 type: 'post',
                 data: {resp: resp, desp: desp},
                 beforeSend: function (xhr) {
                     xhr.setRequestHeader('X-CSRF-Token', X_CSRF_TOKEN);
                 },
                 dataType: 'json',
             }).done(function (res) {
                 if (res.status == "error") {
                     toastr.error(res.msg);
                     return false;
                }
        
                 toastr.success('Profile picture uploaded successfully.');
               
             }).fail(function () {
                 toastr.error('Timeout.');
             });
 </script>

This is my ajax code from where I want to hit the server for some data processing.

 public function profilePicture() {
         $this->loadModel('UserPic');
         $this->loadModel('UserUpdate');
         $this->request->allowMethod(['post']);
         $postData = $this->request->getData();
         if (empty($this->getRequest()->getSession()->read('Auth.User.id'))) {
             echo json_encode(['status' => 'error', 'msg' => 'Session expired. Login and try again.']);
             exit;
        }
         $user_id = $this->getRequest()->getSession()->read('Auth.User.id');
         $imagePath = 'files/user_photos/' . $user_id . '/';
         $custom_name = time() . rand(111, 999) . '.png';
         $store_image = $this->Custom->base64ToImage($custom_name, $postData['resp'], $imagePath);
         $decodeRes = json_decode($store_image, true);
         }
         
         // if ($this->request->getCookie('user_temp_cookie')) {
             $this->response = $this->response->withCookie(new Cookie('user_temp_cookie',  [
  							'user_name' ,
  							'password',
  						]));
 					
			// }
        
         // $this->response = $this->response->withCookie(new \Cookie('user_temp_cookie', ['value' => 'ert-'.rand(), 'path' => '/', 'httpOnly' => true, 'secure' => false, 'expire' => strtotime('+1 year')]));
         echo json_encode(['status' => 'success']);
         exit;
     }

This is my controller code where i want to create cookie, which is not working. Now after pass instance Deprecated issue resolved. But cookie issue still present, still cookie is not created

You are adding the cookie to the response object, and then not doing anything with that. Also, don’t ever echo output directly from a controller. Try this:

return $this->response->withStringBody(json_encode(['status' => 'success']));
1 Like

Thanks a lot. Finally it is working :slightly_smiling_face::pray: :pray: :pray:
Can you give me any reference regarding response

Just the response object in general? https://book.cakephp.org/3/en/controllers/request-response.html#response