-1

I have the following problem, I create a cookie in PHP and then read it in javascript and print it and this happens. I do not know how to correct it. Please help.

I read the cookie in javascript:

var micookie = (document.cookie.indexOf('resultado=') === -1 ? '' : ("; " + document.cookie).split('; resultado=')[1].split(';')[0]);

So he created the cookie in PHP:

setcookie("resultado","success",time() + 1, "/kira");

And it prints like this, as you can see in the image

Cerraste+sessi%C3%B3n 

Code that creates the notification

var i = -1;
var toastCount = 0;
var $toastlast;


var micookie = (document.cookie.indexOf('resultado=') === -1 ? '' : ("; " + document.cookie).split('; resultado=')[1].split(';')[0]);
var micookietipo = (document.cookie.indexOf('tipo_result=') === -1 ? '' : ("; " + document.cookie).split('; tipo_result=')[1].split(';')[0]);
micookietipo = decodeURIComponent((micookietipo + '').replace(/\+/g, '%20'))
var micookiedesc = (document.cookie.indexOf('desc_result=') === -1 ? '' : ("; " + document.cookie).split('; desc_result=')[1].split(';')[0]);
micookiedesc = decodeURIComponent((micookiedesc + '').replace(/\+/g, '%20'))


function alerta() {
  var shortCutFunction = micookie;
  var msg = micookiedesc || '';
  var title = micookietipo || '';
  var $showDuration = $(300);
  var $hideDuration = $(1000);
  var $timeOut = $(2000);
  var $extendedTimeOut = $(500);
  var toastIndex = toastCount++;
  var addClear = $('#addClear').prop('checked');
  toastr.options = {
    closeButton: false,
    debug: false,
    newestOnTop: false,
    progressBar: true,
    positionClass: 'toast-bottom-right' || 'toast-top-right',
    preventDuplicates: true,
    onclick: null
  };
  toastr.options.showEasing = 'swing';
  toastr.options.hideEasing = 'linear';
  toastr.options.showMethod = 'fadeIn';
  toastr.options.hideMethod = 'fadeOut';
  var $toast = toastr[shortCutFunction](msg, title); // Wire up an event handler to a button in the toast, if it exists
  $toastlast = $toast;

  if (typeof $toast === 'undefined') {
    return;
  }
};

if (typeof micookie !== 'undefined' && typeof micookietipo !== 'undefined' && typeof micookiedesc !== 'undefined') {
  alerta();
}

VALIDATE.PHP

    <?php
    $error = $_COOKIE['resultado'];

    if($error == 'error'){
    header("location: ../../index.php");
    } else {
    require_once "../biblioteca.php";
    session_start();

    $db = ConectaDb($dbHost, $dbUser, $dbPass, $dbName);

    $nombre=recoge("nombre");
    $email=recoge("email");
    $password=recoge("password");

    $consulta="SELECT * FROM users WHERE nombre='$nombre' AND email='$email' AND password='$password'";

    $result = $db->query($consulta);

    if (!$result) {
            print "<p>Error en la consulta.</p>\n";
    } 
    elseif ($result->fetchColumn() == 0) {
            setcookie("resultado","error",time() + 1, "/kira");
            setcookie("tipo_result","Datos incorrectos",time() + 1, "/kira");
            setcookie("desc_result","Usuario o contraseña incorrectos",time() + 1, "/kira");
            header("Location: ../../index.php");

    } 
    else {
            $consulta =  "SELECT * FROM users WHERE nombre = '$nombre'";
            $result = $db->query($consulta); 
            if (!$result) {
                print "    <p>Error en la consulta.</p>\n"; print "\n";
            } else {
                $consulta =  "SELECT * FROM users WHERE nombre = '$nombre'";
                $result = $db->query($consulta); 
                foreach ($result as $valor) {
                $tipo_usuario = $valor['tipo_usuario'];
                $foto = $valor['foto'];

                if($tipo_usuario == "admin"){  
                    setcookie("resultado","success",time() + 1, "/kira");
                    setcookie("tipo_result","Bienvenido Administrador $nombre",time() + 1, "/kira");
                    setcookie("desc_result","Has iniciado sessión correctamente",time() + 1, "/kira");
                    $_SESSION['tipo_user'] = 'administrador';
                    $_SESSION['usuario'] = $nombre;
                    $_SESSION['email'] = $email;
                    $_SESSION['fotoperfil'] = $foto;

                                        $carpeta = '../resources/musica/'.$nombre;
                    if (!file_exists($carpeta)) {
                        mkdir($carpeta, 0777, true);
                    }
                                        $carpeta = '../resources/voz/'.$nombre;
                    if (!file_exists($carpeta)) {
                        mkdir($carpeta, 0777, true);
                    }
                                        $carpeta = '../resources/luz/'.$nombre;
                    if (!file_exists($carpeta)) {
                        mkdir($carpeta, 0777, true);
                    }
                                        $carpeta = '../resources/comida/'.$nombre;
                    if (!file_exists($carpeta)) {
                        mkdir($carpeta, 0777, true);
                    }
                                        $carpeta = '../resources/foto/'.$nombre;
                    if (!file_exists($carpeta)) {
                        mkdir($carpeta, 0777, true);
                    }
                        header("Location: ../../panelcontrol_admin.php");
                return;
                }
                elseif($tipo_usuario=="user"){
                    setcookie("resultado","success",time() + 1, "/kira");
                    setcookie("tipo_result","Bienvenido Usuario $nombre",time() + 1, "/kira");
                    setcookie("desc_result","Has iniciado sessión correctamente",time() + 1, "/kira");
                    $_SESSION['tipo_user'] = 'usuario';
                    $_SESSION['usuario'] = $nombre;
                    $_SESSION['email'] = $email;
                    $_SESSION['fotoperfil'] = $foto;

                                        $carpeta = '../resources/musica/'.$nombre;
                    if (!file_exists($carpeta)) {
                        mkdir($carpeta, 0777, true);
                    }
                                        $carpeta = '../resources/voz/'.$nombre;
                    if (!file_exists($carpeta)) {
                        mkdir($carpeta, 0777, true);
                    }
                                        $carpeta = '../resources/luz/'.$nombre;
                    if (!file_exists($carpeta)) {
                        mkdir($carpeta, 0777, true);
                    }
                                        $carpeta = '../resources/comida/'.$nombre;
                    if (!file_exists($carpeta)) {
                        mkdir($carpeta, 0777, true);
                    }
                                        $carpeta = '../resources/foto/'.$nombre;
                    if (!file_exists($carpeta)) {
                        mkdir($carpeta, 0777, true);
                    }
                        header("Location: ../../panelcontrol_user.php");
                return;
                }
                }
            }

    }    
        $db = null;
    }
?>
14
  • 1
    time() + 1 means the cookie is destroy after one second Commented Mar 9, 2019 at 20:14
  • I know, i need this, is working this. Not working text correctly Commented Mar 9, 2019 at 20:15
  • Try and set it for longer and see if you can read it in PHP. And include more PHP code. We need to see everything up to the setcookie function Commented Mar 9, 2019 at 20:16
  • PHP is setting the cookie on a subdirectory. I suspect what you're seeing is a cookie from the root directory because the subdirectory cookie has expired. Commented Mar 9, 2019 at 20:17
  • The setcookie is up, is all here. the content of cookie is this Cerraste Sesión, but JAVASCRIP read this, print other text, this Cerraste + sessi% C3% B3n Commented Mar 9, 2019 at 20:18

1 Answer 1

1

You can decode the string in javascript like so:

var micookie = (document.cookie.indexOf('resultado=') === -1 ? '' : ("; " + document.cookie).split('; resultado=')[1].split(';')[0]);

decodeURIComponent((micookie + '').replace(/\+/g, '%20'))
Sign up to request clarification or add additional context in comments.

13 Comments

I will try now. Thanks for this
Your welcome. if this answer is what you seek please mark it as best answer. thanks @DavidPricop
What does this have to do with it? The problem isn't with encoding, it's that the value is totally wrong.
@Barmar no the value isn't wrong. I tested it and it turns out it's about encoding. because David is using other characters than normal English alphabet
The cookie is supposed to contain success. What special character is in that?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.