Madness Posted March 15, 2008 Share Posted March 15, 2008 New to Sessions I'm new to php and getting a rough idea from websites etc but kinda stuck on one thing. I'm creating a text based RPG. Got a login working an it logs in and will display welcome $charname you have $bankedgold saved. My problem is atm it is very basic after login (due to just starting the code) and I am going to need to have a session for every logged in character which can be constantly updated, after every fight to update experience and gold on hand, after every level to update stats level, after every bank to sent goldonhand to bankedgold . I can get the code working for all the things to refresh but cannot figure out how to correctly use a session to create a unique ID and contain all the information I need to be refreshed inside. Can someone give me a basic outline of how to do this please? Quote Link to comment Share on other sites More sharing options...
Stooney Posted March 16, 2008 Share Posted March 16, 2008 Rather than sessions, what you're looking for is a database. Use MySQL tables to store their account info, gold, experience, (all other character info). Then after a fight, update the database with the new info. You will probably only need sessions for the login, and maybe during fights depending on how your system works. Quote Link to comment Share on other sites More sharing options...
Madness Posted March 17, 2008 Author Share Posted March 17, 2008 I have the database setup, everything works fine with it I can insert info and retrieve it. However I want the session for the login. How I see it Login - if valid details it loads a session with the gold experience stats etc However I am sure I need to use sessions, just unsure how to use them. I don't want the code to be written for me, I just want to be shown the theory behind the code and an example to learn the correct syntax and code from, then I'll continue coding as I go. Quote Link to comment Share on other sites More sharing options...
Madness Posted March 17, 2008 Author Share Posted March 17, 2008 Didn't mention this above :- I have a registration and a login working perfectly, all I really need to be shown is how to start a session and how to effectively use it (preferably without overload CPU usage and such) Quote Link to comment Share on other sites More sharing options...
aschk Posted March 17, 2008 Share Posted March 17, 2008 Well to use sessions you need to use the $_SESSION superglobal. e.g. during login... <?php session_start(); // Do database lookup $_SESSION['userID'] = 7; $_SESSION['bankgold'] = 394; ?> subsequent pages... <?php session_start(); echo $_SESSION['bankgold']; ?> If you ever need to update the database (which you will after each fight), then simply take the userID from the session and update the record pertaining to that id. Simple really... Quote Link to comment Share on other sites More sharing options...
Madness Posted March 17, 2008 Author Share Posted March 17, 2008 Perfect!!! Many thanks think I got the idea of them, gonna give them a try, if I get any more problems I'll ask more specifically lol. 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.