Jump to content

Sessions


Madness

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/96329-sessions/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/96329-sessions/#findComment-493136
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/96329-sessions/#findComment-493693
Share on other sites

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...

Link to comment
https://forums.phpfreaks.com/topic/96329-sessions/#findComment-493892
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.