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??
Related
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.
I am initially seeking guidance to make sure I go in the right direction. From there I will come back with the code and ask for further assistance. I realize it isn't cool to say "hey I dont know what I am doing so I want you to do it for me."
So, here is my situation. I would say my php skills are amateur, and I am looking to increase them by working on projects for myself so I can learn through practice and application. I have created a webpage which contains a form that is used to update a XML file (I am playing around with flatfile DBs at the moment). All works well, the file is updated and the users is brought back to the page and the updated file is displayed. What I would like to do is allow the user to receive an update while they are browsing the website that the XML file has been updated, as well as alert them to the file update if they are returning to the website after having left.
My thoughts are that this would be done by using php session variables, one when they first access the website and another when the XML file is updated by a user. For the one when they access the site I thought a variable with a unique ID and timestamp as well as a timestamp of the files lastmodified value. I realize that this requires keeping storage of the session values since the value will have to be compared to something or else it will always appear as the most recent version, hence no changes.
Now that I think about it I guess you wouldn't need a session variable created on file update since the comparison will be based on the lastmodified value.
Just want to know if I am on the right track or completely off-base.
Any input is greatly appreciated.
Storing the lastModfided value
For your specific case I hope i understood you right. You want to say "notify the user if he wasn't the last one to work on the file" right?
That should be easily doable using the session as after writing you could store the last modified date of the file in the session and them compair that when the user comes back. I don't see any issues with that approach.
If you have you users log in I'd much rather tie the notification to their accounts.
Your session files will be deleted if the user is way for to long and a new one will be created if he uses a different browser. So it would be a really instable way of giving notice.
For the notifications no matter where he is on the site
If your database is updated in the background and you then could like to create a notification for the user I'd advice again using the php session for that.
While you are able (using some rather ugly hacks) to edit the session files they are not a stable enough basis to implement a notification system.
If you just work of session cookies then I'd still just store a "notify this session id if the guy shows up again" somewhere and check those notifications on every page load.
I know this is not as performant/nice/cool but it shouldn't really matter and save you a lot time dealing with.
Hope i understood you right and it makes sense to youu
I'm running a web application that allows a user to log in. The user can add/remove content to his/her 'library' which is displayed on a page called "library.php". Instead of querying the database for the contents of the users library everytime they load "library.php", I want to store it globally for PHP when the user logs in, so that the query is only run once. Is there a best practice for doing this? fx. storing their library in an array in a session?
Thanks for your time
If you store each user's library in a $_SESSION as an array, as you suggested (which is definitely possible) you will have to make sure that any updates the user makes to the library are instantly reflected to that session variable.
Honestly, unless there is some seriously heavy querying going on to fetch a library, or you have tons of traffic, I would just stick to 'execute query whenever the user hits library.php'.
Consider the size of the data. Multiply that by the maximum number of concurrent users.
Then compare that the to memory avaiable on your server. Also consider whether or not this is a shared server; other sites needs resources too.
Based on this, it is probably best to either create a file that can be used (as per Remi's comment), or remain in the default stateless form and read every time. I doubt that reading the data each time is creating much of an overhead.
When the user login you can generate a xml file (USER_ID.xml for instance) that you display with xslt.
http://php.net/manual/en/book.xslt.php
Each PHP script dies when it completes, so data can not be kept permanentely live in a web application as you would do in a PC application.
One way could be sessions, but it depends on the amount of data you want to save. According to your example you are talking about a library, so it sounds to me like big quantity of data need to be saved, in such case the DB is the way to go, and yes you have to query it each time.
Another way could be to save them in an array inside a php file, but in the same way you have to query the DB each time, you would have to include such php file each time.
Since this is a db performance optimization, I would suggest that you take a look at memcached which matches your problem perfectly:
memcached is [..] intended for use in speeding
up dynamic web applications by
alleviating database load.
I think it would be best to store it in a Session.
It the user logs in, the Session is being created and you can save data in it using the superglobal:
$_SESSION['key'] = "value";
You can also store Arrays or everything else there and it can be cleared if the user logs out.
you care for performance; Please note:
Session may use database or file to store data.
database is here to be used instead of files, for it's performance and abilities.
use database, it is designed to be used exactly in such situations!
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.
So for a bit of background, I am creating a website with the Zend Framework. There is a page where I am using AJAX to save a rating to my database. I obvious need the key for the store in order to know what store the rating is to be saved for.
In order to access the store for the page, the URL is MYSTORE.com/stores/2. The 2 is the store key, so it could be 13, 10, whatever. What my PHP script currently does is when it loads the page, it stores the store_id as a session. Then if they rate the store (all in JS), it will snag the store_id value from session, and combine them to send an insert to my database. So here's my problem.
Somewhere down the line, I'll probably want to cache to save my server some trouble. I have never used one before, and am worried that instead of running the script that saves the store_id to session, the page loads from the cache and never stores store_id. This would mean that the review could theoretically be saved to the wrong store. Is this a reasonable worry, and is there a way around this?
My other question is if there was maybe a better way to do it. I'm hesitant to place the store id into the JS or HTML since (at least I think) you can mess with the scripts through Firebug, or other web tools. I'd like my page to be secure. Is there a better way to do this?
I hope my question makes sense, and thank you in advance.
-Ethan
My advice is don't solve a problem until you have a problem. When they load the page just put the movie ID in the URL, possibly with some sort of checksum or hash so someone can't just blanket upvote and downvote every ID.
There's no need to store this in the session. Just keep it in the database until you need to change it. Don't forget that sessions are file-based. Using them for performance gains is a little misguided. Just use them where appropriate.
Knuth said "premature optimization is the root of all evil" and that's what this looks like to me. You're right in that you greatly complicate your code by keeping an ID in the session and that can get out of sync with what the user is seeing (eg using the back button). Stick the ID in the Webpage and that problem is solved.