Javascript Browser Cookies - Restrict Access by Testing a Cookie
Restricting Access Based on a Cookie - Javascript
The javascript cookie methods on these pages demonstrate how to:- set/put a cookie - Try it
- get/read a cookie - Try it
- delete/remove a cookie - Try it
- restrict webpage access based on a cookie - Try it
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:
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.
<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:
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>
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:
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;
}
}
}

