PHP Session Variables Not Preserved

session_register() is not required and may be causing a problem here. Read the docs on session_register() - it is intended to assign session variables using existing variables.

and from here:

Well, session_register() tells PHP that a certain global variable should be considered a session variable. That means that at the end of the script's execution (which is when session data writes usually occur), the resulting value of that global variable will be written using the current enabled session handlers.

I think this is the problem that you are experiencing. At the end of the script execution the session variable gets over-written.


I was facing the same problem. The reason was, in php.ini I've set the value of session.cookie_secure parameter to 1. But I was using the http protocol instead of the https protocol; using the https protocol resolved the issue.

Setting session.cookie_secure = 1 indicates cookies should only be sent over secure connections. Therefore, I was getting new session_ids on every new page while using http.


One mistake that I see is that in the first file you are setting $_SESSION['textvar'] and in the second file you are calling $_SESSION['newvar'].

Also, I tested your code on a server I know is working and it worked fine other than the above error.

I also tried removing the session_register() and the code still works perfectly.