如何测试某人的cookie是否无效? (PHP)

时间:2021-01-04 01:23:38

Can someone show me a simple way I can test if someone's cookies are not enabled in php? I want to create a test script to determine if the user can properly use my site. If they cannot, I will redirect them to a couple screen shots to show them what to change.

有人能告诉我一个简单的方法我可以测试某人的cookie是否未在php中启用?我想创建一个测试脚本来确定用户是否可以正确使用我的网站。如果不能,我会将它们重定向到几个屏幕截图,向他们展示要改变的内容。

I am only using $_SESSION[] variables, and I beleive that the only thing the cookie is for, in my setup is the session id variable.

我只使用$ _SESSION []变量,并且我相信cookie的唯一用途是,在我的设置中是会话id变量。

Ideally, I would like to turn on an option in PHP for that user to pass the session id back and forth via GET/POST if their cookies are messed up.

理想情况下,我想在PHP中打开一个选项,让用户通过GET / POST来回传递会话ID,如果他们的cookie搞砸了。

Anything Javascript will not work for me, as they may not have javascript enabled.

任何Javascript都不适合我,因为他们可能没有启用JavaScript。

3 个解决方案

#1


setcookie("cookies","yes",time() +"3600");

Then redirect after that and test if the cookie is set on the next page. It will take at least two pages to know whether or not cookies are enabled.

然后重定向,然后测试cookie是否在下一页设置。至少需要两页才能知道cookie是否已启用。

#2


use SID
see http://jp2.php.net/function.session-start and http://jp2.php.net/manual/en/session.idpassing.php

使用SID请参阅http://jp2.php.net/function.session-start和http://jp2.php.net/manual/en/session.idpassing.php

SID will be empty if the cookie was accepted, and contain the session id (PHPSESSID=session_id(), actually) otherwise. This does however depend on compilation flags so be sure to check the refs.

如果cookie被接受,则SID将为空,否则将包含会话ID(实际上为PHPSESSID = session_id())。但这取决于编译标志,所以一定要检查refs。

you could use it as :

你可以用它作为:

echo '<br /><a href="page2.php?' . SID . '">page 2</a>';

#3


The standard way of doing this is by using "the proof is in the pudding" programming.

这样做的标准方法是使用“证明在布丁中”编程。

There are two ways to do this:

有两种方法可以做到这一点:

  • If testing for cookies without Javascript, issue a cookie and read it via PHP after a redirect. If it's there, fine. If not...you know cookies are not enabled.
  • 如果在没有Javascript的情况下测试cookie,请发布cookie并在重定向后通过PHP读取它。如果它在那里,很好。如果不是......你知道cookie没有启用。

  • If you can use Javascript, issue a cookie via PHP and read it using Javascript (I think the cookie will be there on the first page load as the cookie would have been set if browser gets the setcookie header).
  • 如果您可以使用Javascript,请通过PHP发布cookie并使用Javascript读取它(我认为cookie将在第一页加载时出现,因为如果浏览器获得setcookie标头,则会设置cookie)。

#1


setcookie("cookies","yes",time() +"3600");

Then redirect after that and test if the cookie is set on the next page. It will take at least two pages to know whether or not cookies are enabled.

然后重定向,然后测试cookie是否在下一页设置。至少需要两页才能知道cookie是否已启用。

#2


use SID
see http://jp2.php.net/function.session-start and http://jp2.php.net/manual/en/session.idpassing.php

使用SID请参阅http://jp2.php.net/function.session-start和http://jp2.php.net/manual/en/session.idpassing.php

SID will be empty if the cookie was accepted, and contain the session id (PHPSESSID=session_id(), actually) otherwise. This does however depend on compilation flags so be sure to check the refs.

如果cookie被接受,则SID将为空,否则将包含会话ID(实际上为PHPSESSID = session_id())。但这取决于编译标志,所以一定要检查refs。

you could use it as :

你可以用它作为:

echo '<br /><a href="page2.php?' . SID . '">page 2</a>';

#3


The standard way of doing this is by using "the proof is in the pudding" programming.

这样做的标准方法是使用“证明在布丁中”编程。

There are two ways to do this:

有两种方法可以做到这一点:

  • If testing for cookies without Javascript, issue a cookie and read it via PHP after a redirect. If it's there, fine. If not...you know cookies are not enabled.
  • 如果在没有Javascript的情况下测试cookie,请发布cookie并在重定向后通过PHP读取它。如果它在那里,很好。如果不是......你知道cookie没有启用。

  • If you can use Javascript, issue a cookie via PHP and read it using Javascript (I think the cookie will be there on the first page load as the cookie would have been set if browser gets the setcookie header).
  • 如果您可以使用Javascript,请通过PHP发布cookie并使用Javascript读取它(我认为cookie将在第一页加载时出现,因为如果浏览器获得setcookie标头,则会设置cookie)。