www.webdeveloper.com
Results 1 to 5 of 5

Thread: PHP logged in user display

Hybrid View

  1. #1
    Join Date
    Mar 2012
    Posts
    3

    Unhappy PHP logged in user display

    hi, i have run into a slight problem. i have made a login system using php and i wish for the user (once logged in) to have their user name diplayed in the header of my site on all pages the visit whilst they are logged in. or if they are a guest for it to display 'guest'. i just cant remember where abouts i have to put this piece of code and what needs to be on each page. could anyone give me a clue as to where it goes and what it needs to be
    thanks in advance

  2. #2
    Join Date
    May 2004
    Location
    chennai, tamil nadu, India
    Posts
    442
    Are you setting the username in the PHP session ? If you can show me the code you are using for logging in the user I can give you some hints.
    Chris, Senior Developer,
    Php laravel developers,
    http://www.chrisranjana.com

  3. #3
    Join Date
    Mar 2012
    Posts
    3
    this is the code that i have on my login page (login.php)

    <?php
    mysql_connect("localhost", "root", "") or die(mysql_error());
    mysql_select_db("my_db") or die(mysql_error());

    if(isset($_COOKIE['ID_my_site']))

    {
    $username = $_COOKIE['ID_my_site'];
    $pass = $_COOKIE['Key_my_site'];
    $check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
    while($info = mysql_fetch_array( $check ))
    {
    if ($pass != $info['password'])
    {
    }
    else
    {
    header("Location: members.php");

    }
    }
    }

    if (isset($_POST['submit'])) {

    if(!$_POST['username'] | !$_POST['pass']) {
    die('You did not fill in a required field.');
    }

    if (!get_magic_quotes_gpc()) {
    $_POST['email'] = addslashes($_POST['email']);
    }
    $check = mysql_query("SELECT * FROM users WHERE username = '".$_POST['username']."'")or die(mysql_error());

    $check2 = mysql_num_rows($check);
    if ($check2 == 0) {
    die('That user does not exist in our database. <a href=add.php>Click Here to Register</a>');
    }
    while($info = mysql_fetch_array( $check ))
    {
    $_POST['pass'] = stripslashes($_POST['pass']);
    $info['password'] = stripslashes($info['password']);
    $_POST['pass'] = md5($_POST['pass']);

    if ($_POST['pass'] != $info['password']) {
    die('Incorrect password, please try again.');
    }
    else
    {

    $_POST['username'] = stripslashes($_POST['username']);
    $hour = time() + 3600;
    setcookie(ID_my_site, $_POST['username'], $hour);
    setcookie(Key_my_site, $_POST['pass'], $hour);

    //then redirect them to the members area
    header("Location: members.php");
    }
    }
    }
    else
    {

    ?>
    <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
    <table border="0">
    <tr><td colspan=2><h1>Login</h1></td></tr>
    <tr><td>Username:</td><td>
    <input type="text" name="username" maxlength="40">
    </td></tr>
    <tr><td>Password:</td><td>
    <input type="password" name="pass" maxlength="50">
    </td></tr>
    <tr><td colspan="2" align="right">
    <input type="submit" name="submit" value="Login">
    </td></tr>
    </table>
    </form>
    <?php
    }

    ?>

    i hope this can be of some use
    thanks for any help in advance

  4. #4
    Join Date
    May 2004
    Location
    chennai, tamil nadu, India
    Posts
    442
    Code:
    <?php
    if(isset($_COOKIE['ID_my_site']))
    
    {
    echo $_COOKIE['ID_my_site']; 
    
    }
    else
    {
    echo "Guest";
    }
    
    ?>
    Try the above snippet
    Chris, Senior Developer,
    Php laravel developers,
    http://www.chrisranjana.com

  5. #5
    Join Date
    Mar 2012
    Posts
    3

    Thumbs up thanks

    and this goes at the top of each of my pages or on just this page?
    thanks for the help

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles