mca0106
asked on
Cookie - remember my choise and redirect
Where can I find a cookie script which can remember my choise if a check box is marked. I want the startpage.htm to contain a link to a english.htm and a french.htm page:
link to english.htm :: french.htm
[check box] <- remember my choise of language if this checkbox is marked. At next visit, never show the startpage.htm but redirect me directly to the english or french page dependant on choise the first time, if the check box was marked at the first visit that is...
Please help me out on this one. Is this possible? I'm totally in the dark...
rgds // Magnus Carlsson
link to english.htm :: french.htm
[check box] <- remember my choise of language if this checkbox is marked. At next visit, never show the startpage.htm but redirect me directly to the english or french page dependant on choise the first time, if the check box was marked at the first visit that is...
Please help me out on this one. Is this possible? I'm totally in the dark...
rgds // Magnus Carlsson
oh and at the very top of startpage.php, you would need:
<?
if($_COOKIE["lang"] == "english") {
header("Location: english.htm");
} elseif($_COOKIE["lang"] == "french") {
header("Location: french.htm");
}
?>
<?
if($_COOKIE["lang"] == "english") {
header("Location: english.htm");
} elseif($_COOKIE["lang"] == "french") {
header("Location: french.htm");
}
?>
ASKER
Sorry forgot... Don't have the possibility of using either perl, php or asp. Is it still possible acheiving this effect with just plain cookie-scripting and maybe some javascript to send on to a regular server?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi,
the following is a javascript/html cookie. On the surfer's first visit, s/he will be presented with the form. Upon either clicking on the links or the radio buttons, the script will automatically write the cookie and redirect the user. On subsequent visits, the cookie will be read and the user redirected.
All you need to do is:
1) name the file 'index.html'
2) change the cookieName variable to whatever you want;
3) decide if you want to keep the links and/or radio buttons, or use a different input type (buttons, perhaps) (and no, I do not have the flag gifs)
4) rewrite the urls so they reflect your web site layout
<html>
<head>
<script language="javascript" type="text/javascript">
var cookieName = "grassBlade";
var index = (document.cookie == document.cookie) ?
document.cookie.indexOf(co okieName) : -1;
if (index != -1)
window.location = readCookie(index);
function putCookie(preference)
{
url = (parseInt(preference)) ? 'french.html' : 'english.html';
writeCookie(url)
}
function writeCookie(url)
{
document.cookie = cookieName + " = " + url + "; expires=Monday, 04-Apr-2010 05:00:00 GMT";
window.location = url;
}
function readCookie(ndx)
{
namestart = (document.cookie.indexOf(" =", ndx) + 1);
nameend = document.cookie.indexOf("; ", ndx);
if (nameend == -1)
nameend = document.cookie.length;
return(document.cookie.sub string(nam estart, nameend));
}
//-->
</script>
</head>
<body>
<form name="theForm">
<table border="1">
<tr>
<td rowspan="2">Preference:</t d>
<td><a href="#" onclick="putCookie('0');re turn false"><img src="eFlag.gif"></a></td>
<td><a href="#" onclick="putCookie('1');re turn false"><img src="fFlag.gif"></a></td>
</tr><tr>
<td>English: <input type="radio" name="language" value="0" onclick="putCookie(this.va lue)"></td >
<td>Francais: <input type="radio" name="language" value="1" onclick="putCookie(this.va lue)"></td >
</tr></table>
</form>
</body>
</html>
Vinny
the following is a javascript/html cookie. On the surfer's first visit, s/he will be presented with the form. Upon either clicking on the links or the radio buttons, the script will automatically write the cookie and redirect the user. On subsequent visits, the cookie will be read and the user redirected.
All you need to do is:
1) name the file 'index.html'
2) change the cookieName variable to whatever you want;
3) decide if you want to keep the links and/or radio buttons, or use a different input type (buttons, perhaps) (and no, I do not have the flag gifs)
4) rewrite the urls so they reflect your web site layout
<html>
<head>
<script language="javascript" type="text/javascript">
var cookieName = "grassBlade";
var index = (document.cookie == document.cookie) ?
document.cookie.indexOf(co
if (index != -1)
window.location = readCookie(index);
function putCookie(preference)
{
url = (parseInt(preference)) ? 'french.html' : 'english.html';
writeCookie(url)
}
function writeCookie(url)
{
document.cookie = cookieName + " = " + url + "; expires=Monday, 04-Apr-2010 05:00:00 GMT";
window.location = url;
}
function readCookie(ndx)
{
namestart = (document.cookie.indexOf("
nameend = document.cookie.indexOf(";
if (nameend == -1)
nameend = document.cookie.length;
return(document.cookie.sub
}
//-->
</script>
</head>
<body>
<form name="theForm">
<table border="1">
<tr>
<td rowspan="2">Preference:</t
<td><a href="#" onclick="putCookie('0');re
<td><a href="#" onclick="putCookie('1');re
</tr><tr>
<td>English: <input type="radio" name="language" value="0" onclick="putCookie(this.va
<td>Francais: <input type="radio" name="language" value="1" onclick="putCookie(this.va
</tr></table>
</form>
</body>
</html>
Vinny
on your start page:
<script language="JavaScript" type="text/javascript">
<!--
function submitform ( selectedtype )
{
document.cookieform.langua
document.cookieform.submit
}>
-->
</script>
you need your form as such:
<form name="cookieform" method="POST" action="process.php">
<input type="checkbox" name="remember" value="1" checked><br />
<input type="hidden" name="language" value="1" checked>
</form>
the links are as below:
<a href="javascript:submitfor
<a href="javascript:submitfor
and on process.php:
<?
if($_POST["remember"]) {
if($_POST["language"] == "english"){
$string = "english";
} else {
$string = "french";
}
if($_COOKIE["lang"]) {
setcookie ("lang", "",time()-60, "/", "yourdomain.com", 0);
}
setcookie ("lang", $string, time()+2592000, "/", "yourdomain.com", 0);
}
if($_POST["language"] == "english"){
header("Location: english.htm");
exit;
} else {
header("Location: english.htm");
exit;
}
?>
the cookie there lasts for 30 days, that can be changed by altering the time()+2592000 part of the 2nd setcookie line (2592000 seconds after the current time, so a 60 second cookie would be time()+60). i think that all works, ask if theres any problems