PHP serialized data stored in mysql db error

I have a solution for storing data based on a database. Works great! However, I have a problem storing a specific data type.

I have an application that uses CSRF tokens. When a form is created, it will create a token for that form. A token is a hashed (sha256) value of various types of values. One copy goes to the form, and the other copy is stored in sessions. When submitting the form, it compares the tokens to make sure they match.

Below is an example of a destruct function that updates db with new data

UPDATE session_manager SET variables= :variables WHERE 1=1 AND id = :id array(2) { [":variables"]=> string(152) "a:1:{s:4:"CSRF";a:1:{s:8:"register";a:2:{s:5:"token";s:64:"e749603241dec1911ef3a40d98b2f5185d389434060483297394b504cc904ede";s:4:"time";i:1443456816;}}}" [":id"]=> string(2) "49" } 

The update statement works fine and works fine. This is the problem that I have, the data is being updated, but the value of the "token" that you can see in the above data is not the same value in db which is lower (this is a binary data load):

 a:1:{s:4:"CSRF";a:1:{s:8:"register";a:2:{s:5:"token";s:64:"b48fc79fc2f51eff765c05476895238a42d9d45b2c1aeb7c6e4582d0381b7f4f";s:4:"time";i:1443456817;}}} 

It would seem that mysql is changing the meaning, and I cannot let my life determine the problem. The solutions I tried include:

  • serialization
  • json_encode
  • base64

Change the db encoding and what not. Tried different types of fields in db, for example TEXT, Longtext and BLOB. This does not seem to work for me :(

Here is sql for db

 CREATE TABLE session_manager( id BIGINT(11) PRIMARY KEY AUTO_INCREMENT NOT NULL, session_id VARCHAR(200), user_agent TINYTEXT NOT NULL, variables BLOB NOT NULL, initial_time DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, regenerate_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL ); 

Any reasons to keep in mind?

+6
source share
2 answers

Have you looked at the time index of your array? He seems to have changed too. This makes me think that the session persistence method is executed (at least) twice. The second time, the session is updated and overwrites the old value.

Run this code with a debugger connected or print / write a stack trace with each function call. This should give you a good idea when the value will be updated again.

PS: The update request request is called again on the next request before you can get the value?

+1
source

Ok So, after further investigation and taking input from everyone (greetings by the way). I solved my problem.

Turns out it has nothing to do with mysql at all. Actually it was connected with "favicon.ico". I use fancy URLs like you, and because I'm in dev, I never worried about the icon. By default, when the page loads, it tries to find the icon ( http: //localhost/favicon.ico ). The system assumes that the user is trying to access the controller (I use mvc), and since the controller does not exist, it is redirected to the home page. The token created on the home page is required because there is a form on it, and as a result, it generates the token a second time, freeing the original token. I realized this by looking at all the network connections through the developer console.

+1
source

All Articles