Maybe stupid question but its still interesting for me. Is it possible to transfer some data between different sessions? Can I add some variable into another user's $_SESSION directly? Something like this abstract code:
$notMySession = getSessionById('123'); $notMySession['kindaInfo'] = 'something'
No directly you cannot transfer session data from one session to another session. That is what the session is made for.
I hope this helps you.
Each user has its own session, which PHP will use when it speaks to that specific user. That means that each session is isolated from one another.
Since the session is not stored on the users computer, there might be a way to reach the session files from your code and directly modify the files. But that doesn't sound like a particularly sane thing to do.
Related
So as a engineer, I usually require a concert understanding to be able to work with something. I feel like I understand the basics of a session. I am wondering about the specifics and details there of.
What are the limitations of a session?
How can I manipulate a session? What can explicitly not be done to or with a session.
What data structures does PHP use to define and manage sessions?
Is a PHP session different from any other session in any significant way?
I understand that these questions are general, so if anyone can simply suggest a good resource I would be thankful. There is plenty of info out there, but it is either too basic or teaching to a specific topic.
Thank you for the help.
Sessions is a way for the server to recognize you so he sends to you a customized version of the page instead of sending always the same page for everybody.
To recognize you one way is he tells the browser to save in your computer a small file with a simple text, and when you visit the page again the server would ask the browser for that file, if the browser sends it, and it contains the expected content, the server can now know this is you again. That are cookies.
Another way to maintain a session, a part from cookies, is the server puts a special unique token for you in the url of all the links the page has. Whenever you browse the site all pages you visit will have that token, the server see it and know it's the token it made to you, so he knows it's you again.
So both with cookies or url-based sessions, the server will have to save info about the sessions opened, for example to store the $_SESSION variables you create in PHP, if you create such a variable the server will save it to a file which he will later identified by your cookie or token content and when you re-visit the page he will read that file and load the $_SESSION variables you create last time.
Here's a good resource: http://php.net/manual/en/book.session.php
What are the limitations of a session?
I don't really know what you mean by that. Limitations in what context?
How can I manipulate a session?
To manipulate values, just use the $_SESSION superglobal directly.
What can explicitly not be done to or with a session?
Again, without context, it's hard to understand what you mean. I guess an important point is that sessions are transient, so you can't explicitly store data you want to keep indefinitely.
What data structures does PHP use to define and manage sessions?
The filesystem.
Is a PHP session different from any other session in any significant way?
What is another session?
http://php.net is the best source for your questions
PHP session is a very nice way of having persistent information on your site for different users.
Check out the PHP session functions you can use.
You can view examples of how to use sessions at php.net.
A session is most commonly associated with user accounts. A user can log into your site, and you create a user session to keep track of their information and make sure they are allowed to be logged in.
The basic assumption is that a session is secure, because the server is aware of the sessions in progress. Utilizing sessions over HTTPS is a fairly secure way of keeping users logged into your site (without HTTPS you run the risk of session hijacking).
The other basic function is to have persistent data about a given user. So let's say you wanted to keep track if the user has submitted a form, you could do:
$_SESSION['form_submitted'] = TRUE;
And now you can check that global variable whenever you want to know if that specific user has submitted the form. So the session (in the same way a cookie is used) allows you to do really cool things that otherwise would not be possible.
I have user information which I need to use in different places on the web application, stuff such as email, name, user settings, and so on and so forth.
The question I want to ask, is it wise to do so, or is it a huge waste of memory?
and if it's not wise to do so, are there other things which I can do which could solve my problem?
Thank you.
Store anything you need there, as long as you remember that it is kept on the server and will be retrieved for each page load in the session.
It's usually considered bad practice to store too much in the session as the house-keeping that the server has to perform starts to add up when you've got many users. You only really need to store a user id in the session then you can pull everything else out of a database.
Why not using Cookies??
Good afternoon, everybody.
I'm here with a doubt.
Already have an active session, and I have to create another session. with more data.
More This new session is getting empty and the data is going to the first session.
Anyone know how to add data in the session specified by name.
I would be very grateful for the help.
You cannot have multiple sessions active at the same time. However a session is just an associative array, so you can create multiple namespaces inside using different keys. For example
$_SESSION['one'] = $foo;
$_SESSION['two'] = $bar;
I believe you can specify separate sessions using session_name(). Another thing you can do is read the actual session files if you have access to them on the file system. Variables and data are all there in plain text.
But all this sounds very sloppy to me and I can't imagine why you would need to do it this way. Provide more information about what you're trying to accomplish and maybe we can help you find a more straightforward way of achieving it.
I saw this example in php manual page
http://www.php.net/manual/en/session.examples.php
The example will create a global session for all client. Can I use this example to create some global application for all client, instead of save it to DB or local file.
What're the pros and cons of this method?
Thanks for any help.
It might work, but I wouldn't recommend it... to much scope for potential confusion by othe rdevelopers working with the code, potential issues if you update session variables within the wrong scope, and the use of the term "session" for something that is not session-related can lead to a whole world of confusion
Yes it is possible by sharing the session id between two clients but sessions are only used to store temporary data of a user. So once a session is destroyed there is no way to retrieve that data.
No, this is not possible, or advised. A session is bound to one client, and clients do not share a session.
I have two pages and I want to pass data to each other.
How can I do this without accessing a DB?
Sessions? Cookies? someother magical way?
If you know how, can you please post sample code?
Thanks
Session variables is one way:
$_SESSION["variable"] = "value";
This variable can then be read/modified by another page.
Also note, that you need to start the session by calling start_session(); at the beginning of your script.
And Cookies are another way... You can also try writing in and out of a file instead of a DB
How does a user get between these two pages? I assume a Form based solution is out of the question...
Amongst the possibilities, here are some that I think about :
You could $_SESSION (see Session Handling) -- if both pages are accessed by the same user, without too much time between the two accesses, so the session doesn't expire.
You could store your data to a file ; that'll work fine if :
The amount of data is big
You want it to persist for a long time
But you'll have to do some cleaning-up by yourself
Another idea would be some external daemon, like memcached
But, as it's a caching engine, it's not necessarily good for storing data : the data that is cache can be removed from the cache even if it has not expired yet (i.e. if there is no place left in cache, memcached will remove some least used data)
Of course, if the data is small and you don't mind it going back and forth through the network, and both pages are accessed by the same user using the same browser, you could use cookies
Only a couple of possibilities, though ; my preferences would probably be :
$_SESSION
or files
Depending on your situation.