Safe way to work with cookies

Asked

Viewed 187 times

1

Colleagues

I am developing a system from which I intend to store in a cookie the product he selected, in case it closes the browser and comes back, the product remains active in this way:

setcookie('compras',$_SESSION['produto'],time()+3600); 

But I am worried if the cookie is disabled in his browser. Is there any way to resolve this? That is, it select a product and not finalize the purchase, close the browser and when you return, the product still appear in its cart.

  • I don’t know if it’ll help you, but you can use the localstorage

  • 1

    If cookies are turned off not even the session will work, since the session identifier requires the cookie, unless you ignore the minimum security idsession by URL parameter. You have Localstore and also Websql available in some browsers. But if the person is with cookies turned off he will be with everything else too.

  • Nothing prevents you from sending a message, asking you to activate cookies, sometimes :p

1 answer

1

Check for customer-enabled cookies yourself. If they are not, let him know about the functionality loss and ask him to enable cookies in the browser.

You can test with PHP:

<?php
session_start();

setcookie('compras', $_SESSION['produto'],time()+3600);

header('Location: VerificaCookies.php');

So on the page Verificacookies.php:

if(isset($_COOKIE['compras'])){
    echo 'Cookies habilitados';
} else {
    // Melhore as mensagens. Estas são só de exemplo
    echo 'Cookies desabilitados';
}

EDIT 1: ACCORDING (ALMOST) TO ANDERSON’S COMMENT

There is also the possibility to use Javascript - site W3C (I couldn’t check with Modernizr, because there are blocked websites for me):

if(navigator.cookieEnabled) {
    cookies = true;
} else {
    cookies = false
}

There’s this other site, Javascript Kit with more complete example also:

<script type="text/javascript">
var cookieEnabled=(navigator.cookieEnabled)? true : false

//if not IE4+ nor NS6+
if (typeof navigator.cookieEnabled=="undefined" && !cookieEnabled){ 
    document.cookie="testcookie"
    cookieEnabled=(document.cookie.indexOf("testcookie")!=-1)? true : false
}

//if (cookieEnabled) //if cookies are enabled on client's browser
//do whatever

</script>
  • 1

    I believe the same operation can also be done with Javascript, such as the library Modernizr ago.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.