SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru adammc's Avatar
    Join Date
    Aug 2004
    Location
    Cairns, Australia
    Posts
    762
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile If else logged in - sessions

    Hi Folks,

    Can I possible have some consructive critism on this code?

    I basically want to check which type of member is logged in and then display the appropriate menu.

    PHP Code:

    else {   // if the user is logged in - display appropriate menu


    if (isset($_SESSION['student_id'])) 

        {
        echo 
    "student menu";
        }
        
        
    if (isset(
    $_SESSION['partner_id'])) 

        {
        echo 
    "partner menu";
        }
        


    Am I doing this right?
    Could I do anyting different?

    Many thanks

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,228
    Mentioned
    194 Post(s)
    Tagged
    2 Thread(s)

    log in logic

    Are the user types unique? That is, can someone be both a "student" and a "partner"? If not then "if else" may be better than 2 "if"s
    Are there more than 2 user types? If so perhaps a "switch case" would be best.
    You may also want to do more than simply check if the SESSION is set. Maybe test to see if it validates to a particular criteria.

  3. #3
    SitePoint Guru adammc's Avatar
    Join Date
    Aug 2004
    Location
    Cairns, Australia
    Posts
    762
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, thanks for the reply

    user_id is unique as its an ato inc mysql data type

    So I use 'if' on the first check then 'ifelse' on the 2nd check right?

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,228
    Mentioned
    194 Post(s)
    Tagged
    2 Thread(s)

    if else

    Once an "if" is TRUE, PHP won't bother with the rest of them, so it should save server resources (albiet a nominal amount). eg.
    PHP Code:
    if ($a == 'a')
    {
    //do a stuff
    }
    else if (
    $b == 'b')
    {
    //do b stuff
    }
    else
    {
    //do default stuff

    Using a series of "if"s the server will test each even when you know it will be FALSE. This probably only matters when the test is resource intensive (db call, lengthy calculation, etc.) but why not develop good practice while your at it anyway?


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
  •