www.webdeveloper.com
Results 1 to 8 of 8

Thread: [RESOLVED] how to display username after logged in? Help

  1. #1
    Join Date
    Jul 2009
    Posts
    10

    resolved [RESOLVED] how to display username after logged in? Help

    Ok i know some of yous will know what i mean, I want to know how to display the username of the logged in user EXAMPLE (Welcome, _______)

    Index.php
    <title>Pulse-Box</title>
    <link rel="stylesheet" href="assets/css/style.css" type="text/css" media="all" />
    </head>
    <body>

    <div id="wrapper">

    <?php include ('header.php'); ?>

    <div id="nav"></div>

    <?php include ('leftbar.php'); ?>


    <div id="right">
    <h1><font size="2">News & Updates</font></h1>
    <p>
    Well hello and welcome, Today i have fianled installed and fixed all errors on my login script, if you spot any errors
    please report them to me <b>Via</b> <a href="contact.php">Contact</a> page.
    </p>
    <span class="postinfo"> Posted by Admin on 22:20 02/07/2009</span>

    </div>
    </div>


    <?php include ('footer.php'); ?>

    </body>

    Register.php
    <? include("includes/connect.php");?>

    <title>Pulse-Box Tester</title>
    <link rel="stylesheet" href="assets/css/style.css" type="text/css" media="all" />
    </head>
    <body>

    <div id="wrapper">

    <?php include ('header.php'); ?>

    <div id="nav"></div>

    <?php include ('leftbar.php'); ?>


    <div id="right">










    <div id="right">


    <?PHP session_start();?>
    <? include("includes/connect.php");?>

    <? if($_SESSION['logged_in'] == 'Yes') { ?>

    <script type="text/JavaScript">window.location="index.php";</script>

    <? } else { ?>
    <?//#########################################################//?>

    <? if($_POST['submit']) { ?>

    <?
    $fullname=$_POST['fullname'];
    $email=$_POST['email'];
    $username=$_POST['username'];
    $password=$_POST['password'];
    ?>

    <? if(!$fullname) { ?>
    Please enter your fullname.
    <? } else if(!$email) { ?>
    Please enter an email address.
    <? } else if(!$username) { ?>
    Please enter a username.
    <? } else if(!$password) { ?>
    Please enter a password.
    <? } else { ?>




    <?
    $new = "INSERT INTO `members` (`ip`, `fullname`, `email`, `username`, `password`)
    VALUES ('127.0.0.1', '$fullname', '$email', '$username', '$password')";
    mysql_query($new) or die(mysql_error());
    ?>


    <div id="right">
    <h1><font size="2">Registration Success</font></h1>
    <p>
    Thank you for registering with us, your accouunt has sucessfully been created, </ br>
    Please click <a href="index.php">here</a> to be returned to home page.

    </div>
    </div>

    <? } ?>

    <? } else { ?>


    <h1><font size="2">Register your free user account!</font></h1>

    <table cellpadding="0" cellspacing="0" border="0">
    <p>
    <tr>
    <td><p><font size="2pt">Username</font></p></td>
    <td> <input type="text" name="username" id="username" value=""> </td>
    </tr>

    <tr>
    <td><p><font size="2pt">Password</font></p></td>
    <td> <input type="password" name="password" id="password" value=""> </td>
    </tr>

    <tr>
    <td><p><font size="2pt">Full Name</font></p></td>
    <td> <input type="text" name="fullname" id="fullname" value=""> </td>
    </tr>

    <tr>
    <td><p><font size="2pt">Email</font></p></td>
    <td> <input type="text" name="email" id="email" value=""> </td>
    </tr>

    <tr><td colspan=""> <br><input type="submit" name="submit" id="submit" value="Register!">
    </table>
    </form>
    <br>
    <b><i>*All fields above have to be filled in otherwise you will not be able to sign up!*</b></i>

    <? } ?>

    <?//#########################################################//?>
    <? } ?>


    </div>














    </div>


    <?php include ('footer.php'); ?>

    </body>
    Please help me correct my scipt if its rong, and help me getting username displayed

    Regards Robert

  2. #2
    Join Date
    Apr 2008
    Posts
    102
    You'll need to have your login pages. (I didn't notice any in your post)

    Make sure you have a login.html and a login.php.
    Your login.html will be the login form and login.php will be the script that checks the username and password. This is what I use for login.php ...

    PHP Code:
    <?

    $dbhost 
    "localhost";
    $dbname "db";
    $dbuser "user";
    $dbpass "pass";

    mysql_connect $dbhost$dbuser$dbpass)or die("Could not connect: ".mysql_error());
    mysql_select_db($dbname) or die(mysql_error());

    $user $_POST['user']; 
    $pass $_POST['pass'];

    $sql="SELECT * FROM users WHERE user='$user' and pass='$pass'";
    $result=mysql_query($sql);

    $count=mysql_num_rows($result);

    if(
    $count==1){
    header("Location: http://mysite.com/membersarea.php?user=$user"); } 
    else 
    header("Location: http://mysite.com/loginerror.php");
    }
    ?>
    You put this at the top of your members only page ...

    PHP Code:
    <?
    $user 
    $_GET['user'];
    session_start();
    $_SESSION['user'] = $user;

    if(isset(
    $_SESSION['user'])){
    } else {
    echo 
    "
    <script language='javascript'>
    alert('Sorry, but you must login to view the members area!')
    </script>
    <script>
    window.location='http://mysite.com/login.html'
    </script>
    "
    ; }
    ?>
    And finally, to actually display the username put this code where you want it to be displayed ...

    PHP Code:
    <? echo "Welcome, $user"?>
    Now for an explanation ...

    This part connects to your database:
    PHP Code:
    $dbhost "localhost";
    $dbname "db";
    $dbuser "user";
    $dbpass "pass";

    mysql_connect $dbhost$dbuser$dbpass)or die("Could not connect: ".mysql_error());
    mysql_select_db($dbname) or die(mysql_error()); 
    This part checks to see if the password matches with the username:
    PHP Code:
    $sql="SELECT * FROM users WHERE user='$user' and pass='$pass'";
    $result=mysql_query($sql);

    $count=mysql_num_rows($result); 
    This part basically says that if the username and password match, then place the username as a url variable otherwise redirect to loginerror.php:
    PHP Code:
    if($count==1){
    session_start();
    $_SESSION['users'] = $user;
    header("Location: http://mysite.com/membersarea.php?user=$user"); } 
    else 
    header("Location: http://mysite.com/loginerror.php");

    This part takes the information out of your url variable user and puts it as the session called user:
    PHP Code:
    $user $_GET['user'];
    session_start();
    $_SESSION['user'] = $user
    This part says that if $SESSION['user'] is set, then continue with the rest of the website but if its not set, then send an alert saying you must be logged in and then redirects you to the login page:
    PHP Code:
    if(isset($_SESSION['user'])){
    } else {
    echo 
    "
    <script language='javascript'>
    alert('Sorry, but you must login to view the members area!')
    </script>
    <script>
    window.location='http://mysite.com/login.html'
    </script>
    "
    ; } 
    The reason we use the session is to determine whether the person is logged in or not and if they aren't then redirect them to the login page.

    Hope this helps.

  3. #3
    Join Date
    Jul 2009
    Posts
    10

    too complicated, i all ready have a login.php

    here is the index.php, register.php, login.php and connect.php

    connect.php
    <?

    $host = "localhost";
    $user = "user";
    $pass = "pass";
    $name = "dbname";

    $ms = mysql_pconnect($host, $user, $pass);
    if ( !$ms )
    {
    echo "Error connecting to database.\n";
    }

    mysql_select_db($db);
    ?>
    login.php

    <?PHP session_start();?>
    <? include("includes/connect.php");?>

    <? if($_SESSION['logged_in'] == 'Yes') { ?>

    <script type="text/JavaScript">window.location="index.php";</script>

    <? } else { ?>
    <?//#########################################################//?>
    <? if($_POST['submit']) { ?>

    <?
    // Get Form Data
    $username=$_POST['username'];
    $password=$_POST['password'];
    ?>

    <?
    // Check to see if the data is correct
    $getuserinfo = "SELECT * FROM members where username='$username' and password='$password'";
    $getuserinfo1 = mysql_query($getuserinfo) or die("No Info");
    $user = mysql_fetch_array($getuserinfo1);
    ?>

    <? if(!$username) { ?>
    Please enter your username.
    <? } else if(!$password) { ?>
    Please enter your password.
    <? } else if(!$user) { ?>
    Username/Password is incorrect.
    <? } else { ?>

    <?PHP
    session_start();
    $_SESSION['username']=$username;
    $_SESSION['logged_in']='Yes';
    ?>

    <script type="text/JavaScript">window.location="index.php";</script>

    <? } ?>

    <? } else { ?>

    <form method="POST" action="login.php">
    <table cellpadding="0" cellspacing="1" border="0">
    <tr><td> Member Login </td></tr>

    <tr>
    <td> Username </td>
    <td> <input type="text" name="username" id="username" value=""> </td>
    </tr>

    <tr>
    <td> Password </td>
    <td> <input type="password" name="password" id="password" value=""> </td>
    </tr>

    <tr><td colspan="2"> <input type="submit" name="submit" id="submit" value="Login!">

    </table>
    </form>

    <? } ?>
    <?//#########################################################//?>
    <? } ?>
    register.php
    <? include("includes/connect.php");?>

    <title>Pulse-Box Tester</title>
    <link rel="stylesheet" href="assets/css/style.css" type="text/css" media="all" />
    </head>
    <body>

    <div id="wrapper">

    <?php include ('header.php'); ?>

    <div id="nav"></div>

    <?php include ('leftbar.php'); ?>


    <div id="right">










    <div id="right">


    <?PHP session_start();?>
    <? include("includes/connect.php");?>

    <? if($_SESSION['logged_in'] == 'Yes') { ?>

    <script type="text/JavaScript">window.location="index.php";</script>

    <? } else { ?>
    <?//#########################################################//?>

    <? if($_POST['submit']) { ?>

    <?
    $fullname=$_POST['fullname'];
    $email=$_POST['email'];
    $username=$_POST['username'];
    $password=$_POST['password'];
    ?>

    <? if(!$fullname) { ?>
    Please enter your fullname.
    <? } else if(!$email) { ?>
    Please enter an email address.
    <? } else if(!$username) { ?>
    Please enter a username.
    <? } else if(!$password) { ?>
    Please enter a password.
    <? } else { ?>




    <?
    $new = "INSERT INTO `members` (`ip`, `fullname`, `email`, `username`, `password`)
    VALUES ('127.0.0.1', '$fullname', '$email', '$username', '$password')";
    mysql_query($new) or die(mysql_error());
    ?>


    <div id="right">
    <h1><font size="2">Registration Success</font></h1>
    <p>
    Thank you for registering with us, your accouunt has sucessfully been created, </ br>
    Please click <a href="index.php">here</a> to be returned to home page.

    </div>
    </div>

    <? } ?>

    <? } else { ?>


    <h1><font size="2">Register your free user account!</font></h1>

    <table cellpadding="0" cellspacing="0" border="0">
    <p>
    <tr>
    <td><p><font size="2pt">Username</font></p></td>
    <td> <input type="text" name="username" id="username" value=""> </td>
    </tr>

    <tr>
    <td><p><font size="2pt">Password</font></p></td>
    <td> <input type="password" name="password" id="password" value=""> </td>
    </tr>

    <tr>
    <td><p><font size="2pt">Full Name</font></p></td>
    <td> <input type="text" name="fullname" id="fullname" value=""> </td>
    </tr>

    <tr>
    <td><p><font size="2pt">Email</font></p></td>
    <td> <input type="text" name="email" id="email" value=""> </td>
    </tr>

    <tr><td colspan=""> <br><input type="submit" name="submit" id="submit" value="Register!">
    </table>
    </form>
    <br>
    <b><i>*All fields above have to be filled in otherwise you will not be able to sign up!*</b></i>

    <? } ?>

    <?//#########################################################//?>
    <? } ?>


    </div>














    </div>


    <?php include ('footer.php'); ?>

    </body>
    index.php

    <title>Pulse-Box</title>
    <link rel="stylesheet" href="assets/css/style.css" type="text/css" media="all" />
    </head>
    <body>

    <div id="wrapper">

    <?php include ('header.php'); ?>

    <div id="nav"></div>

    <?php include ('leftbar.php'); ?>


    <div id="right">
    <h1><font size="2">News & Updates</font></h1>
    <p>
    Well hello and welcome, Today i have fianled installed and fixed all errors on my login script, if you spot any errors
    please report them to me <b>Via</b> <a href="contact.php">Contact</a> page.
    </p>
    <span class="postinfo"> Posted by Admin on 22:20 02/07/2009</span>

    </div>
    </div>


    <?php include ('footer.php'); ?>

    </body>

    Please note im very new to php and just dont get some of this , please help me thanks alot!

  4. #4
    Join Date
    Apr 2008
    Posts
    102
    Well It looks like your login.php already set the session username, so all you have to do is place ...

    PHP Code:
    <? echo "Welcome, {$_SESSION['logged_in']}"?>
    Where ever you want it to welcome them.

  5. #5
    Join Date
    Dec 2005
    Posts
    2,984
    The above poster meant to say

    PHP Code:
    <? echo "Welcome, {$_SESSION['username']}"?>

  6. #6
    Join Date
    Apr 2008
    Posts
    102
    Sorry, I meant to put "username" not "logged_in"

  7. #7
    Join Date
    Jul 2009
    Posts
    10
    thanks aj_nsc You solved it! Thanks a million

  8. #8
    Join Date
    Feb 2012
    Posts
    2
    K. But, if I want to create somewhere on the web and display the username somewhere on the page without disturbing the content that is already on the page?

    And how can a user delete his account and make his username accesible to others. How to make any content on a different page not visible to the user who are not logged in and with a link to login.

    And you told us how to show the username and what will be displayed there if the user has not logged in? How to make a link with Login or Register appear if the user hasn't logged in?

    I am a novice pragrammer and I started learning PHP basics just today and fell into it. Stuck right here coz my dream is to make something like Google. And I'm the Founder amd CEO of Pupoz, M.S.Rishikesh.

    Regards,
    Me.

Thread Information

Users Browsing this Thread

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

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