Why can't the array in the SESSION be obtained sometimes?

Source: Internet
Author: User
Why can't the array in the SESSION be obtained sometimes? After logon: put the user object into the SESSIOn to access $ _ SESSION ['user'] ['priv']. Sometimes this is empty.
$ User = $ _ SESSION ['user']; and then access it. sometimes you can directly access $ _ SESSION ['user'] ['priv, what's going on?
My php version 5.4.22


Reply to discussion (solution)

Sometimes you can directly access $ _ SESSION ['user'] ['priv'] without proposing an array ($ user = $ _ SESSION ['user']). what's going on?
My php version 5.4.22

When the fetch fails: var_dump ($ _ SESSION ['user']); check the data and the result is correct. Contains data. It can be obtained in two steps, that is, it cannot be obtained before. In the same method, this cannot be obtained for the first time. This is the second time. The session data has not changed.

You 'd better give the test code

At least the result of var_dump ($ _ SESSION ['user ']);

The test code cannot be reproduced. The test is normal. There are actually two steps.

Eliminate one external cause first. Will it happen when you test and output $ _ SESSION ['user'] ['priv'], that is, when the first access has not come yet and set the session?
Cannot reproduce test solution

Have you ever wondered whether your code is wrong!

$ _ SESSION ['user'] ['priv'] = ''; // The value of priv is null. it is null for anyone to obtain it!

$ _ Default SESSION lifecycle: 20 minutes

The time has obviously exceeded.

Yes? Yes, yes? No ,???? Log ,?? What is the situation ?,?? Debug is convenient.

When the fetch fails: var_dump ($ _ SESSION ['user']); check the data and the result is correct. Contains data. It can be obtained in two steps, that is, it cannot be obtained before. In the same method, this cannot be obtained for the first time. This is the second time. The session data has not changed.
----------
Have you ever written a program? Have you ever encountered such a problem? I met it many times.

Eliminate one external cause first. Will it happen when you test and output $ _ SESSION ['user'] ['priv'], that is, when the first access has not come yet and set the session?
Cannot reproduce test solution


The problem is always here. There are two steps. Not a step. The test I mentioned cannot be reproduced, that is, writing test. php cannot be reproduced.
The actual program does not take two steps. The result is incorrect. Very stable.
----------------
I post it elsewhere. I have the same problems on the Internet.

Yes? Yes, yes? No ,???? Log ,?? What is the situation ?,?? Debug is convenient.


-----------------
Yes, you can. if you don't, you don't have to. if you don't have to, you don't have.
I posted it in three parts. It is impossible to view only one post.

The first var_dump ($ _ SESSION ['user']); is there a value? We recommend that you paste the code for analysis.

Have you ever wondered whether your code is wrong!

$ _ SESSION ['user'] ['priv'] = ''; // The value of priv is null. it is null for anyone to obtain it!


The program has been written for nearly 10 years. I have been working for more than 13 years, and I have been a boss for many years. how can I make mistakes in programming? Can I still face people who can see them and directly hit them?
It's strange. So send it.

Cannot be heavy ?, Your ?? Generation ??? Line generation? Different, do you not provide replacement? How to analyze?

You add log ,? Times? ?, All? Output
Var_dump ($ _ SESSION ['user']);
Var_dump ($ _ SESSION ['user'] ['priv']);
Easy to find ??.

You need to check the code. this is the real code:

// User is the user object obtained from the database $ user =$ _ SESSION ['user']; echo $ user ['priv']; displayed normally: 100 // echo $ _ SESSION ['user'] ['priv']; // The display is empty. This line is changed to the above two lines.

No matter which method is used for the second visit, it is okay. However, for the first visit, direct reading of two-dimensional data is inevitable, but separate reading is normal, I don't know what I understand, right...

No matter which method is used for the second visit, it is okay. However, for the first visit, direct reading of two-dimensional data is inevitable, but separate reading is normal, I don't know what I understand, right...


No. Go directly to the 15th floor and ignore other things.

Is it caused by garbled characters?

Is it caused by garbled characters?

No garbled characters.

Print_r ($ _ SESSION ['user']);
In this way, the data structure information can be obtained to make a judgment basis.
Because you cannot reproduce the error, you can only find the cause in the code that assigns a value to $ _ SESSION.

Most likelyUser objects retrieved from the databaseSometimes an array or an object


Have you ever wondered whether your code is wrong!

$ _ SESSION ['user'] ['priv'] = ''; // The value of priv is null. it is null for anyone to obtain it!


The program has been written for nearly 10 years. I have been working for more than 13 years, and I have been a boss for many years. how can I make mistakes in programming? Can I still face people who can see them and directly hit them?
It's strange. So send it.


It seems that the development of windows is not as high as yours! Windows is still being patched. the vulnerability is that the code is not well written ~~~! All yours are correct.

Print_r ($ _ SESSION ['user']);
In this way, the data structure information can be obtained to make a judgment basis.
Because you cannot reproduce the error, you can only find the cause in the code that assigns a value to $ _ SESSION.

Most likelyUser objects retrieved from the databaseSometimes an array or an object



In fact, I think it is a problem when assigning values to sessions!

This is the code used by the session function to read sessions in thinkphp:

            if (strpos($name, '.')) {                list($name1, $name2) = explode('.', $name);                return isset($_SESSION[$prefix][$name1][$name2]) ? $_SESSION[$prefix][$name1][$name2] : null;            } else {                return isset($_SESSION[$prefix][$name]) ? $_SESSION[$prefix][$name] : null;            }


If the session has a BUG, I think the TP user has long responded. he reads two-dimensional data directly when reading any session.
I suggest that you first look for the reason in the business logic and session assignment.
In addition, you said you had the same problem with you. I don't know if it is convenient to send some links.

Check whether the data retrieved from the database is abnormal.

It seems that the problem is not clear, and no reason is found.

There is no problem after dividing into two steps, and you have no idea about this. Closed the post. Thank you for your support.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.