Jump to content

[SOLVED] theory ? about sessions and database


koolaid

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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