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