koolaid Posted May 23, 2008 Share Posted May 23, 2008 Do you need to write to a database to start a session? I just need to pass 1 variable from page to page, but it does not need to be saved after the user closes the browser. Do i have to set up a database? What would be an easy approach to this? Quote Link to comment Share on other sites More sharing options...
haku Posted May 23, 2008 Share Posted May 23, 2008 You don't need to have a database to use sessions. They are completely independent things. Quote Link to comment Share on other sites More sharing options...
unidox Posted May 23, 2008 Share Posted May 23, 2008 Use session_start(); on the top of each page that you need the session to be read. Set the session using $_SESSION['name'] = "Bob"; Quote Link to comment Share on other sites More sharing options...
vurentjie Posted May 23, 2008 Share Posted May 23, 2008 and unset a session with unset( $_SESSION['sessionvariable'] ); apologies for repeating....mistake Quote Link to comment Share on other sites More sharing options...
unidox Posted May 23, 2008 Share Posted May 23, 2008 no you don't need a database to register a session variable, although in some instance the scenario is that a user logs into some site, his details are checked against a database and if ok, session variables are registered, basically you could do the following without a database: at the very beginning of each page where you neeed the session variable add the following session_start(); you assign session variable like so session_register("sessionvariable"); $_SESSION['sessionvariable']=$othervariable; and then for ease assign $mysession = $_SESSION['sessionvariable']; unset a session with unset( $_SESSION['sessionvariable'] ); just do a google and you will get a lot of information You just repeated what I posted but with code that he doenst need. Please read post guidelines before you get a permanent ban. Quote Link to comment Share on other sites More sharing options...
koolaid Posted May 23, 2008 Author Share Posted May 23, 2008 Thank you guys! I have found a ton of tutorials for sessions but they all seemed to include data bases. I know that a database would be a more powerful tool, but in this case i really don't need it. I am so new to PHP i was not sure how to accomplish this w/o going crazy. Thanks again! Quote Link to comment Share on other sites More sharing options...
haku Posted May 23, 2008 Share Posted May 23, 2008 It's not that databases are more powerful, its that databases and sessions serve different purposes. Sessions are temporary storage for small pieces of data, whereas databases are long-term/permanent storage that can hold data from tiny sizes up to huge sizes. Quote Link to comment Share on other sites More sharing options...
.josh Posted May 23, 2008 Share Posted May 23, 2008 The reason why some people tend to mix the two together is for security reasons. People like to hijack/fake session info, so coders like to store that info in the db and then verify the info (like the session id) against what's in the database at the time. Also, they are "mixed" so-to-speak, because many things you do, use both of them. For instance, You would use the db to hold information about someone, like their name, address, password, etc.. and you would have the user login to your site and you would use a session to hold that information from page to page to keep them "logged in." Sessions are most commonly used for signifying whether someone is "logged in" or "logged out" (though there are many many other uses for it). If you know a thing or two about computers, think of a database as the computer's rom or harddrive, and think of sessions as the ram. If you're just looking to do something simple, non-data sensitive, forget the whole database thing, as it is overkill. Based on your original post, all you need is something as simple as this: page1.php <?php session_start(); $_SESSION['somevariable'] = "blah"; echo "<a href = 'page2.php'>go to next page</a>"; ?> page2.php <?php session_start(); echo $_SESSION['somevariable']; ?> Quote Link to comment Share on other sites More sharing options...
koolaid Posted May 23, 2008 Author Share Posted May 23, 2008 That is EXACTLY what i am looking for. I am flash developer (i know you hate me) .... anyway i have a game in flash and i need to pass just one variable from stage to stage. However, i need the stages to be different pages rather then navigating within my flash piece. So here i am. Thank you for the simple explanation. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted May 23, 2008 Share Posted May 23, 2008 You just repeated what I posted but with code that he doenst need. Please read post guidelines before you get a permanent ban. Strictly necessary? Quote Link to comment Share on other sites More sharing options...
rondog Posted May 23, 2008 Share Posted May 23, 2008 You just repeated what I posted but with code that he doenst need. Please read post guidelines before you get a permanent ban. Strictly necessary? haha seriously he posted a minute or two after you. Happens all the time. That is EXACTLY what i am looking for. I am flash developer (i know you hate me) Im a flash developer that uses php constantly with it and noone hates me Quote Link to comment Share on other sites More sharing options...
.josh Posted May 23, 2008 Share Posted May 23, 2008 LoL I have dabbled in flash quite a bit, myself. Quote Link to comment Share on other sites More sharing options...
Wolphie Posted May 23, 2008 Share Posted May 23, 2008 Sessions are also extremely popular for shopping carts and eCommerce. I tend to only store a single session variable to determine whether a user is logged in or not; rather than storing their username etc.. in session variables. Cookies pretty much accomplish the same task except sessions are stored on the server, and cookies are stored locally. Remember to click "Topic Solved" Quote Link to comment Share on other sites More sharing options...
haku Posted May 23, 2008 Share Posted May 23, 2008 Sessions also store a cookie on the user's machine. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.