Empty a cookie when logging

0

Good morning, how can I empty a cookie when logging? I was doing tests with unset but I did not empty the cookie.

Cookie that is in the index.php and opens a welcome modal

<script>
function getCookie(c_name){
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1){
    c_start = c_value.indexOf(c_name + "=");
}
if (c_start == -1){
    c_value = null;
}else{
    c_start = c_value.indexOf("=", c_start) + 1;
    var c_end = c_value.indexOf(";", c_start);
    if (c_end == -1){
        c_end = c_value.length;
    }
    c_value = unescape(c_value.substring(c_start,c_end));
}
return c_value;
}

 function setCookie(c_name,value,exdays){
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}

if(getCookie('tiendaaviso')!="1"){
$('#mensaje').modal('show');
}
 function PonerCookie(){
 setCookie('tiendaaviso','1',365);
 $('#mensaje').modal('hide');
 }
</script>

File logout.php

<?php
 //Inicio la sesión
   session_start();

   $_SESSION["autorizado"] = "N";
   $_SESSION["user"] = "";
   $_SESSION["pass"] = "";

   $link =$_POST["link"];

   if($link == "")
   $link = "/";
   header("Location: /_admin/");

   // Destruir todas las variables de sesión.
   $_SESSION = array();

   if (ini_get("session.use_cookies")) {
    $params = session_get_cookie_params();
    setcookie(session_name(), '', time() - 42000,
    $params["path"], $params["domain"],
    $params["secure"], $params["httponly"]

   );
 }
 session_destroy();
 ?>

Close session button

<script type="text/javascript">
var elems = document.getElementsByClassName('confirmation');
var confirmIt = function (e) {
    if (!confirm('¿Estas seguro que quieres cerrar sesión?')) 
e.preventDefault();
};
for (var i = 0, l = elems.length; i < l; i++) {
    elems[i].addEventListener('click', confirmIt, false);
}
</script>
    
asked by Miguel 03.10.2018 в 07:24
source

2 answers

0

With JS it would be something like this:

const borrarCookie = (cookie) => {
 document.cookie = cookie + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}

const getCookie = (cname) => {
  var name = cname + "=";
  var decodedCookie = decodeURIComponent(document.cookie);
  var ca = decodedCookie.split(';');
  for(var i = 0; i <ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0) == ' ') {
      c = c.substring(1);
    }
    if (c.indexOf(name) == 0) {
      return c.substring(name.length, c.length);
    }
  }
  return false;
}

$("#crear").on("click", () => {
  document.cookie = "tiendaaviso=tienda aviso es genial";
});

$("#eliminar").on("click", () => {
  borrarCookie("tiendaaviso");
});

$("#leer").on("click", () => {
  let valorCookie = getCookie("tiendaaviso");
  if(valorCookie != false){
    alert('El valor de la cookie tiendaaviso es "${valorCookie}"');
  }else{
    alert("La cookie tiendaaviso no tiene valor");
  }

});

You can see it working here

With PHP it would be something like this:

unset($_COOKIE["tiendaaviso"]);

You can see it working here

If you still get a safe error the cookie has a multidimensional value and is not a single level, to eliminate it you would have to go through the $_COOKIE with a loop and go eliminating it or if you already know in what level you can eliminate it directly, if you can place a print_r($_COOKIE) and show us how it shows you it would be very helpful.

    
answered by 03.10.2018 в 15:53
-1
public function cerrarSesion() {
        session_destroy();
        setcookie('usuario', null, time()-1);
    }
    
answered by 03.10.2018 в 08:51