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.


[ 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.

                 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") {
                     return false;
                 toastr.success('Profile picture uploaded successfully.');
             }).fail(function () {

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

 public function profilePicture() {
         $postData = $this->request->getData();
         if (empty($this->getRequest()->getSession()->read(''))) {
             echo json_encode(['status' => 'error', 'msg' => 'Session expired. Login and try again.']);
         $user_id = $this->getRequest()->getSession()->read('');
         $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' ,
			// }
         // $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']);

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?