【问题标题】:Hide code if there is a cookie如果有 cookie 则隐藏代码
【发布时间】:2026-01-16 10:55:02
【问题描述】:

我尝试了各种不同的代码,php、javascript,但我无法让它按照我想要的方式工作。让我解释一下:

首先,我的代码应该检查 cookie。如果有 cookie,则不应显示此代码:

<ul>
  <li><a id="demo03" href="#modal-03">DEMO03 7</a>
  </li>
</ul>




<!--DEMO03-->
<div id="modal-03" style="background-image:url(images/access.jpg); background-size:cover;">
  <!--"THIS IS IMPORTANT! to close the modal, the class name has to match the name given on the ID-->
  <div id="btn-close-modal" class="close-modal-03">
    <!--CLOSE MODAL-->
  </div>

  <div class="modal-content">
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <img src="images/logo.png" />
    <br />
    <br />
    <font color="#bfbaa8">Man skal være over 18 år, for at få adgang til denne side.<br />
                <font size="+4">Indtast dit fødselsår</font>
    <br />
    <input maxlength="4" id="age_validation_input" size="33" align="middle" style="background-color: rgba(191, 186, 168, 0.8); border:none; height:90px; width:380px; color:#3d3b33; padding:15px; font-size:70px; font-weight:bold;" value="F.eks. 1963" onblur="if (this.value == '') {this.value = 'F.eks. 1963';}"
    onfocus="if (this.value == 'F.eks. 1963') {this.value = '';}">
    </font>
    <br />
    <input type="button" id="age_validation_btn" align="middle" style="background-color: rgba(99, 95, 82, 0.8); border:none; height:65px; width:410px; color:#c4bda4; font-size:40px; font-weight:bold;" value="OK" />
    <br />
    <br />
    <font size="-1" color="#69665b">Ved at klikke "OK" giver du tilladelse til, at webstedet benytter cookies.</font>
  </div>
</div>

然后我有一个javascript sn-p。如果一切正常,应该设置一个 cookie(进入站点时将被搜索的 cookie,如上所述)。这是代码,/////////// 是应该创建 cookie 的位置。

$('#age_validation_btn').on('click', function() {

  var age = parseInt($('#age_validation_input').val(), 10);
  var currentYear = (new Date).getFullYear();
  var calculatedYear = currentYear - 123;
  var oldage = age - calculatedYear;

  if (age > currentYear) {
    swal("Hov!", "Vi er ikke nået til det år endnu.", "error");
    return false;
  }

  if (oldage < 5) {
    swal("Hov!", "Du skal indtaste et gyldigt fødselsår.", "error");
    return false;
  }

  if (isNaN(age) || age == 'F.eks. 1963') {
    swal("Hov!", "Du skal indtaste dit fødselsår, for at få adgang til denne side.", "error");
    return false; //stop the validation here
  }

  if (new Date().getFullYear() - age >= 18) {
    //////////////////////////////
    //////////////////////////////
    //////////////////////////////
    $('#btn-close-modal').trigger('click');
  } else {
    location.href = 'noaccess.html';
  }
});

谁能帮帮我?我已经尝试了很长一段时间了,但没有运气:-(

举个例子,我试过这个...... 这段代码是应该隐藏代码的代码:

<?php
if(!isset($_COOKIE[$cookie_name])) {
    // nothing should happen, pass user
} else {
    echo '
  <ul>
    <li><a id="demo03" href="#modal-03">DEMO03 7</a>
    </li>
  </ul>




  <!--DEMO03-->
  <div id="modal-03" style="background-image:url(images/access.jpg); background-size:cover;">
    <!--"THIS IS IMPORTANT! to close the modal, the class name has to match the name given on the ID-->
    <div id="btn-close-modal" class="close-modal-03">
      <!--CLOSE MODAL-->
    </div>

    <div class="modal-content">
      <br />
      <br />
      <br />
      <br />
      <br />
      <br />
      <img src="images/logo.png" />
      <br />
      <br />
      <font color="#bfbaa8">Man skal være over 18 år, for at få adgang til denne side.<br />
                <font size="+4">Indtast dit fødselsår</font>
      <br />
      <input maxlength="4" id="age_validation_input" size="33" align="middle" style="background-color: rgba(191, 186, 168, 0.8); border:none; height:90px; width:380px; color:#3d3b33; padding:15px; font-size:70px; font-weight:bold;" value="F.eks. 1963">
      </font>
      <br />
      <input type="button" id="age_validation_btn" align="middle" style="background-color: rgba(99, 95, 82, 0.8); border:none; height:65px; width:410px; color:#c4bda4; font-size:40px; font-weight:bold;" value="OK" />
      <br />
      <br />
      <font size="-1" color="#69665b">Ved at klikke "OK" giver du tilladelse til, at webstedet benytter cookies.</font>
    </div>
  </div>
    ';
}
?>

这是应该创建 cookie 的部分:

$('#age_validation_btn').on('click', function() {

    var age = parseInt($('#age_validation_input').val(), 10);
    var currentYear = (new Date).getFullYear();
    var calculatedYear = currentYear - 123;
    var oldage = age - calculatedYear;

    if(age > currentYear) {
        swal("Hov!", "Vi er ikke nået til det år endnu.", "error");
        return false;
    }

    if(oldage < 5) {
        swal("Hov!", "Du skal indtaste et gyldigt fødselsår.", "error");
        return false;
    }

    if(isNaN(age) || age == 'F.eks. 1963') {
        swal("Hov!", "Du skal indtaste dit fødselsår, for at få adgang til denne side.", "error");
        return false;//stop the validation here
    }

    if(new Date().getFullYear() - age >= 18) {  
        <? $cookie_name = "user"; ?>
        $('#btn-close-modal').trigger('click'); 
    } else {
        location.href = 'noaccess.html';
    }
});

【问题讨论】:

  • 添加至少一种您尝试创建 cookie 的方式,以便我们进行故障排除...同时检查您的浏览器的开发工具并检查是否实际创建了 cookie...跨度>
  • 好的,我们开始:完成
  • 我没有看到任何会在您的代码中的任何位置设置 cookie...所以您能提供至少一个您尝试过的示例吗?
  • 你还没有展示你如何尝试实际设置 cookie
  • 倒数第五行是php,只是一个变量的声明

标签: javascript php jquery cookies


【解决方案1】:
  1. PHP - 如果未设置 cookie,则显示代码,而不是在设置时隐藏代码:
<?php
if(!isset($_COOKIE["user"])) { ?>
  <ul>
    <li><a id="demo03" href="#modal-03">DEMO03 7</a>
    </li>
  </ul>
<?php } ?>
  1. JS - 使用https://github.com/js-cookie/js-cookie
if (new Date().getFullYear() - age >= 18) { 
     Cookies.set("user","ok");
     $('#btn-close-modal').trigger('click');
 } else {
     Cookies.remove("user");
    location.href = 'noaccess.html';
 }

这假设您想在他们下次访问时隐藏 UL

【讨论】:

  • 不幸的是,模态现在甚至没有出现。模态应该显示是正确的,并且当输入时它不应该再次显示(一天或类似的时间),但它根本不显示模态。
  • 我放了一个!在资产前面,使模态显示。现在按钮不起作用?是因为它在调用 btn-close-modal 之前设置了 cookie?
  • 在浏览器中按 F12 - 您是否包含 cookie JS 文件?
  • 您需要在未设置cookie时显示代码 - 然后! (不)走在 isset 前面
  • 什么js文件?是的,我添加了!在它前面。
【解决方案2】:

如果你想隐藏 html,当没有设置 cookie 时,比从这里修改你的 php 代码:

if(!isset($_COOKIE[$cookie_name])) {
    // nothing should happen, pass user
} else {
//your html code here
}

到这里:

if(!isset($_COOKIE[$cookie_name])) {
//your html code here
}

(注意isset函数前的感叹号)

【讨论】:

    【解决方案3】:

    您可以使用这些函数来创建和获取cookie(此示例来自:http://www.w3schools.com/js/js_cookies.asp

    var getCookie = function(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1);
        if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
    }
        return "";
    }
    
    var setCookie = function(cname, cvalue, exdays) {
        var d = new Date();
        d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
        var expires = "expires=" + d.toUTCString();
        document.cookie = cname + "=" + cvalue + "; " + expires;
    }
    

    要设置你的cookie,请使用:

    setCookie("MyCookie", "MyValue", 365)
    

    在您的文档就绪功能中,您可以使用它进行测试

    getCookie("MyCookie")
    

    【讨论】:

    • 我已经尝试了所有类型的不同代码和 sn-ps,但我似乎无法以一种使代码执行我想要它的方式来实现它 - 正如我上面提到的:/跨度>
    • 如果 cookie 存在,要隐藏你的部分,用你选择的 id 包裹代码隐藏,然后使用 $(document).ready(function() { if( !!getCookie ("MyCookie")) { $('#mydivtohide').hide() } });