SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey all...I need some quick help with sessions. I am designing a site that will use some fairly cutting edge stylsheets. I want to have it backwards degrade gracefully, but some of the thing I want to do won'y let me do that.

    I will have an initial page allowing users to choose their site options.

    Low or Hi

    Hi will be IE 5.5 specific, low will be all other browsers. I want to be able to use the same pages for the most part so I just want to use two different stylesheets. If they choose Hi, they get the Hi one and vice-versa.

    I would like to attach a variable to the HREF tag for Hi and Low thereby "assigning" a sessiona variable that will give them one stylesheet or the other for the duration of their session.

    THe problem is that I have never used sessions before. Could someone please point me in the right direction or help me out with some code?

    thanks in advance...peace out.

    ps...I think I can do most of the coding myself, I am mostly just asking for help with the sessions. Also, I am running the NuSphere package on Win98. Aren't there some sort of settings to get sessions running properly with WIN?
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My knowledge of sessions and cookies is pretty poor. But, I think that cookies may be more appropriate to your requirements. With a session, the session and associated session variables only last for the duration of the session. However, a cookie persists. So you when a visitor comes to the site you can check to see if they have your preferences stored in cookies. If not you present them with their preferences options and create some cookies on their HDD. If the cookies exist you read in the values which define their preferences.

    If you were to use sessions only, you would need a syste m where the user logs in and you store their user id in the session cookie and fetch their preferences from say a database table and store them as session variables.

    I know that goof has implimented something similar to what I think you want to do. I'll bet he's implimented it all with javascript too. Maybe its worth asking him.
    Last edited by freakysid; Apr 7, 2001 at 11:30.

  3. #3
    One website at a time mmj's Avatar
    Join Date
    Feb 2001
    Location
    Melbourne Australia
    Posts
    6,282
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Sessions are quite handy, but there are two methods for keeping a session active.

    The session id is stored on the client end, and all the variables are stored at the server end.

    One way of keeping the session active is to alter all of the links so that the session id is inserted onto the query string. The downside to this is it produces an ugly looking url that search engines don't like. However, it's not too bad. These forums have all sorts of stuff in the query string.

    The other way is to use a session cookie. It's a cookie stored at the client end that contains only the session ID, and it is set to expire when the browser is closed.

    PHP has support for both methods, and depending on how PHP is set up on your server, you may also be able to have it automatically choosing the mode, automatically inserting the id on the url if the client doesn't accept cookies.

    My server can't do it automatically, but with a bit of coding and a variable called "SID" I can get by.

    For heaps of info on how to do this (assuming a basic understanding of PHP) the PHP.net website is great:
    http://www.php.net/manual/en/ref.session.php

    For a simpler intro to sessions try kev's article. Please note that it doesn't have a lot of advanced info on sessions per se. I learnt most of what I know from the php.net page above.
    http://www.webmasterbase.com/article.php?pid=0&aid=319
    Last edited by mmj; Apr 7, 2001 at 12:15.
    [mmj] My magic jigsaw
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The Bit Depth Blog Twitter Contact me
    Neon Javascript Framework Jokes Android stuff

  4. #4
    SitePoint Columnist Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To be honest you don't even need to use sessions or cookies for this - you can automatically detect the browser a user is surfing with from the HTTP_USER_AGENT environment variable, and serve them the page for their browser based on that.

  5. #5
    SitePoint Member
    Join Date
    Apr 2001
    Location
    Newcastle, UK
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but the trick with that is making sure to use a match pattern that will only turn up a match with one browser though if you compare the results of echoing the http_user_agent var for diff types of browser you'll find something, I think when I last looked compatible was the word I locked onto between ie5.5 and netscape 4.75, dunno if thats still the case

  6. #6
    SitePoint Enthusiast
    Join Date
    Mar 2001
    Location
    Washington State
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    have a look here :

    Browser Detection and Appropriate CSS Generation :
    --------------------------------------------------
    http://phpbuilder.com/columns/tim20000821.php3

    be sure to look through user comments too.

  7. #7
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Andy I might try this approach if you are planning on using session anyway. I will assume you have a table of users with usernames and passwords, etc.... When you authenticate a user put their current session id in that table as well. And then in your preferneces table simply keep a key that corresponds to the user table like a session id. That way once they are authenticated and they have chosen their option either IE or NS then store that session var in the prefs table. That way next time they come to the site you can authenticate them then update the session id in both tables, that way you will always have a way to call their current prefs by their session id. I hope that might work for you. I haven't put a whole lot of though into it, but off the top of my head that seems the best route to take.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  8. #8
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    actually, i don't have users. I want this to be a nobrainer for people. I don't want them to login of anything like that. Just select the speed they want and away...

    Then just keep that speed selected through the duration of the visit. I don't even care if they have to select it again.

    are you picking up what I am putting down?

    WOuld cookies be a better option then Louie?
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes

  9. #9
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I'm not too experienced with sessions - so I'll post this code and let others critique it.

    First off, in your index.php you will want to test to see if a user $option is set. If $option is not set, present the user with a splash screen where they pick their entrance gate. Otherwise, register the session variable $option which will *either* be available as a get variable (because the user just clicked on thier preference) or as a session variable (because option was already registered as a session variable during this visitors current browsing session).
    PHP Code:
    <?php
    session_start
    ();
    if (! 
    $option) {
       
    // option is not set
       
    echo " <html><head><title>make up your mind</title></head><body>
              <a href='
    $PHP_SELF?option=high'>high</a>
              <a href='
    $PHP_SELF?option=low'>low</a>
              </body></html>"
    ;
    } else {
       
    // register the variable option in the session
       
    session_register("option");   
       if(
    $option == "high" {
          include(
    "hi.inc"); // do whatever has to be done for a "high" user here
       
    } else {
          include(
    "lo.inc"); // do whatever has to be done for a "low" user here
       
    }
    }
    ?>
    Now on subsequent pages you may just want to default to low if for some reason option is not set.
    PHP Code:
    <?php
    session_start
    ();
    if (
    $option "high") {
       include(
    "high.inc");
     } else {
          include(
    "lo.inc");
    }
    ?>
    Of course you may have in your mind a different method of the user selecting their option - maybe a popup window. But this is one workable (???) solution.

    BTW, as the session is managed using cookies it is important that not one smidgeon of text (including white space) is generated by the script before session_start() and session_register() are called. Otherwise your script will produce a "Cannot send headers. Headers already sent' or something, error.
    Last edited by freakysid; Apr 11, 2001 at 05:22.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •