How can i set a default username for users that aren't logged in?

Discussion in 'Customization & add-ons' started by alexblyz91, Feb 21, 2020.

  1. alexblyz91

    alexblyz91 New Member

    Joined:
    Aug 9, 2019
    Messages:
    24
    I am using the following to get the users username:
    Code:
    $isloggedin=Am_Lite::getInstance()->isLoggedIn();
    $username=Am_Lite::getInstance()->getUsername();
    
    But when a user is not logged in i want to set them a default username of : nulluser
    how can i do this?
  2. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    You can use this code:

    PHP:
    $username Am_Lite::getInstance()->getUsername() ?: 'nulluser';
    Best Regards.
  3. alexblyz91

    alexblyz91 New Member

    Joined:
    Aug 9, 2019
    Messages:
    24
    Thanks Caesar, but how can i set the username as 'nulluser' only if the username is not already set? (user is not logged in)
    i am thinking maybe do an if statement to check if username is above 0 characters, and if not then set username as nulluser.

    is their a better way and if so could you provide an example?
  4. alexblyz91

    alexblyz91 New Member

    Joined:
    Aug 9, 2019
    Messages:
    24
    eg.
    if username is found - do nothing
    if username is not logged in - set username as 'nulluser'
  5. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    Please test my code. It works exactly as you need.

    If user is logged in then variable $username will have his username and otherwise string nulluser.
  6. alexblyz91

    alexblyz91 New Member

    Joined:
    Aug 9, 2019
    Messages:
    24
    awesome, i thought it looked to short, very simplistic, thankyou!
    caesar likes this.
  7. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295

Share This Page