- Home /
Bringing a session variable to another page using php
so basically, i want to bring a session variable called 'username' that is generated when user successfully log in, then i want this variable to be usable in another scene. in my login.php
if login is successful -> $_SESSION['username'] = $username
i want to use this variable to another scene, but it seems like any of the scene does not know that this session variable even exist. Please help!
Answer by FlaSh-G · May 28, 2018 at 01:30 PM
A session is an entry in a serverside database. It's bound to a randomly generated ID that is submitted to the user upon creating the session. The user then stores it in a cookie and sends it with every further request to that website, so the server can grab the session that belongs to the ID.
Now, if you're talking about a scene, you're talking about a Unity app that probably communicates with a server using the WWW class or UnityWebRequest. By default, these classes do not have any support for handling cookies, so the server puts the session ID in the login request's answer and Unity straight up ignores it.
Since there's no standardized way of handling this, you have three options:
Do research on how to get WWW or UnityWebRequest to work with session cookies.
Write your own server side session system that doesn't rely on cookies, but just sends the generated ID back through regular means. Unity then stores the ID however you want rather than doing regular cookie stuff.
Send authentification data with every request. Probably not a good idea in the long run.
In the second case, how would you store the session id (client side)?
Depends on how you defined your ID. You'll probably end up with a string.
Yes, by now it is the "random" generated string associated to a new session.