# Issue in cookie

**URL:** <https://discourse.cakephp.org/t/issue-in-cookie/8444>\
**Category:** Need Help\
**Created:** [October 1, 2020, 1:15pm UTC](https://discourse.cakephp.org/t/issue-in-cookie/8444 "2020-10-01T13:15:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![debendra986](https://avatars.discourse-cdn.com/v4/letter/d/c5a1d2/32.png) [@debendra986](https://discourse.cakephp.org/u/debendra986)\
**Post date:** [October 1, 2020, 1:15pm UTC](https://discourse.cakephp.org/t/issue-in-cookie/8444/1 "2020-10-01T13:15:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [October 1, 2020, 3:19pm UTC](https://discourse.cakephp.org/t/issue-in-cookie/8444/2 "2020-10-01T15:19:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![debendra986](https://avatars.discourse-cdn.com/v4/letter/d/c5a1d2/32.png) [@debendra986](https://discourse.cakephp.org/u/debendra986)\
**Post date:** [October 1, 2020, 6:48pm UTC](https://discourse.cakephp.org/t/issue-in-cookie/8444/3 "2020-10-01T18:48:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [October 1, 2020, 7:16pm UTC](https://discourse.cakephp.org/t/issue-in-cookie/8444/4 "2020-10-01T19:16:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![debendra986](https://avatars.discourse-cdn.com/v4/letter/d/c5a1d2/32.png) [@debendra986](https://discourse.cakephp.org/u/debendra986)\
**Post date:** [October 2, 2020, 3:03am UTC](https://discourse.cakephp.org/t/issue-in-cookie/8444/5 "2020-10-02T03:03:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [October 2, 2020, 3:45am UTC](https://discourse.cakephp.org/t/issue-in-cookie/8444/6 "2020-10-02T03:45:42Z")

</div>

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:

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

---

<div class="post-metadata">

**Author:** ![debendra986](https://avatars.discourse-cdn.com/v4/letter/d/c5a1d2/32.png) [@debendra986](https://discourse.cakephp.org/u/debendra986)\
**Post date:** [October 2, 2020, 4:37am UTC](https://discourse.cakephp.org/t/issue-in-cookie/8444/7 "2020-10-02T04:37:00Z")

</div>

Thanks a lot. Finally it is working 🙂🙏 🙏 🙏  
Can you give me any reference regarding `response`

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [October 2, 2020, 2:03pm UTC](https://discourse.cakephp.org/t/issue-in-cookie/8444/8 "2020-10-02T14:03:31Z")

</div>

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