user5741771
user5741771

Reputation:

How do I display a <div> when the URL contains the ID?

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?

Upvotes: 1

Views: 817

Answers (3)

Sᴀᴍ Onᴇᴌᴀ
Sᴀᴍ Onᴇᴌᴀ

Reputation: 8297

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>

Upvotes: 1

fjc
fjc

Reputation: 5825

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>

Upvotes: 1

Obsidian Age
Obsidian Age

Reputation: 42334

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! :)

Upvotes: 2

Related Questions