Confused about sessions and cookies
Moderator: General Moderators
Confused about sessions and cookies
I'm new to PHP and I'm at a complete loss when it comes to cookies/sessions. I have read a ton about them in the last few days but just can't wrap my head around them completely. I'm trying to write a basic gift registry script that allows people to set up a registry and have others view it. The only feature needed is the ability for the registrant to update their registry via an admin type page. What I need is a way to store the users id when they log in to keep them logged in and to allow them to access the page where they can edit their registry. I know that I'll need cookies or sessions to do add this functionality, but I'm not sure how to go about it. Could someone help me out with an example of setting up a session for something like this? Also, where would the sessions be stored? Do they have to be stored in the database? Do they have to be stored at all, or can they just be passed on from page to page? Thanks in advance for any help.
Native PHP Sessions are stored in files on your server. They are associated with a unique session id that is stored in a cookie on the user's machine, unless they have disabled cookies, in which case it's stored in the url.
Basically, the data is just stored in a large (serialized) array which can be accessed/modified conveniently via the $_SESSION superglobal.
Basically, the data is just stored in a large (serialized) array which can be accessed/modified conveniently via the $_SESSION superglobal.
how are you planning on storing usernames and passwords? In a text file?
Code: Select all
session_start();
if($posted_validated_username == $valid_username && $posted_validated_password == $valid_password){
$_SESSION['logged_in'] = true;
}All user information will be stored in a database in a table called users. The username will be their email address. Basically I want to log them in and store their username in a cookie or session to check against when they access their page. If their session is open then a link to an admin page will be visible on their page. If not, the link won't be visible. Thats pretty much all I need to do as far as sessions are concerned.