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.
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.
Men's Miami Dolphins Nike Aqua Legend Staff Practice Performance T-Shirt,Denver Broncos Team Crest Pin,Women's Carolina Panthers Black Direct Snap V-Neck Long Sleeve T-Shirt Wholesale New England Patriots Jerseys.San Francisco 49ers Women's NFL Mock Wrap Scrub Top - Scarlet,Preschool Philadelphia Eagles Starter Green Satin Varsity Jacket Patriots Jerseys Cheap.Preschool Baltimore Ravens Purple Reflection T-Shirt,Youth Chicago Bears Navy/Orange Strike Full-Zip Hoodie.Jimmy Graham New Orleans Saints Fair Catch Short Sleeve Tee - Black,Chicago Bears Double-Sided 28ĄŻĄŻ x 40ĄŻĄŻ Banner.New Orleans Saints Thin Rim License Plate Frame with Decals,vip wholesale jerseys,cheap nfl accessories kenwood Patriots Jerseys Wholesale.Women's Seattle Seahawks Marshawn Lynch Nike College Navy Game Jersey,Nike Green Bay Packers Women's Gridiron Premium Slim Fit T-Shirt - Green,Women's Los Angeles Rams New Era Navy Arctic Blast Cable Cadet Beanie.Mens Kansas City Chiefs Jamaal Charles Majestic Red Hashmark II Synthetic Big & Tall T-Shirt,Dallas Cowboys Pro Line Charcoal Lightweight Full Zip Hooded Jacket,Women's Denver Broncos Sterling Silver Earrings with Austrian Crystals Patriots Jerseys On Sale.Women's Kansas City Chiefs Nike Gray Rally Open Hem Rewind Sweatpants,Men's San Francisco 49ers Concepts Sport Scarlet Medalist Pant & T-Shirt Pajama Set,WomenĄŻs Chicago Bears Alshon Jeffery Nike Navy Blue Game Jersey
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:
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;
}
}
}