Replacing something when a user logs in

Discussion in 'Programming' started by passingTime, Aug 17, 2011.

  1. #1
    Hi everyone,

    I've gotten a login setup on a site and it displays somewhere on every page. What i am trying to do is when someone logins I would like it to be switched to a little control panel showing your name and other things that i am able to setup.

    I'm just not sure how to switch them, maybe this will help you get an idea of what i mean:

    if (logged in) {
    show control panel

    } else show login



    Must i use javascript for this?
     
    passingTime, Aug 17, 2011 IP
  2. techntuts

    techntuts Member

    Messages:
    197
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #2
    I think using javascript isn't good option. If the client had turned off javascript then it becomes problem. He cant see any control panel.
     
    techntuts, Aug 17, 2011 IP
  3. passingTime

    passingTime Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Any ideas on how to do it then? :S
     
    passingTime, Aug 17, 2011 IP
  4. ssmm987

    ssmm987 Member

    Messages:
    180
    Likes Received:
    4
    Best Answers:
    3
    Trophy Points:
    43
    #4
    If you login, either a cookie, or session variable is set.
    E.g.
    
    session_start();
    if(login_valid())
    {
     $_SESSION['name']=$_POST['loginname'];
    }
    
    Code (markup):
    You only have got to check if this variable is set
    
    if(isset($_SESSION['name']))
    {
     echo "Your name is: ".$_SESSION['name'];
    }
    
    Code (markup):
     
    ssmm987, Aug 17, 2011 IP
  5. passingTime

    passingTime Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thank you for the reply. I have a simple login i'm testing with. Which simply is:

    <form action='login.php' method='post'>
    <input type="text" name="username" /><br>
    <input type="password" name="password" /><br>
    <input type="submit" value="Log in" />
    </form>

    I want this only to show if the user is logged in. If not I want something else shown.

    Not sure how to do this though. So it'll end up as something like (i think):

    if(isset($_SESSION['username']))
    {
    echo "welcome";
    } else *display the login form*


    Not sure how to do this though :s
     
    passingTime, Aug 17, 2011 IP
  6. passingTime

    passingTime Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    The login.php is very simple as well right now:

    <?php

    session_start ();

    $username = $_POST['username'];
    $password = $_POST['password'];

    if ($username && $password){

    $connect = mysql_connect("localhost","root","") or die ("Couldnt connect!");
    mysql_select_db("phplogin") or die ("Couldn't find the DB");

    $query = mysql_query ("SELECT username && password FROM users WHERE username='$username'");

    echo "hello";

    } else echo "no username";
     
    passingTime, Aug 17, 2011 IP
  7. passingTime

    passingTime Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    But how do i remove the form?
     
    passingTime, Aug 17, 2011 IP
  8. passingTime

    passingTime Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Seriously? No one knows how to simply remove a HTML form if someone has logged in?
     
    passingTime, Aug 18, 2011 IP
  9. passingTime

    passingTime Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Bump. I'm still looking for an answer please. Forget the code it's definitely not something i would use on a site it was just a very simple write up to test this on.

    All i want to do is remove HTML elements if a user is logged in. The only way I know how is to create 2 pages, so 2 index pages. Once the user is logged in the user will have access to the second index page which does not contain the form.

    There has to be a better way of doing this though. How can I make it so that the form is removed once the user logs in?
     
    passingTime, Aug 19, 2011 IP
  10. expertmac

    expertmac Member

    Messages:
    78
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    46
    #10
    you want to remove login form or login form link from page? if i am correct then use this.

    <? if($session->logged_in){?>

    <a href="">Control panel</a>

    <? } else { ?>

    <a href="">Login</a> | <a href="">Register</a>

    hope this will help.

    Thanks
     
    expertmac, Aug 23, 2011 IP
  11. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #11
    If you want to write your own code in Javascript, you'll need a Javascript variable to tell you whether the user has logged in. So in your PHP login, once you ascertain that you get one (and only 1) record back from your select statement
    
    echo '<script>';
    // if the user is logged in
    echo 'var loggedIn = true;';
    //else
    echo 'var loggedIn = false;';
    echo '</script>';
    
    Code (markup):
    Then your Javascript code just has to see if loggedIn is true or false.
     
    Rukbat, Aug 24, 2011 IP