![]()  | 
  | 
  | 
| Subject:
Javascript - Checking for Cookies Enabled Problem
 Category: Computers > Programming Asked by: catullus13-ga List Price: $10.00  | 
Posted:
18 Oct 2004 05:16 PDT
 Expires: 17 Nov 2004 04:16 PST Question ID: 416334  | 
I have a problem with a form that is using javascript to check to see
if the user's browser has cookies disabled. This is an online poll,
and I'm trying to check to see if the user already has a cookie set
from previously having taken the form or if the user is blocking
cookies on their browser. If either applies, then their response isn't
recorded and they are sent to a page that tells them they have already
taken the poll or have their cookies disabled. The function that
checks for a cookie is working fine, but the function that checks to
see if cookies are disabled has a problem. The "onSubmit" in the
<form> tag already is set to the validation function, so I can't set
that also to check for the cookies disabled. So, I'm trying to set the
cookies disabled function to 1 in the validation function, but it
doesn't want to pass that value to the conditions processing that
page. Here's the code:
In the Header include file, here is the function checking to see if
cookies disabled:
<!-- # CHECK TO SEE IF COOKIES ENABLED FOR POLL # -->
<SCRIPT LANGUAGE="Javascript">
<!--
 var cookieEnabled = false;
 if (typeof document.cookie == "string")
 {
   if (document.cookie.length == 0)
   {
     document.cookie = "test";
     cookieEnabled = (document.cookie == "test");
     document.cookie = "";
   }
   else
   {
     cookieEnabled = true;
   }
 }
//-->
</SCRIPT>
This is working fine.
Then, on the form page, here is the form tag and the cookies value I'm
passing as a hidden:
<FORM ACTION="/tbpoll/vote.html" NAME="tbpoll" METHOD="POST"
onsubmit="return validateForm(this)">
<INPUT TYPE="HIDDEN" NAME="cookies" VALUE="0">
Then, on the form page, here is the javascript validation code that is
checking to see if at least one of the radio buttons on the form has
been checked. In this code, I'm trying to set the value of cookies
disabled to 1, but it doesn't seem to be working:
<SCRIPT language="javascript">
<!-- HIDE
function validateForm(form) {
    if (isValidRadio(form.choice)) {
      return true;
    }
  if(cookieEnabled){form.cookies.value=1;}
  return false;
}
// validate that the user has checked one of the radio buttons
function isValidRadio(radio) {
    var valid = false;
    for (var i = 0; i < radio.length; i++) {
        if (radio[i].checked) {
            return true;
        }
    }
    alert("Make a choice from the radio buttons.");
    return false;
}
//-->
</SCRIPT>
Then, on the processing page, I have checking to see if cookies
disabled equal 1, or that a cookie has already been set (they've
voted):
 INCLUDE "poll_config.inc";
 if (!$quesid) { header("Location: ../index.html"); exit; }
 $voteyet = 0;
 $now = time();
 
 // See if this registered user has voted
 if ($tbvote == $quesid) { $voteyet = 1; }
 // If cookies are disabled they cannot vote
 if (!$cookies) { $voteyet = 1; }
 
if ($voteyet)
 {
    $tb_title = "Business Poll";
    INCLUDE "$DOCUMENT_ROOT/inc/tb_header.inc";
    echo ("<TABLE WIDTH=\"500\" CELLPADDING=\"0\" CELLSPACING=\"0\"
BORDER=\"0\">");
    echo ("<TR><TD><FONT FACE=\"Arial\" SIZE=\"-1\"><BR>");
    echo ("Our records indicate that you've already voted in this
poll, or that your computer does not accept cookies. In order to
preserve the integrity of the poll results, we don't allow visitors to
vote more than once. ");
    echo ("</FONT></TD></TR></TABLE><P>");
 
    INCLUDE "$DOCUMENT_ROOT/inc/tb_footer.inc";
    exit;
 }
 if (!$voteyet)
 {
   // if ($other) { $choice = "Other"; }
   if($other){$display=="";}
   $fields = "(quesid, choice, timestamp, wn, ipaddress, writein,
comments, display)";
   $vals = "($quesid, '$choice', $now, '$wn', '$REMOTE_ADDR',
'$other', '$comments', '$display')";
   $query = "INSERT INTO pollresp $fields VALUES $vals";
   $result = mysql($mysqldb, $query);
   // Set a cookie to prevent multiple voting.
   $expire = time() + 1209600;   // one week
   $expire = date("l, d-M-Y H:i:s", $expire);
   header("Set-Cookie: tbvote=$quesid; path=/; expires=$expire;
domain=.somecompany.com");
 }
I've tried again and again, but I can't get that variable value of
cookies to get set to 1. Can someone please help me with this one?
Thanks. | 
  | 
| There is no answer at this time. | 
  | 
| Subject:
Re: Javascript - Checking for Cookies Enabled Problem
 From: phptiger-ga on 18 Oct 2004 11:47 PDT  | 
I think I can do a cookie check for you in PHP. Why do you try to do that in JavaScript? So, if you want a solution in PHP, let me know.  | 
| Subject:
Re: Javascript - Checking for Cookies Enabled Problem
 From: catullus13-ga on 18 Oct 2004 12:26 PDT  | 
That's fine. I usually use Javascript for checking to see if cookies are enabled, but with the onSubmit in the <FORM> tag already handling the validation, I wasn't sure how to go about doing it.  | 
| Subject:
Re: Javascript - Checking for Cookies Enabled Problem
 From: phptiger-ga on 23 Oct 2004 15:05 PDT  | 
I'd do it like that:
// set the cookie like that after vote has been counted
 if ($voteyet>0) // this line has changed too - because we change the
value of $voteyet - only if there's a cookie value à la "tbvote_xx"
 {
   // if ($other) { $choice = "Other"; }
   if($other){$display=="";}
   $fields = "(quesid, choice, timestamp, wn, ipaddress, writein,
comments, display)";
   $vals = "($quesid, '$choice', $now, '$wn', '$REMOTE_ADDR',
'$other', '$comments', '$display')";
   $query = "INSERT INTO pollresp $fields VALUES $vals";
   $result = mysql($mysqldb, $query);
   /* Different code here: */
   // Set a cookie to prevent multiple voting.
   setcookie("tbvote_" . $quesid, $quesid, time()+1209600);
 }
and to check if there's already a cookie you just say:
$voteyet = ($_COOKIE['tbvote_'.$quesid]!="" ? $_COOKIE['tbvote_'.$quesid] : 0 );
and then check if($voteyet==0) you know that there's no cookie set
I hope you understand what I mean, otherwise just ask again ;)
Hopefully this is the answer to your problem.
PS: I'm using this system for a community + a CMS therefore it should work well | 
| Subject:
Re: Javascript - Checking for Cookies Enabled Problem
 From: catullus13-ga on 24 Oct 2004 16:24 PDT  | 
That isn't exactly what I was looking for. Right now, the code as it is does the checking to see if a cookie has been set just fine. My problem is checking to see if the user's computer is blocking cookies because the poll wants to treat these folks just the same as if they had no cookie. The javascript I usually use to do (see my code above in my question) isn't passing that value of 1 to the processing page, and I can't figure out why.  | 
| Subject:
Re: Javascript - Checking for Cookies Enabled Problem
 From: phptiger-ga on 27 Oct 2004 10:54 PDT  | 
So, you want everybody who has cookies disabled not to vote, right?
Well, then I'd go away from JavaScript (because what if JS is
disabled) and still only work with PHP:
The idea: Set a random cookie, check immediately afterwards if it's
available - if not, you'll know that you can't use cookies here,
therefore don't allow this user to vote
The code:
setcookie("cookie_check", "2004", time()+10); // set cookie with value
2004 (just for fun ;)) + let it expire after 10 secs
if($_COOKIE['cookie_check']!="2004") $voteyet = 1; // check if the
value of this cookie equals your value (2004, e.g.) - if not, you know
that this computer doesn't support cookies
The cookie will expire after 10 secs the script was created
Well, and this code should be replace your line:
if (!$cookies) { $voteyet = 1; }
I'm pretty sure that works but I haven't checked it yet (but I'll do
so as soon as possible).
I hope that's the solution for your problem.
Have a nice day! | 
| Subject:
Re: Javascript - Checking for Cookies Enabled Problem
 From: phptiger-ga on 11 Nov 2004 11:20 PST  | 
Well, I'd like to know if I helped you or not. Otherwise we still can try to figure that out - if you want to (doesn't seem like that?!) But mostly I'd like to know if my coding skills helped ;)  | 
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.  | 
| Search Google Answers for | 
| Google Home - Answers FAQ - Terms of Service - Privacy Policy |