problem with cookie and user table mysql php

1

Hello people I am doing the part of "Keep the session started" in the following way: two cookies are created, one with the user and the other with a series of random numbers (which are a field of the user table) , the problem that I have is that when the cookie of the series of numbers sets it to me, it does so with a value that is no longer in the table, that is, the cookie always keeps the previous value that was in the table and never the current .. then they never coincide with me and therefore it does not work for me

  

Example of the PROBLEM: current value of cookie_login in the users table is 565, then the user if logue with the option "keep session started" this value is updated to another .. for example 124, but the cookie stays with the value 565 and I do not know why he does that ..   would believe that it is because the cookie is on the client side and then it is never synchronized in real time with the server .. but how can I solve it?

I attach the code:

 if($a->ConectarUser($_POST['user'],$_POST['pass'])==1)
    {
        echo "Se ha logueado correctamente";
        if ($_POST["recordame"]=="1") //es que pidió memorizar el usuario
        {
        //busco el id del usuario que hizo Esto , lo saco del metodo ConectarUser, el cual ya creò una sesion con el idusuario
        $iduser=intval($_SESSION["idUsuario"]);
  //1) creo una marca aleatoria en el registro de este usuario
  //alimentamos el generador de aleatorios
  mt_srand (time());
  //generamos un número aleatorio
  $numero_aleatorio = mt_rand(1000000,999999999);
  //2) meto la marca aleatoria en la tabla de usuario
  //3) ahora meto una cookie en el ordenador del usuario con el identificador del usuario y la cookie aleatoria
  setcookie("idusuario", $iduser , time()+(60*60*24*365));
  setcookie("marca_aleatoria_usuario_dw", $numero_aleatorio, time()+(60*60*24*365));

        $a->cargar_cookie_user($numero_aleatorio,$iduser);
  echo $_COOKIE['idusuario'];
        echo $_COOKIE['marca_aleatoria_usuario_dw'];
 }
    header('Location: ../main.php');
 }

greetings

    
asked by fer 06.05.2017 в 09:17
source

2 answers

0

your problem is the ignorance of the operation of cookies in general and the global variable $ _COOKIE.

What is a cookie?

A cookie is made up of a key that gives it a name and an associated value, which can be created, modified or deleted both in the client and in the server and which, once created, is sent in each HTTP request, of each file , as a parameter of this protocol. The name of cookies is derived from the computer term magic cookie, which defines the information that is sent and is returned the same in each request.

How does a cookie work?

The client with his browser makes a request to the server:

GET /index.html HTTP/1.1

The server answers:

HTTP/1.1 200 OK
Content-type: text/html
Set-Cookie: name=value
(content of page)

The browser sets the cookie with name = value. If nothing changes, the browser sends another request:

GET /spec.html HTTP/1.1
Cookie: name=value
Accept: */*

Now, how do I modify a cookie? If you modify the cookie with js, the change is made in the client, for example name = ger, the next page that you ask will say

GET /otrapage.html HTTP/1.1
Cookie: name=ger
Accept: */*

If instead the one that changes the value is PHP (or any language on the server) and puts name = fer, the answer will say

HTTP/1.1 200 OK
Content-type: text/html
Set-Cookie: name=fer
(content of page)

So what is the problem?

In your code, the client sends a cookie, php reads the cookie, inserts it into the global variable $ _COOKIE ['userid'], therefore it is 565. in your script (on the obvious server side) you do

$iduser = 124;
setcookie("idusuario", $iduser , time()+(60*60*24*365));

that if you read the definition it says: "setcookie () defines a cookie to be sent along with the rest of the HTTP headers." this means that the server is now ready to respond with

HTTP/1.1 200 OK
Content-type: text/html
Set-Cookie: idusuario=124
(content of page)

Peeeero, $ _COOKIE continues with the value he had before, which is the header of the header he received before, therefore his value does not change. then you do

 header('Location: ../main.php');

if in the first line of main.php you put echo $ _COOKIE ['userid']; will give you 124, because the page where you were (let's call login.php) answered with the header of set-cookie and location, the client's browser saved the cookie and said

GET /main.php HTTP/1.1
Cookie: idusuario=124
Accept: */*

Other things to consider

All this occurs in the headers, that is if you go to an echo or something that forces the page to be rendered, you will not be able to modify the cookies and you will receive a warning.

I hope it was clear. Greetings!

    
answered by 06.05.2017 / 21:15
source
0
setcookie("marca_aleatoria_usuario_dw", $numero_aleatorio, time()+(60*60*24*365));

Create the cookie in the browser

  echo $_COOKIE['idusuario'];

does not read the cookie you just sent, it reads from an array with the status of cookies at the time the script was started (before you change it)

but you can use $ random_number without problem or redirect to another page and that will already have the new cookie

header( 'Location: /login_ok.php' );
    
answered by 07.05.2017 в 04:31