1

I need to display certain content on the document when the URL contains that particular content's ID. Currently, the USER will go through a series of steps for a security check. At the end, a cookie will be placed on their system to avoid doing the security check in 30 days. Now, let's say that the cookie fails to go on the system or it fails to locate the cookie on the user's system, there has to be a notice so the user doesn't get confused on why they are getting another security check before 30 days. So far, this is what I got:

Places the cookie on the computer (setcookie.php):

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

Validates 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>

Finished page (checkfinished.php) [This is where I'm stuck on]:

<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>

- Now I need it to show the Div with the nocookieset id if the URL contains the id in there (such as http://example.com/example.php?nocookieset)

If it doesn't contain ?nocookieset in the URL, then it won't show the Div.

How can I make this happen?

3 Answers3

2

The best way to do this is to inject the HTML content inside a PHP condition:

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

That way the nocookieset DIV will only get outputted to the page if the GET variable is set :)

Hope this helps! :)

Obsidian Age
  • 41,205
  • 10
  • 48
  • 71
1

In PHP (i.e. checkfinished.php), check if the GET variable is set, using the superglobal $_GET...You could use various techniques to check if the index is set (e.g. 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>';
}

You could also check in the client-side, e.g. with Navigator.cookieEnabled - see this guide for more information. Following this technique, the need for checkcookievalidation.php would be eliminated.

Note: in the sandbox example below, usage of document.cookie is disabled but you can see it in action in 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>
Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
1

Using JS:

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

Version that supports more than one ID:

<script>
    var supportedIds = ["nocookieset", "toomanycookiesset"];
    if(supportedIds.indexOf(location.search.substr(1))>-1) {
        document.getElementById(location.search.substr(1)).style.visibility="visible";
    } 
</script>
fjc
  • 5,590
  • 17
  • 36
  • This one worked great! Although a little concerned especially since most people don't like using JS. Could this work with –  Jan 23 '17 at 22:18
  • 1
    With `noscript` it will not work. Is your target group disabling JS a lot? Check out http://stackoverflow.com/questions/9478737/browser-statistics-on-javascript-disabled for a basic discussion on JS vs no JS. I wouldn't worry about it too much. – fjc Jan 23 '17 at 22:27
  • One more thing! I need to create one that can support 2 IDs (ie. https://example.com/index.html?ID1=ID1&ID2=ID2, whereas ID1 will show one thing on the page and ID2 will show the other thing on the page at the same time. Here's an example: Let's say I want to display "Hello there." as ID1 and "How are you?" as ID2, so if I only use ?ID1=ID1, it will only show "Hello there.", and if I only use ?ID2=ID2, it will show "How are you?" But if I use ?ID1=ID1&ID2=ID2, it will show "Hello there. How are you?" How can I do this? I need it for a web filter tool I'm making. –  Mar 21 '17 at 19:30