简体   繁体   中英

PHP - can't get variable from session

I am a novice to PHP as well as web development. I have a problem with PHP session. On my site (Wordpress) there is a login form, that stores username in session. After login is successful, username is saved in session, as well as an array with some additional data. The problem is, when user is redirected to the next page (after login), session is empty. I start session like this:

 $lifetime = 7200;
 session_start();
 setcookie(session_name(), session_id(), time() + $lifetime);

Interesting thing is that session cookie files are created and stored, but the site can't reach them (as far as I understand). DevTools -> Application -> Cookies shows one file for login page:

  1. tj1c9o73f9cfc9iriomlo6ho24

and two for the next page:

  1. tj1c9o73f9cfc9iriomlo6ho24
  2. 07jfifapiqe7gemes3hf4ov8c6

I looked into "temp" folder and found out that there is a third file:

  • (sess_)vohv6511n76us10nb57lph8t23

and it has all the data I need. I really don't understand what is happening. Please, help me.

You don't need to explicitly set a cookie. The session handler does that for you unless you configure something else.

Are you calling session_start() on both the login page and the next page?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM