为什么php不通过javascript删除cookie设置? [英] Why is php not removing cookie set by javascript?

查看:151
本文介绍了为什么php不通过javascript删除cookie设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站上有帖子评论框。



发布评论由javascript处理,js发布数据到php脚本,php做数据库相关的东西,并显示确认



因此,如果用户登录时,如果用户登录,那么php会给予not_loggedin响应,js会显示带有登录页面的引导弹出框。没有登录,那么javascript会将输入的注释存储在cookie中,以便在登录用户后不要重新键入注释。像这样

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

并且在登录注释textarea之后,通过读取存储的注释文本的cookie来填充。



所有这一切都工作完美,但在php中插入注释后,我试图删除这样的cookie,

  setcookie($ id,,time() -  3600); 
print_r($ _ COOKIE);
exit('< p class =bg-info>谢谢!您的评论已发布。< / p>')

但它仍然没有删除cookie,当我重新加载页面,评论textarea框我填充



我如何解决这个问题?



我甚至尝试过显示由js存储的php中的cookie,lke

  //在db 
中插入注释// setcookie ($ id,,time() - 3600);
print_r($ _ COOKIE);
exit('< p class =bg-info>谢谢!您的评论已发布。< / p>')

但它不显示由js设置的cookie,它在发布评论后显示PHPSESSID cookie。 ,

 数组

[PHPSESSID] => c5rc6c8ggg24edg1v2o8hebb20

我没有尝试删除PHPSESSID cookie。,
i在页面上使用js显示此信息。 as
post_comment.php是同一服务器上另一个目录中的另一个文件。



我做错了什么?



简单来说,
js是设置cookie,而php应该删除cookie。



--------

p> i尝试设置路径,同时在js中设置cookie

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

和发布cookie后,现在我得到了。

 数组

[PHPSESSID] => c5rc6c8ggg24edg1v2o8hebb20
[4778] =>这是评论

我的PHP代码就是这样。

  // insert comment is db 
setcookie($ id,,time() - 3600);
printr($ _ COOKIE);
exit('< p class =bg-info>谢谢!您的评论已发布。< / p>')

但是cookie仍然存在。



<

更新2:
这很奇怪,



在php中设置相同名称的cookie,则会创建另外一个cookie。
我的PHP代码。

  setcookie($ id,sdf,time()+ 36000); 
printr($ _ COOKIE);
exit('< p class =bg-info>谢谢!您的评论已发布。< / p>')

现在当我检查broswers cookie管理器,我看到2个相同名称的cookie。 p>

但是两者都有不同的内容,设置为javascript的那个具有由用户enetered的注释和另一个我们使用php设置的cookie,具有内容sdf。



我不知道这是怎么可能有两个完全相同的名称的cookie。 ,



任何线索?

解决方案

您需要确保所有
参数(名称和时间除外。)在JavaScript中设置Cookie并在PHP中删除Cookie



参数即名称,路径(值和过期时间可以不同) / p>

例如



在javascript中设置cookie时,如果使用

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

您将路径设置为/



然后同时删除cookie中的PHP,你应该专门设置像这样。

  //删除cookie。 
setcookie($ id,,time() - 36000,/);


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 ?

解决方案

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

这篇关于为什么php不通过javascript删除cookie设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆