myterm: Rigorous or unusually severe.
Derived from Draco, a politician in Athens whose legal code provided for exceedingly harsh punishments. In the brenz.net world, I refer to those developers who seek to validate their code against W3C standards, specifically XHTML 1.0 Strict and CSS level 1.
home
L337 Speak Converter
Popup Windows
Javascript Cookies
Email via ASP CDONTS
VoiceXML Demo

Restrict Access Based on a Cookie - Javascript

The javascript cookie methods on these pages demonstrate how to:
  • set/put a cookie
  • get/read a cookie
  • delete/remove a cookie
  • restrict webpage access based on a cookie

Test the Cookie

This sample page demonstrates how test a client-side cookie using Javascript. We are checking the value stored in the cookie that we set on the Setting a Javascript Cookie page.
Examining cookie...
Potential Security Breach!
Using only HTML and javascript does not provide any real security for your website, since all of the client-side source code (including your PassValue) can be easily retrieved from the web page. Using ASP to set and read cookies provides a higher degree of security, since ASP can employ server-side code which will not be visible to website visitors. This example is intended only for illustrative purposes to help visitors to the brenz.net website understand and work with client-side cookies.
The following sample code demonstrates how to compare the value stored in the cookie to a predetermined value of your choosing. If the values match, the user is directed to a new web page: YourPassURL.html. If the values don't match, YourFailURL.html is loaded instead. To implement this cookie test, put the following javascript before the ending <head> tag in the current document:
<script type="text/javascript">
cookieGet(); // gets the cookie and assigns it to YouWrote
if (YouWrote == "PassValue") {
//cookie matches your PassValue, go to pass page
document.location.href = "YourPassURL.html";
} else {
//cookie doesn't match, go to fail page
document.location.href = "YourFailURL.html";
}
</script>
The cookieGet() javascript function needs to be defined, either before the ending <head> tag in the current document or in an external javascript file, as follows:
var cookieText = "Put your desired cookie value here";
var cookiePrefix = "";
var myPage = location.href;
var wwwFlag = myPage.indexOf('www');
if (wwwFlag > 0) {
cookiePrefix = "www";
}
var cookieName = cookiePrefix + "cbCookie";
function cookieGet() {
if (document.cookie) {
index = document.cookie.indexOf(cookie_name);
if (index != -1) {
namestart = (document.cookie.indexOf("=", index) + 1);
nameend = document.cookie.indexOf("cbEndCookie;", index);
if (nameend == -1) {
nameend = 0;
}
YouWrote = document.cookie.substring(namestart, nameend);
return YouWrote;
}
}
}
Related Pages: [the end]


The method(s) on this page have been tested on Windows XP, using Internet Explorer 6.0, Firefox 1.5, and Netscape 7.2.
The L337 Speak Converter averages 20 to 30 thousand page views per month.
Popup Windows
This page covers the basics of popup windows - opening, closing, and changing URLs.
Javascript Cookies
Learn the basics of manipulating cookies with javascript - setting, getting, deleting.
VoiceXML Demo
A simple, toll-free demonstration of telephone and web application interaction.
©2007 chris brenz, brenz.net