Jump to content

[SOLVED] theory ? about sessions and database


koolaid

Recommended Posts

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.

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!

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.

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'];
?>

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.

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 ;)

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"  ;)

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.