1

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 ?

  • Do you still see the cookie in the js side after the comment stored? – andrex Sep 22 '14 at 04:41
  • @andrex i dont get it, what you mean, the comment box i populated with the cookies value after i reload the page and even the cookie has ben posted . –  Sep 22 '14 at 04:44
  • see this http://stackoverflow.com/questions/10593013/delete-cookie-by-name – gabereal Sep 22 '14 at 04:46
  • @gabereal i am not trying to remove the PHPSESSID cookie., thanks –  Sep 22 '14 at 04:47
  • its almost impossible to tell what's going wrong without more information. does the login page redirect you to the comments page or is this all happening via ajax? – gabereal Sep 22 '14 at 04:48
  • You said, when a user type in the textarea then login, javascript save the text into a cookie so that after login it should populate back the textarea. Then when saved it sends it to the php and save it right? Now what I am asking is that after the php saves you check if cookie is still there and check if cookie in the js side (after saving from php) is still there or not. You have two side to possibly check the cookie after it's stored in DB, in PHP and JS. – andrex Sep 22 '14 at 04:48
  • @andrex is exactly right. you need to make sure that you are removing the cookie on the client side. the link i sent is for deleting any cookie not just a session cookie. but it looks like you are trying to remove the cookie in your php script and then echoing out some html after. you need to echo out something from your php script that you can check for success in your js script and then use that to delete the cookie. – gabereal Sep 22 '14 at 04:49
  • @andrex issue solved by setting path / while setting the cookie in js. –  Sep 22 '14 at 04:51
  • @AMB Can you post how you solved it? It would be helpful to others – andrex Sep 22 '14 at 04:52
  • @andrex errr, sorry but when i set the path while setting the cookie in js its now showing in the php after comment have been posted ., but still not removing the cookie., issue , not solved yet /. –  Sep 22 '14 at 04:54
  • @AMB again is the cookie removed in PHP? Is the cookie after PHP saved the comment, then go back to js check if cookie is still in JS (client side stored in browser) – andrex Sep 22 '14 at 05:06
  • @andrex cookie isnt removed, after posting a comment , cookie is still in the browser. –  Sep 22 '14 at 05:54
  • How did you know that it's still in the browser? Just by refreshing the page or by looking at the stored cookie in the browser's cookie manager interface? – andrex Sep 22 '14 at 05:57
  • @andrex i am checking with the browser's cookie manager interface., its still there and even if i reload the page the textarea is get repopulated again which is handled in php. –  Sep 22 '14 at 06:00
  • @andrex issue solved , please check answer and there are people with same issues ., tried searching in google too. thanks –  Sep 22 '14 at 06:26

1 Answers1

2

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 , "/");