[英]Cookie check doesn't work
I'm making a cookie system, but It doesn't work; 我正在制作一个cookie系统,但是它不起作用。 I have this code: 我有以下代码:
<?php
$cookie = $_GET['cookie'];
if(empty($cookie))
{
$cookie = '';
}
if($cookie = 'ok')
{
setcookie("terminos", "", time() + 60*60*24*365*10);
}
elseif($cookie = 'no')
{
?>
<div id="pmyr_div">
<center style="position: relative; top: 50%; margin-top: -23px;margin:auto 0;">
<img src="cooltext1105916314.png" style="margin-top:20px;" />
<br><br>
<big><b>USTED TIENE EL ACCESO DENEGADO A ESTE SITIO POR DENEGAR NUESTROS TÉRMINOS LEGALES.</b></big>
<br><br>
<a href="index.php?state=test"><b>De acuerdo, seré un chico bueno.</b></a>
</center>
</div>
<?
}
elseif($cookie = 'test')
{
setcookie("user", "", time()-60*60*24*365*10);
header('location: index.php?cookie=raw');
}
elseif($cookie = 'raw')
{
?>
<div id="pmyr_div">
<center>
<br><b>MUY IMPORTANTE!<br>ES NECESARIO QUE SE LEA NUESTROS TÉRMINOS LEGALES.</b><br><br>Sólo puede entrar en este sitio web <b>si es mayor de 18 años de edad</b>, o al menos la mayoría de edad en la jurisdicción donde usted reside o del que se accede a esta página web.<br><br>Si usted no cumple con estos requisitos, entonces usted NO ESTÁ AUTORIZADO, Y NO DEBE INGRESAR a este sitio web.
<br><br><br><br>
<a href="index.php?cookie=ok" style="color: #8e1a1a;font-weight:bold;text-decoration:none;cursor:pointer;">SOY MAYOR DE 18 AÑOS</a>
<a href="index.php?cookie=no" style="color: #8e1a1a;font-weight:bold;text-decoration:none;">NO ENTRAR</a>
</center>
</div>
<?
}
elseif(empty($cookie))
{
?>
<div id="pmyr_div">
<center>
<br><b>MUY IMPORTANTE!<br>ES NECESARIO QUE SE LEA NUESTROS TÉRMINOS LEGALES.</b><br><br>Sólo puede entrar en este sitio web <b>si es mayor de 18 años de edad</b>, o al menos la mayoría de edad en la jurisdicción donde usted reside o del que se accede a esta página web.<br><br>Si usted no cumple con estos requisitos, entonces usted NO ESTÁ AUTORIZADO, Y NO DEBE INGRESAR a este sitio web.
<br><br><br><br>
<a href="index.php?cookie=ok" style="color: #8e1a1a;font-weight:bold;text-decoration:none;cursor:pointer;">SOY MAYOR DE 18 AÑOS</a>
<a href="index.php?cookie=no" style="color: #8e1a1a;font-weight:bold;text-decoration:none;">NO ENTRAR</a>
</center>
</div>
<?
}
if(isset($_COOKIE["terminos"]))
{
//page content here
}
elseif(!isset($_COOKIE["terminos"]))
{
?>
<div id="pmyr_div">
<center>
<br><b>MUY IMPORTANTE!<br>ES NECESARIO QUE SE LEA NUESTROS TÉRMINOS LEGALES.</b><br><br>Sólo puede entrar en este sitio web <b>si es mayor de 18 años de edad</b>, o al menos la mayoría de edad en la jurisdicción donde usted reside o del que se accede a esta página web.<br><br>Si usted no cumple con estos requisitos, entonces usted NO ESTÁ AUTORIZADO, Y NO DEBE INGRESAR a este sitio web.
<br><br><br><br>
<a href="index.php?cookie=ok" style="color: #8e1a1a;font-weight:bold;text-decoration:none;cursor:pointer;">SOY MAYOR DE 18 AÑOS</a>
<a href="index.php?cookie=no" style="color: #8e1a1a;font-weight:bold;text-decoration:none;">NO ENTRAR</a>
</center>
</div>
<?
}
?>
When I press the link SOY MAYOR DE 18 AÑOS
the message doesn't disappear. 当我按链接SOY MAYOR DE 18 AÑOS
该消息不会消失。 I think that the cookie isn't set. 我认为未设置Cookie。
What can I do? 我能做什么?
In all your if
and elseif
statements, you have something like: 在所有if
和elseif
语句中,都有类似以下内容:
($cookie = "ok")
It should be: 它应该是:
($cookie == "ok")
=
is for assignment, ==
is for comparing. =
用于分配, ==
用于比较。
For using the cookie, put the following at the top of the script: 要使用Cookie,请将以下内容放在脚本顶部:
$cookie_is_set = isset($_COOKIE['terminos']);
After each of your setcookie()
calls, add: 在每个setcookie()
调用之后,添加:
$cookie_is_set = true;
Then at the bottom of the script, instead of 然后在脚本底部,而不是
if (isset($_COOKIE['terminos']))
use: 采用:
if ($cookie_is_set)
You are setting an empty cookie 您正在设置一个空的cookie
setcookie("terminos", "/*here you have to put a value*/", time() + 60*60*24*365*10);
http://php.net/manual/en/function.setcookie.php http://php.net/manual/zh/function.setcookie.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.