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 15 of 15
  1. #1
    Regular Coder
    Join Date
    Apr 2007
    Posts
    123
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Displaying username after login

    I want to display a users name on a page after a successful login. I've looked at the source for the site after logging in and it displays
    Code:
    Welcome, <a href="member.php?u=49542">rpjd</a>
    Can anyone explain the href to me?

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by rpjd View Post
    I want to display a users name on a page after a successful login. I've looked at the source for the site after logging in and it displays
    Code:
    Welcome, <a href="member.php?u=49542">rpjd</a>
    Can anyone explain the href to me?
    is a url to User CP of a given user. What do you want to explain? what is in the page? how it work? or what?

    best regards

  • #3
    Regular Coder
    Join Date
    Apr 2007
    Posts
    123
    Thanks
    1
    Thanked 0 Times in 0 Posts
    How it works would be interesting to know. I thought it would display the username using a session variable, which I can't get working:
    PHP Code:
    <?php $SESSION['Username'?>

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,332
    Thanks
    60
    Thanked 526 Times in 513 Posts
    Blog Entries
    4
    You need to use echo or print like this:

    PHP Code:
    //echo and print both do the same thing
    <? echo $_SESSION['username'];?>
    <? 
    print $_SESSION['username'];?>
    Last edited by tangoforce; 03-16-2011 at 12:48 AM.

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by rpjd View Post
    How it works would be interesting to know. I thought it would display the username using a session variable, which I can't get working:
    PHP Code:
    <?php $SESSION['Username'?>
    there are two things here:
    1. the page where you see that a anchor, is generated using the user id that u and his user name which apear between a tags. the url for href point to the user cp and doesn't do nothing until you click on it
    2. the user CP page, the url from href, is displayed only after you click on that link and redirect you to that page

    session are used only to check if you are loged in or not and probably for few other things like this.

    best regards

  • #6
    Regular Coder
    Join Date
    Apr 2007
    Posts
    123
    Thanks
    1
    Thanked 0 Times in 0 Posts
    This is my login script that works, not getting any errors and redirection works.
    PHP Code:
    <?php 
    global $Con;
    $Username $_POST['Username'];
    $Password $_POST['Password'];
    if(empty(
    $Username) || empty($Password)) 
      {
    echo 
    "Username and/or Password empty.  Fill in both to login";
    header('Location: http://localhost/DB/Login.php');
        }
    else{
    include(
    'E:/MySQLCon.php');
    $Con mysql_connect($host$user$pass);
    if(!
    $Con)
     {
      die(
    'There was an error connecting to the server, please try again later' mysql_error());
     }
     if(!
    mysql_select_db("DB"$Con))
    {
    echo 
    "not connected";
     }
    $Username str_replace("'","''",$Username);
    $Password md5($Password);

    // Verify that user is in database
    $query "SELECT Pass FROM password WHERE Username = '$Username'";
    $user mysql_query($query$Con);
    if(!
    $user || (mysql_numrows($user) < 1))
    {
    return 
    1//Indicates username failure
    }     
     
    // Retrieve password from result
    $Pass mysql_fetch_array($user);

    // Validate that password is correct
    if($Password == $Pass['Pass'])
    {
    session_start();
    session_register('Username');
    header('Location: http://localhost/DB/Home.php'); 
     }
    else
    {
     
    header('Location: Buyers Login.php');
    }
    }        
    ?>
    This is the script that I want to display the username on successful login, and guest if not.
    PHP Code:
    <?php 
    if(isset($_REQUEST[session_name()])) 
      {
      echo 
    "<div id='User'>Welcome: " $_SESSION['Username'] . "</div>";
      } 
    else
       {
    echo 
    "<div id='Guest'>Welcome: Guest </div>";
    }
    ?>
    However its displaying Welcome: Guest
    can anyone tell me whats wrong?

  • #7
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    not sure if will work:
    PHP Code:
    <?php 
    if(isset($_SESSION['Username'])) 
      {
      echo 
    "<div id='User'>Welcome: " $_SESSION['Username'] . "</div>";
      } 
    else
       {
    echo 
    "<div id='Guest'>Welcome: Guest </div>";
    }
    ?>
    best regards

  • #8
    Regular Coder
    Join Date
    Apr 2007
    Posts
    123
    Thanks
    1
    Thanked 0 Times in 0 Posts
    No luck with this either
    PHP Code:
    <?php 
    session_start
    ();
    if(isset(
    $_SESSION['Username'])) 
      {
      echo 
    "<div id='User'>Welcome: " $_SESSION['Username'] . "</div>";
      } 
    else
       {
         echo 
    "<div id='Guest'>Welcome: Guest </div>";
         }
    ?>

  • #9
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    PHP Code:
    if($Password == $Pass['Pass']) 

    session_start(); 
    $_SESSION['Username'] = $Username;
    header('Location: http://localhost/DB/Home.php');  
     } 
    (Your session_start() should be at the top of your script so you can check if the user at least came from the login page. The way your code is now, it is very hackable)

    then on each page

    PHP Code:
    <?php  
    session_start
    (); 
    if(isset(
    $_SESSION['Username']))  
      { 
      echo 
    "<div id='User'>Welcome: " $_SESSION['Username'] . "</div>"
      }  
    else 
       { 
         echo 
    "<div id='Guest'>Welcome: Guest </div>"
         } 
    ?>

  • #10
    Regular Coder
    Join Date
    Apr 2007
    Posts
    123
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks bullant, problem is neither Username or Guest is displaying. Surely one should?

  • #11
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    yep, one or the other definitely should.

    If nether are, then either your IF block is not being reached in your code before the script terminates or redirects elsewhere, or it is being bypassed during the running of your script.

    Most probable cause is a logic error in your code somewhere. Can you post your actual php code from the top of the scrip to at least the line after you IF block so we can see what is happening.

  • #12
    Regular Coder
    Join Date
    Apr 2007
    Posts
    123
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I've tested the login script and I've got both the username and the 32 character password to print. The redirect works as well. I tested the session
    PHP Code:
    if(isset($_SESSION['Username']))
      {
      echo 
    "ok";
      } 
    else
       {
         echo 
    "no";
         }
    ?> 
    and prints 'ok'. I have a style id 'User' to position the users name on the page. So I tested to see if the positioning works
    PHP Code:
    if(isset($_SESSION['Username']))
      {
      echo 
    "<div id='User'>Welcome </div>";
      } 
    else
       {
         echo 
    "no";
         } 
    and it didn't, yet other contents style accordingly. Can't understand why the Welcome won't print let alone position on page. Help!

  • #13
    Regular Coder
    Join Date
    Apr 2007
    Posts
    123
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Got it working at last. I didn't realise that I had display none in my css.
    Code:
    <?php 
    session_start(); 
    if(isset($_SESSION['Username']))
      {
      echo "<div id='User'>Welcome : " . $_SESSION['Username'] . " </div>";
      } 
    else
       {
    	 echo "no";
    	 }
    ?>
    Thanks again.

  • #14
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Quote Originally Posted by rpjd View Post
    Got it working at last. I didn't realise that I had display none in my css.


    Always use your browser's `view source` functionality.

    Glad to see you finally got it to work, though it's most likely been working all along.
    Last edited by kbluhm; 03-16-2011 at 03:02 PM.

  • #15
    Regular Coder
    Join Date
    Apr 2007
    Posts
    123
    Thanks
    1
    Thanked 0 Times in 0 Posts
    A whiI le ago I had my login script and display username working. I was registering the username with session_register() and it was working, but I read it has been deprecated, which has me wondering why? I'm trying to get this working with $_session which I understand to be a global variable. I changed
    PHP Code:
    session_start();
    $register_session('Username') = $Username
    to
    PHP Code:
    session_start();
    $_SESSION['Username'] = $Username
    in my login script. On the webpages I have
    PHP Code:
    session_start(); 
    if(isset(
    $_SESSION['Username']))
    {
    echo 
    "<div id='User'>Welcome : " $_SESSION['Username'] . " </div>";

    else
    {
    echo 
    "<div id='Guest'>Welcome : Guest</div>";

    Now I keep getting Welcome: Guest.
    Why if $_session is a global array is it not recognising the session Username?
    Or am I doing something wrong here?


  •  

    Posting Permissions

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