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 8 of 8
  1. #1
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post

    You are logged in... (Short code)

    What is the best way going about displaying a message saying:
    Code:
    You are logged in as username!
    If they are logged in though, but if they are not logged in then it would say:
    Code:
    You are not logged in. Login - Register
    I want the code to be short as possible of course, don't want to go writting a load of code for such a simple thing. I would usually do it with an IF statement, but I was wondering if there was a shorter way.

  • #2
    Regular Coder
    Join Date
    Jan 2009
    Posts
    193
    Thanks
    0
    Thanked 20 Times in 20 Posts
    You could use a ternary operator like

    PHP Code:
    $user_is_logged_in 'display logged in message ' 'display logged out message'
    http://php.net/manual/en/language.op...comparison.php

  • Users who have thanked skywalker2208 for this post:

    martynball (03-30-2010)

  • #3
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post
    I have seen that kind of code before, Ima read up on it as I don't understand it fully. What exactly is the "?" for?

  • #4
    Regular Coder xconspirisist's Avatar
    Join Date
    Jun 2006
    Location
    Great Britain.
    Posts
    138
    Thanks
    1
    Thanked 6 Times in 6 Posts
    It is called a Ternary operator, it is essentially a shortened IF statement. The question mark means "then" and the colon means "else". These two are functionality identical;

    PHP Code:
    $value = ($myvar == 1) ? 'yes' 'no'
    PHP Code:
    if ($myvar == 1) { 
       
    $value 'yes';
    } else {
       
    $value 'no';

    If I have been helpful, use the "thank" button - It makes me happy!

    xconspirisist.co.uk - homepage of my online alias
    technowax.net - a community for people interested in all forms of modern technology.

  • Users who have thanked xconspirisist for this post:

    martynball (03-30-2010)

  • #5
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post
    In the PHPBB forum system they user defined vars such as S_USER_LOGGED (Logged in user). Do they have to reassign the var value everytime the page loads? :S

    Or do they use something which STAYS set the entire session? Sesssion var by any chance?

  • #6
    Regular Coder xconspirisist's Avatar
    Join Date
    Jun 2006
    Location
    Great Britain.
    Posts
    138
    Thanks
    1
    Thanked 6 Times in 6 Posts
    Similar. I imagine they use something like this;

    PHP Code:
    <?php

    define 
    ('S_USER_LOGGED_IN', isset($_SESSION['username']));

    //--

    if (S_USER_LOGGED_IN) {
       echo 
    "You are logged in.";
    } else {
       echo 
    "You are not logged in. Oh dear.";
    }

    ?>
    Bare in mind that defining it as a constant may look clean, but you are likely to come across a scenario where someone "logs out" half way through your script can your constant will obviously not update. I use something like this;

    PHP Code:
    abstract class Session {
       public static function 
    isLoggedIn() {
          return isset(
    $_SESSION['username']); 
       }
    }

    //--

    if (Session::isLoggedIn() {
       echo 
    'Welcome! You may have some pie.';
    } else {
       echo 
    'No pie for guests.';

    Last edited by xconspirisist; 03-30-2010 at 11:09 PM.
    If I have been helpful, use the "thank" button - It makes me happy!

    xconspirisist.co.uk - homepage of my online alias
    technowax.net - a community for people interested in all forms of modern technology.

  • Users who have thanked xconspirisist for this post:

    martynball (03-30-2010)

  • #7
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post
    wtf is a adstract class?

  • #8
    Regular Coder xconspirisist's Avatar
    Join Date
    Jun 2006
    Location
    Great Britain.
    Posts
    138
    Thanks
    1
    Thanked 6 Times in 6 Posts
    An abstract class is a type of class, that cannot be instantiated, ie: You cannot create new copies of that class.

    Classes are ways of grouping your code together so that you can reuse them. In PHP they are generally regarded as an advanced feature, because you can easily write scripts that do not uses classes.

    For more information, check out the php manual; http://uk3.php.net/manual/en/language.oop5.php
    If I have been helpful, use the "thank" button - It makes me happy!

    xconspirisist.co.uk - homepage of my online alias
    technowax.net - a community for people interested in all forms of modern technology.


  •  

    Posting Permissions

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