user1642018
user1642018

Reputation:

Why is php not removing cookie set by javascript?

There is post comment box on my site.

Posting comment is handled by javascript, js posts data to php script and php does the db related stuff and shows confirmation only if user is logged in if user isnt logged in then php gives not_loggedin response after receiving it js shows the bootstrap pop over box with link to login page.

So if user is not logged in then the javascript stores the entered comment in cookie so that after logging in user dont have retype the comment. Like this

    document.cookie = id + "=" + input_text + "; ";

and after logging in the comment textarea is populated by reading the cookie which has the stored comment text..

everything up to this is working perfect but after inserting the comment in php i am trying to remove the cookie like this .,

setcookie($id, "", time()-3600);
print_r($_COOKIE);
exit('<p class="bg-info">Thank you! Your comment has been posted.</p>'); 

but its still not removing cookie , when i reload the page , the comment textarea box i populated with the previously enetered comment which is read again from cookie.,

how do i solve this ?

i even tried displaying the cookie in php which is stored by js , lke this

//insert comment in db 
//setcookie($id, "", time()-3600);
print_r($_COOKIE);
exit('<p class="bg-info">Thank you! Your comment has been posted.</p>');  

but it doesnt shows the cookie which is set by the js, it shows PHPSESSID cookie after posting comment.,

Array
(
    [PHPSESSID] => c5rc6c8ggg24edg1v2o8hebb20
)

i am not trying to remove the PHPSESSID cookie., i am showing this on page using js . as post_comment.php is another file in another directory on the same server .

what i am doing wrong ?

In simple words , js is setting cookie and php should remove cookie.

----------

update 1 :

i tried setting path while setting cookie in js like this

document.cookie = id + "=" + input_text + "; path=/";

and after posting cookie., now i get this.,

Array
(
    [PHPSESSID] => c5rc6c8ggg24edg1v2o8hebb20
    [4778] => this is comment
)

my php code is like this .,

//insert comment is db                              
setcookie($id, "", time()-3600);
printr($_COOKIE);
exit('<p class="bg-info">Thank you! Your comment has been posted.</p>'); 

but cookie is still there.


update 2 : this is very strange.,

if i try to set the cookie of the same name in php , one more cookie gets created. my php code.

setcookie($id, "sdf", time()+36000);
printr($_COOKIE);
exit('<p class="bg-info">Thank you! Your comment has been posted.</p>'); 

now when i check the broswers cookie manager , i see 2 cookies with the same name .,

but both having different content , the one which was set usng javascript have the comment enetered by user and another cookie which we set using php above , is having content "sdf".

i dont know how is this possible to be having2 cookies with the exact same names. ,

any clues ?

Upvotes: 1

Views: 1434

Answers (1)

user1642018
user1642018

Reputation:

You need to make sure the all the parameters (except name and time depending on the cookie.) are same while Setting Cookie in Javascript and while Removing Cookie in PHP

Parameters i.e. name,path (value and expire time can be different.)

for eg.

While setting cookie in javascript if you use it like this

document.cookie = id + "=" + input_text + " ; path=/";

you set the path to "/"

then while removing cookie in php you should specifically set like this.

//remove cookie.
setcookie($id, "", time()-36000 , "/");

Upvotes: 2

Related Questions