Welcome Guest!

If you do not have an account yet on The Web Squeeze forums, please Register! It’s FREE and there are many benefits:

  • Receive Fast Advice
  • Learn Programming Languages
  • Get Professional Website Reviews
  • Quick Troubleshooting Assistance

> Checking Cookies Enabled

This is a discussion on Checking Cookies Enabled, within the PHP section. This forum and the thread "Checking Cookies Enabled" are both part of the Programming Your Website category.

 
Reply to this topicStart new topic
> Checking Cookies Enabled
djeyewater
post Apr 27 2008, 03:15 PM
Post #1


Squeezing
***

Posts: 81
Joined: 18-February 08


Heres my problem:
When a user loads the page I try to create a cookie, then when they submit the form I check whether the cookie was created, then either process the POST or return a message telling them to turn on cookies.

Now if they turn on cookies after receiving the message and re-submit the form, it will still tell them they don't have cookies created (because the cookie wasn't created before submitting the form since they previously had cookies turned off). How can I get round this?

My thoughts are either:
  • No cookies message tells them to refresh the page after enabling cookies and then re-submit the form. Problems with this are that when they refresh the page they will get the browser message saying do they want to re-send POST information, may be offputting to some people. Also after they refresh the page, but before they re-submit the form it will still say cookies not enabled, which again may confuse some people.
  • Use javascript to create the cookie when the form is submitted. Problem with this is they need js turned on.
  • If cookies aren't enabled, change the action of the form to have a get var like ?nocookie=true on the end, then if this is detected, write a cookie. I don't think it's possible to reload a page while keeping POST vars and not have the user have to click okay on the 'do you want to resend POST information' alert? So I'd stick the POST vars into a SESSION, reload the page and process as normal but use the SESSION vars instead of POST.
Out of those I like the third one best, but there must be a better way to do this? Would appreciate any comments/suggestions.

Thanks

Dave
Go to the top of the page
 
+Quote Post
christopher
post Apr 27 2008, 08:59 PM
Post #2


Squeezing
***

Posts: 54
Joined: 15-February 08
From: Ottawa, Canada


You could test if the user has cookies enabled BEFORE even displaying the form:
CODE
if (!isset($_COOKIE['test_cookie'])) {
if (!isset($_GET['ct'])) {
setcookie('test_cookie', 'true');
header("Location: /test.php?ct");
} else {
echo "Enable cookies and <a href="test.php">click here</a>!";
exit;
}
} else {
echo "Display form here!";
}


1. First check that my test cookie is set.
2. If yes, display the form (and set your specific cookie).
3. If not, check if ct (stands for cookie test) GET variable is set.
4. If no, set the test cookie and redirect back to same page WITH ct GET variable set.
5. If they are redirected back and test cookie is not set, but ct GET is set, then they must have cookies disabled. Advise them to enable them.
6. If they are redirected back and the test cookie is set, then the form is displayed as in #2 above.


--------------------
Blog: annoyed.ca | Web Site Hosting: www.bluephyre.com
Go to the top of the page
 
+Quote Post
djeyewater
post Apr 29 2008, 04:05 AM
Post #3


Squeezing
***

Posts: 81
Joined: 18-February 08


Thanks for the suggestion.

The way I am planning to set the site out is the login form will appear on every page, also you don't have to login, it just unlocks extra functionality. (Probably I should have put that in my first post). Using your script, if someone doesn't have cookies enabled, and doesn't enable them because they don't want to log in, on every page that they visit the script will try to set a cookie and then reload the page (for people who do have cookies enabled I will set a session var so its not checked every page). I think only a very small number of people don't have cookies enabled, and I don't know how much of a problem reloading the page each time would create, so would this be a problem or not?

Thanks

Dave
Go to the top of the page
 
+Quote Post
If you found The Web Squeeze to be helpful, please donate so we can keep this site FREE, FRESH, and fortified with Web Design & Development info!
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   4 jamesicus 271 6th April 2008 - 11:33 PM
Last post by: jamesicus
No New Posts   2 djeyewater 249 19th July 2008 - 01:06 PM
Last post by: djeyewater