简体   繁体   中英

Cookie check doesn't work

I'm making a cookie system, but It doesn't work; 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>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <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>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <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>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <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. I think that the cookie isn't set.

What can I do?

In all your if and elseif statements, you have something like:

($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_is_set = isset($_COOKIE['terminos']);

After each of your setcookie() calls, add:

$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

setcookie("terminos", "/*here you have to put a value*/", time() + 60*60*24*365*10);

http://php.net/manual/en/function.setcookie.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM