Mastering the Web
Contents
Website Planning Tutorial
Website Design Tutorial
HTML Tutorial
HTML Tables Tutorial
CGI Tutorial
JavaScript Tutorial
Perl Tutorials
CSS Tutorial
Installing a Web Server
Security Tutorial
HTML Cookies Tutorial
Web Tracking Tutorial
Download Free Programs
F.A.Q.

  1. Introduction
  2. Cookies with Perl and JavaScript
  3. Cookie parameters in JavaScript
  4. Issus concerning cookie acceptance
  5. What to do when cookies are disabled
  6. What to do with cookies

Download FWTLogstat1

Download FWTLogstat2

What to do when cookies are disabled

When your site depends on the use of cookies, and the user's browser is not accepting them, you must redirect the user to a warning page explaining him the problem and how to solve it. Here I will explain how to do the redirection. To see how to detect if cookies are enabled, you may look at other pages in this tutorial.

How to do a redirection using JavaScript

Each window in the browser has a Location object that contains information on the current URL. Location objects are predefined JavaScript objects that represent the complete URL associated with a given window object. Each property of the location object represents a different portion of the URL.

In general, a URL has this form:

protocol// host: port/ pathname# hash? search

For example:

http://www.great-web-info.com/samples/redsite.html#pointone?q=how

(As you can see, the colon is part of the protocol name.)

A Location object has a property for each part of the URL, and also has the following properties:

href -- represents a complete URL

hostname -- represents the concatenation host:port.

If you assign a string to the location property of an object, JavaScript creates a Location object and assigns that string to its href property. The following two statements are equivalent:

window.location.href="http://www.great-web-info.com/"
window.location="http://www.great-web-info.com/"

When you set the Location object, or any of its parts, to a value different from the current one, the new document is loaded in the window. Depending on the precise conditions, the new document may be loaded from the server or from the browser's cache.

The Location object has two methods: reload and replace. The reload method has the same effect as the Reload button of the browser. The replace method loads the new document and replaces the old document's entry in the history list of visited sites. An alternate method to load a new document but creating a new entry in the history list is to use the History.go method.

Therefore, when you have detected if cookies are enabled or not using the methods explained earlier, and you have set on or off a variable called "cookies_enabled", you can code something like this:

if (cookies_enabled) {
    // carry on with your normal processing
} else {
    location.href = 'http://www.yoursite.com/warning_page.htm";
}

If setting and retrieving the cookie on the same page does not work because of a bug in the browser--for example, a browser that has cookies disabled but still continues to accept them--, then you must use a two page method.

<html>
<head>
<script language="JavaScript">
d = new Date();
// Give the cookie a 5 minutes life
d.setTime(d.getTime()+5*60*60*­1000);
document.cookie = "test=1; expires=" + d.toGMTString() + "; path=/";
location = "page2.html";
</script>
</head>
<body></body>
</html>

<html>
<head>
<script language="JavaScript">
if (document.cookie.indexOf("test=") > -1) {
    location = "start.html";
} else {
    location = "nocookie.html";
}
</script>
</head>
<body></body>
</html>

How to do a redirection using Perl

Perl scripts, as opposed to JavaScript scripts, are processed at the server side. That means that for a Perl script to be processed, the browser must do a request of the document (the script), the Web server receives this request, it calls the script, the script is processed, and the result is returned to the browser. The difference between an ordinary HTML page and a Perl (or any other computer language) script, is that the page is returned "as it is" (if it exists), but the script must be processed by a command processor, in this case Perl. What the server returns is the result of that processing.

There are no many possibilities in normal HTML coding to make the browser request the processing of a Perl script. They are mainly limited to user-initiated actions as in the case of a link or a form, where you can set the "action" attribute of the tag to the URL of the script. For example, in a feedback page:

However, things are very much easier using server-side includes (SSI). This is a technique were the files that contain the HTML are not given the usual extensions "*.HTM" or "*.HTML", but instead they are given the extension "*.SHTML". When the Web server sees a page with this extension, it looks at the page coding before sending the page. If it finds some of the special SSI tags, it does what they demand. One of those special tags is for executing a server-side script.

In the case of Perl, the two-pages method is mandatory. As a script can be called from the address box of the browser or through a link, you could, for example, set as you home page the following script.

#!/usr/local/bin/perl
# ---script #1 sends the cookie and redirects
print "Set-Cookie: Cookie=Test\r\n";
print "Location: http://www.yoursite.com/cgi-bin/start.pl\r\n\r\n";
exit;

We are working here with dynamic pages, that is, pages that are created by processing a script. The real home page would be created by the following script that, in reality, should do something more than proclaiming the browser's status. One of the branches should write all the home page, and the other should write a page explaining the user that he must enable cookies to go on.

#!/usr/local/bin/perl
# ---script #2 reads the cookie and warns the user
if ($ENV{'HTTP_COOKIE'} =~ /Cookie=Test/) {
	print("Content-type: text/html\r\n\r\n");
	print("");
	print("");
	print("Your browser accepts cookies");
	print("");
	print("");
	print("");
	exit;
}
else {
	print("Content-type: text/html\r\n\r\n");
	print("");
	print("");
	print("Your browser doesn't accept cookies");
	print("");
	print("");
	print("");
	exit;
}

There are other reasons why a browser may not accept cookies aside from users' preferences; for example, the computer may be behind a firewall that blocks them. Although I am using here a value of "Test" for the cookie, I do not endorse the use of test cookies because even if a test cookie may succeed, there is no guarantee that other cookies will succeed also. The best approach, in my opinion, is to create and check the real cookie that you need, and use it if possible at all.

Previous | Contents | Next

| HOME | FEEDBACK | BOOKMARK |
Build your Website
© 1999-2008 Hector Castro -- All rights reserved

If your doubt is not answered in this site, please use the
contact form .
I'll answer as soon as posible.
I can help you using instant messaging. To schedule a meeting, please use the
meeting form.
You will find the late news about the free programs offered here on my blog
Free Webmaster Tools
You can get news about updates to my free programs through this
RSS feed.

www.great-web-info.com