Welcome Guest, Not a member yet? Register   Sign In
Best practice in determining if cookies are enabled on the user's machine
#1

[eluser]rainmanx[/eluser]
Hi guys, long time no see.

I searched through the forum for a solution to this, but I couldn't find any. It may seem a childish question, but please bare with me.

I need to test if cookies are enabled on the user's machine, in order to warn him that the functionality of the app is limited unless he will enable them (for instance, I use cookies to store the language preference and to make this preference persistent).

So, I came up with this:
Code:
$this->session->set_userdata('cookies_enabled', 'some_test_value');
$c_enabled = $this->session->userdata('cookies_enabled');
if (!$c_enabled) {
  echo 'You must enable cookies for the application to work properly.';
}
The code above could be part of, let's say, the default controller; it doesn't really matter. Logically, this code would produce a FALSE value for $c_enabled if cookies are disabled (my thinking is that the app is trying to read a value previously written in the cookie and if this fails, the return value is FALSE).

BUT, regardless of cookies being enabled or not, $c_enabled always has the right value (e.g. 'some_test_value').

Maybe this is not the best practice for testing if cookies are enabled or not. It definitely doesn't work and I don't know why.

If anyone has a solution to this or another approach that works, thanks in advance.
#2

[eluser]Michael Wales[/eluser]
Set the cookie variable, then move to another method to check if that cookie variable exists. I have no proof behind this, but just a theory that CI may be retrieving that data without actually checking the cookie if it all occurs within the same method (ie. caching the cookie data as it's stored/updated).
#3

[eluser]rainmanx[/eluser]
I did this in the controller:
Code:
private function CheckCookieSupport() {

  return $this->session->userdata('cookies_enabled');

}
    
function index() {

  // Test for cookie support
  $this->session->set_userdata('cookies_enabled', 'some_test_value');
  $c_enabled = $this->CheckCookieSupport();
  if (!$c_enabled) {
    echo 'Cookies must be enabled.';
  }
  ...

}
It still returns the 'some_test_value' value for $c_enabled, as if cookies were enabled even if they are not.

Maybe I don't see the forest because of the trees here...
#4

[eluser]rainmanx[/eluser]
Code:
private function CheckCookieSupport() {

  $c_enabled = $this->session->userdata('cookies_enabled');
  if (!$c_enabled) {
    echo 'Cookies must be enabled.';
  }

}

function index() {

  // Test for cookie support
  $this->session->set_userdata('cookies_enabled', TRUE);
  $this->CheckCookieSupport();
  ...

}
doesn't work either.
#5

[eluser]alpar[/eluser]
You should try a javascript solution, from what i know it's more common than a server side, Also php isn't the best at detecting if there aren't cookies setcookie() returns true even if they are disabled (of course it doesn't explain why do you get a value back). Did you tried it with more browsers different settings? Or do you use something like developer toolbar to make sure the cookie isn't actually there, left over?




Theme © iAndrew 2016 - Forum software by © MyBB