Sessions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • James

    #1

    Sessions

    I am confused. What is the differance between cookies and sessions. I
    know cookies are on the client and sessions on the server (where do
    server cookies come in?)?

    Can anyone give some real examples as the technical explanations are
    well - technical and I am not sure that I understand the point!

    Thanks...
  • Pedro Graca

    #2
    Re: Sessions

    James wrote:[color=blue]
    > I am confused. What is the differance between cookies and sessions. I
    > know cookies are on the client and sessions on the server (where do
    > server cookies come in?)?
    >
    > Can anyone give some real examples as the technical explanations are
    > well - technical and I am not sure that I understand the point![/color]

    Let's say you have a site with configurable colours :)

    User chooses #FF99CC backgroud-color.
    You now either send a cookie with that or set a session variable; and
    then output your pages with
    <?php echo '<body bgcolor="', $_COOKIE['bgcolor']:$_COOKIE['bgcolor']?'#FFFFFF', '">'; ?>
    or
    <?php echo '<body bgcolor="', $_SESSION['bgcolor']:$_SESSION['bgcolor']?'#FFFFFF', '">'; ?>

    Pretty much the same, right?
    Well ... tomorrow he returns and if you went for the cookie way he still
    sees the pages in #FF99CC; if you went the session way they'll be white
    now.


    .... getting more complicated now ...
    You decide to put the colour data in a database and so send the cookie
    with the userid instead of the colour values and get the colour values
    with a select:

    $sql = "select bgcolor, ... from userprefs where userid={$_COOKI E['userid']}";

    but you'll have to that query for every page!!!
    So you do it once and save the values in a session variable, and for
    every access you have (probably in a included file)

    <?php
    if (!isset($_SESSI ON['loggedin'])) {
    // log user in with $_COOKIE after checking it exists
    // set $_SESSION vars including
    $_SESSION['loggedin'] = true;
    }
    ?>

    Hope this made sense
    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--

    Comment

    Working...