当URL包含ID时,如何显示<div>?

时间:2017-01-23 22:05:24

标签: javascript php html

当网址包含特定内容的ID时,我需要在文档上显示某些内容。目前,用户将通过一系列步骤进行安全检查。最后,将在其系统上放置一个cookie,以避免在30天内进行安全检查。现在,假设cookie无法进入系统或无法在用户系统上找到cookie,必须有通知,以便用户不会对为什么在30天之前进行另一次安全检查感到困惑。到目前为止,这就是我得到的:

将cookie放在计算机上(setcookie.php):

<?php
$cookie_name = "securitycheck";
$cookie_value = "29610741351979104njnj23j52nx72d72n892ccr3179hd3";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>

验证cookie(checkcookievalidation.php):

<head>
<?php
$cookie_name = "securitycheck";
?>
</head>
<body>
<?php
if(!isset($_COOKIE[$cookie_name])) {
     echo "Cookie named '" . $cookie_name . "' is not set! Internal Error!";
     header( 'Location: checkfinished.php?nocookieset' ) ;
} else {
     echo "Cookie '" . $cookie_name . "' is set!<br>";
     header("Location: checkfinished.php");
}
?>
</body>

完成页面(checkfinished.php)[这是我坚持的地方]:

<div id="nocookieset" style="visibility: visible;" align="center"><h3>Although the check did complete with errors. We were unable to set the cookie due to an internal server error. Please contact the web team about this if the issue continues.</h3></div>

- 现在我需要它来显示带有nocookieset id的Div,如果URL包含中的id(例如http://example.com/example.php?nocookieset

如果网址中不包含?nocookieset,则不会显示Div。

我怎样才能实现这一目标?

3 个答案:

答案 0 :(得分:2)

执行此操作的最佳方法是在PHP条件中注入HTML内容:

<?php
if(isset($_GET['nocookieset'])) {
?>
<div id="nocookieset">
</div>
<?php
/* Don't forget to close the condition logic! */
}
?>

这样,如果设置了GET变量,nocookieset DIV将只输出到页面:)

希望这有帮助! :)

答案 1 :(得分:1)

在PHP中(即 checkfinished.php ),使用superglobal $_GET检查是否设置了GET变量...您可以使用各种技术检查是否索引已设置(例如isset()array_key_exists)...

if (array_key_exists('nocookieset',$_GET)) {
    echo '<div id="nocookieset" style="visibility: visible;" align="center"><h3>Although the check did complete with errors. We were unable to set the cookie due to an internal server error. Please contact the web team about this if the issue continues.</h3></div>';
}

您也可以在客户端检查,例如使用Navigator.cookieEnabled - 有关详细信息,请参阅this guide。遵循这种技术,将消除对 checkcookievalidation.php 的需求。

注意:在下面的沙箱示例中,已禁用document.cookie的使用,但您可以在this plunker中看到它的运行情况。

document.addEventListener('DOMContentLoaded', function() {
  var element = document.getElementById('message');
  cookiesEnabled = false; //set tentatively
  if (navigator.cookieEnabled) {
    //document.cookie = "testcookie";
    //cookiesEnabled = (document.cookie.indexOf("testcookie") != -1) ? true : false;
    
    //we can't set cookies in this sandbox so set it to true for now
    cookiesEnabled = true;
  }
  if (cookiesEnabled) {
    element.innerHTML = 'Yes';
  } else {
    element.innerHTML = 'No';
  }
});
<span style="font-style: italic;">Are Cookies Enabled? </span><span id="message"></span>

答案 2 :(得分:1)

使用JS:

<script>
    if(location.search === "?nocookieset") document.getElementById("nocookieset").style.visibility="visible";
</script>

支持多个ID的版本:

<script>
    var supportedIds = ["nocookieset", "toomanycookiesset"];
    if(supportedIds.indexOf(location.search.substr(1))>-1) {
        document.getElementById(location.search.substr(1)).style.visibility="visible";
    } 
</script>