Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Nov 2010
    Posts
    393
    Thanks
    48
    Thanked 1 Time in 1 Post

    Question How to show different content depending on if the user is logged in?

    Hello,

    Currently I have no login system as I'm either going to need to find a good free one, wait until my friend's real life situations have been resolved or I hire a freelancer. However, I would like to set up the most basic, one account login system so I can implement the feature of seeing different content depending on whether the user is logged in or not.

    Maybe this system works differently to my logic, hence why I am posting for help. Eventually, depending on the users level, they will see different content. I suppose my first question would be, is this done by id elements, so if logged in and their access is 1 show this, if 2 show this and so on....

    Maybe I could use check boxes to show the differences, but nevertheless, I basically want to start working on the different things people see depending on their level and if they are logged in or not.

    Best Regards,
    Tim

  • #2
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,306
    Thanks
    11
    Thanked 283 Times in 282 Posts
    You'd probably end up using PHP for the authentication solution. Leaving that aside for the moment, your individual pages could be structured something like this to get you rolling on the idea:
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
    <title>Untitled 1</title>
    </head>
    
    <body>
    <?php
    $loggedin = true;       // hardcode these three here for testing, they will be set
    $username = "bob";   // by the auth system once we have one
    $userlevel = 4;         //
    
    if (!$loggedin)
       echo "<p>Cannot access this page without logging in</p>";
    else {
    if ($userlevel == 1) {
    ?>
    <p>You are user level 1</p>
    <?php
    }
    else
    if ($userlevel == 4) {
    ?>
    <div>
    <p>Welcome to the special page for user level 4 masters</p>
    </div>
    <?php
    }
    }
    ?>
    </body>
    </html>
    * completely untested code, I just typed it like this. You get your money's worth

    Dave

  • Users who have thanked tracknut for this post:

    MrTIMarshall (08-03-2012)

  • #3
    Regular Coder
    Join Date
    Nov 2010
    Posts
    393
    Thanks
    48
    Thanked 1 Time in 1 Post
    Hiya Dave,

    Thank you for your reply. If I did all my pages like your example, adding the "if user = logged in" and "If user level = 3", no matter how complex the actual login system ends up being like, is this the only/best way it's done?

    I'm only asking as it would be a shame to go through everything, implementing this and then needing to edit it all again in the future. Nevertheless, this will get me into practice!

  • #4
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,306
    Thanks
    11
    Thanked 283 Times in 282 Posts
    I couldn't tell you if this is the best way to do it. It certainly isn't the only way you could, but it is one reasonably simple way. Sorry for being vague here... I haven't done many auth systems, so I don't consider myself familiar with the various ways to do it, and was mostly just giving you a workable example.

    Dave

  • #5
    Regular Coder
    Join Date
    Nov 2010
    Posts
    393
    Thanks
    48
    Thanked 1 Time in 1 Post
    Quote Originally Posted by tracknut View Post
    I couldn't tell you if this is the best way to do it. It certainly isn't the only way you could, but it is one reasonably simple way. Sorry for being vague here... I haven't done many auth systems, so I don't consider myself familiar with the various ways to do it, and was mostly just giving you a workable example.

    Dave
    Well I did ask for basic, and there isn't much to implement this into at the moment, so if help comes along soon, there wouldn't be much to change

    Thanks!


  •  

    Posting Permissions

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