0

I'm pretty clueless on how to retrieve or echo a username from the database,

i have referenced this

and more similar questions like this, however i still don't understand how to echo out the current user username.

everything works fine, i can login and logout

just cant get find a way to echo current username

here is the code

User.php

public function get_user_sess()
{
    try{

        $query = $this->db->prepare("SELECT * FROM users WHERE user_name=':username'");
        $query->execute(array(':username'=>$username));
        $userRow = $query->fetch(PDO::FETCH_ASSOC);

        return $_SESSION['user_session'] = $userRow['username'];

    }

    catch(PDOExeception $e)
    {
        echo $e->getMessage();
    }
}

public function login($username, $password)
{
    try{
        $stmt = $this->db->prepare("SELECT * FROM users WHERE user_name=:username OR user_pass=:password LIMIT 1");
        $stmt->execute(array(':username'=>$username, ':password'=>$password ));
        $userRow = $stmt->fetch(PDO::FETCH_ASSOC);
        if ($stmt->rowCount() > 0)
        {
            if(password_verify($password,$userRow['user_pass']))
            {
                $_SESSION['login'] = TRUE;
                $_SESSION['user_session'] = $userRow['user_id'];
                return true;

            }

            else{
                return false;
            }
        }
    }


    catch(PDOExeception $e)
    {
        echo $e->getMessage();
    }
}

Profile.php

<?php
session_start();
error_reporting(-1);
require_once 'User.php';

$user = new User();
if(isset($_GET['q'])){
   $user->logout();
   $user->redirect('login');
}


require_once 'layouts/header.php';
?>
<h1>Welcome 
<?php 

$user = new User();
echo $user->get_user_sess();
?>
</h1>

<ul>
    <li><a href="profile.php?q=logout">LOGOUT</a></li>
</ul>

<?php require_once ('layouts/footer.php');?>

Login.php

<?php
session_start();
error_reporting(-1);
require 'Db.php';
require 'User.php';



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


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


    $guest = new User();
    if($username == "")
    {
        echo "Enter username please";

    }

    if($password == "")
    {
        echo "Enter password please";
    }



    if($guest->login($username, $password)){
        $guest->redirect('profile');      
    }


    else{
        echo "Invalid Entry";
    }





}

require_once 'layouts/header.php';

?>

<div class="container">
    <div class="row">
        <h1>Sign In</h1>


        <form action="" method="POST">
            <div class="form-group">
                <label>Username</label>
                <input type="text" name="txt_username" class="form-control" required/>
            </div>


            <div class="form-group">
                <label>Password</label>
                <input type="password" name="txt_pass" class="form-control" required/>
            </div>


         <button type="submit" name="btn_login" class="btn btn-primary">Log In</button>

        </form>
    </div>
</div>
8
  • put session_start() on top of your user.php Commented Jun 1, 2017 at 17:18
  • @MuhammadUsman i dont think its a session thing i think. here is the error i got A session had already been started - ignoring session_start() in /Applications/MAMP/htdocs/eli14/User.php on line 2 Commented Jun 1, 2017 at 17:23
  • 1
    $query->execute(array(':username'=>$username)); What about $username variable it is out of scope of function -- Is it global? Commented Jun 1, 2017 at 17:26
  • @MuhammadUsman its not global, what the correct format for retrieving a username ?thanks Commented Jun 1, 2017 at 17:28
  • Please check here codingcage.com/2015/04/… Commented Jun 1, 2017 at 17:30

1 Answer 1

1

In user.php file just set the session variable. For example in your function:

  $_SESSION['user_session'] = $userrow['username'];

Here do not return anything. Just set the session variable.

Now in any page where you want the username to be displayed Write

 session_start ();
 echo $_SESSION['user_session'];

Hope it clears your problem. Also in your PDO statement change

user_name=':username' // in select statement 

To

user_name=:username   // without inverted commas

Here no need of ':username' writing with inverted commas. Also no need of FETCH_ASSOC as you are getting only one row. So use fetch() PDO function. More updates:

See first login the user in your application. In the login script itself set the username session variable. Then

  1. In the profile page access the session value by calling

     session_start ();
      echo $_SESSION['user_session'];
    

    If you have doubt please update your login script file I will show you.

Hope you understand.

Third update:

  1. See in your login function you have already set the session variable

    $_SESSION['user_session'] = $userRow['user_id'];
    

    Therefore you are always getting the user I'd value from the session variable.

  2. So in the login function itself set

    $_SESSION['username'] = $userRow['username'];
    

    And then call this variable in the profile page. There no need to call the get_user_sess() function. Just start the session and get the variable.

Also your login script must sanitize user input values.

Sign up to request clarification or add additional context in comments.

8 Comments

you also made an syntax error with row ['username'] which will throw an undefined constant error.
yeah it still didnt work, i get no errors which is good, but no username echos
@CoderSam user_name=:username // in select statement change it to what ?
Remove inverted commas.
it still does not echo out username,it just prints out 1
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.