PHP Displaying user info when logged in

Thomas farley

Alright, I have tried and searched everywhere to fix this but no luck.

All I am trying to do is display a users username and email (Who are logged in) and then print their details to thier account page. The problem is that all of the users in the database are being logged, I only want the users who is logged in to be displayed.

Db.php

<?php
$myConnection= mysqli_connect("localhost","root","") or die ("could not connect to mysql");

mysqli_select_db($myConnection, "register") or die ("no database");
>

Auth.php

<?php
session_start();
if(!isset($_SESSION["username"])){
header("Location: login.php");
exit(); }
?>

Login.php

<?php
  require('db.php');
    session_start();
    // If form submitted, insert values into the database.
    if (isset($_POST['username'])){
        $username = $_POST['username'];
        $password = $_POST['password'];
        $username = stripslashes($username);
        $username = mysqli_real_escape_string($myConnection, $username);
        $password = stripslashes($password);
        $password = mysqli_real_escape_string($myConnection, $password);
    //Checking is user existing in the database or not
        $query = "SELECT * FROM `users` WHERE username='$username' and password='".md5($password)."'";
        $result = mysqli_query($myConnection, $query) or die(mysqli_error());
        $rows = mysqli_num_rows($result);
        if($rows==1){
            $_SESSION['username'] = $username;
      $_SESSION['user_id'] = $row['user_id'];
            header("Location: index.php"); // Redirect user to index.php
            }else{
                echo "<div class='form'><h3>Username/password is incorrect.</h3><br/>Click here to <a href='login.php'>Login</a></div>";
                }
    }else{
?>

Register.php

<?php
    require('db.php');
    // If form submitted, insert values into the database.
    if (isset($_POST['username'])){
        $username = $_POST['username'];
        $email = $_POST['email'];
        $password = $_POST['password'];
        $username = stripslashes($username);
        $username = mysqli_real_escape_string($myConnection, $username);
        $email = stripslashes($email);
        $email = mysqli_real_escape_string($myConnection, $email);
        $password = stripslashes($password);
        $password = mysqli_real_escape_string($myConnection, $password);
        $trn_date = date("Y-m-d H:i:s");
        $query = "INSERT into `users` (username, password, email, trn_date) VALUES ('$username', '".md5($password)."', '$email', '$trn_date')";
        $result = mysqli_query($myConnection, $query);
        if($result){
            echo "<div class='form'><h3>You are registered successfully.</h3><br/>Click here to <a href='login.php'>Login</a></div>";
        }
    }else{
?>

Account.php //Where I want user data to be displayed on page

<?php
    // SQL query
    $strSQL = "SELECT * FROM users";

    // Execute the query (the recordset $rs contains the result)
    $rs = mysqli_query($myConnection, $strSQL);

    // Loop the recordset $rs
    // Each row will be made into an array ($row) using mysqli_fetch_array
    while($row = mysqli_fetch_array($rs)) {

       // Write the value of the column FirstName (which is now in the array $row)
      echo $row['username'] . "<br />";
      echo $row['email'] . "<br />";
      }

    // Close the database connection
    mysqli_close($myConnection);
    ?>
Professor Zoom
$strSQL = "SELECT * FROM users";

Why that query? if you say you wanted to display only the info about users logged in, you are getting all users without conditions

Do the query for the user who is logged in at the moment, something like

$strSQL = "SELECT * FROM users WHERE username = '".$_SESSION['username']."'";

or somethinbg like this

  <?php

  session_start(); //Add this

  //Also you have to add your connection file before your query
  require('db.php');

  // SQL query
  $strSQL = "SELECT username, email FROM users WHERE user_id = '".$_SESSION['user_id']."'";

  // Execute the query (the recordset $rs contains the result)
  $rs = mysqli_query($myConnection, $strSQL);

  // Loop the recordset $rs
  // Each row will be made into an array ($row) using mysqli_fetch_array
  while($row = mysqli_fetch_array($rs)) {

    // Write the value of the column FirstName (which is now in the array $row)
    echo $row['username'] . "<br />";
    echo $row['email'] . "<br />";

  }

  // Close the database connection
  mysqli_close($myConnection);

  ?>

I think it should have to work, tell me if it worked for you

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Displaying a portlet only when user is logged in

From Dev

Displaying data for logged user

From Dev

Displaying data connected to the logged in user

From Dev

iOS - Saving logged in user info

From Dev

Mvc4 set user as logged in when user info is found in the session

From Dev

When to check if user is logged in?

From Dev

NullReference when user is not logged in

From Dev

Displaying only logged in user's records

From Dev

Not able to access Spring Security Logged in User info

From Dev

Logged user: what info to send to the browser?

From Dev

angularjs logged in user info to be available throughout the app

From Dev

Check if user logged in / persist user info react native

From Dev

User restriction page when logged in

From Dev

Find When a user last logged in

From Dev

Check role of user logged in (PHP)

From Dev

php check if user is currently logged in

From Dev

PHP + Html tables displaying correctly info

From Dev

Displaying posts made by user that is logged in - c#, asp.net

From Dev

Displaying a user profile page PHP

From Dev

Powershell, getting user input for computer name and displaying that computers BIOS info, OS info and disk info

From Dev

PHP drupal 7 Menu_item won't show when user is nog logged in. _permission

From Dev

is_user_logged_in() return false even when logged in to WordPress?

From Dev

Why are my INFO logs still getting logged when level off?

From Dev

How to add user info when creating a user?

From Dev

Change NavigationView items when user is logged

From Dev

schtasks parameter to disable "execute when user logged on"

From Dev

Strange terminal navigation when logged in as new user

From Dev

Exception when checking if user is logged out

From Dev

schtasks parameter to disable "execute when user logged on"

Related Related

  1. 1

    Displaying a portlet only when user is logged in

  2. 2

    Displaying data for logged user

  3. 3

    Displaying data connected to the logged in user

  4. 4

    iOS - Saving logged in user info

  5. 5

    Mvc4 set user as logged in when user info is found in the session

  6. 6

    When to check if user is logged in?

  7. 7

    NullReference when user is not logged in

  8. 8

    Displaying only logged in user's records

  9. 9

    Not able to access Spring Security Logged in User info

  10. 10

    Logged user: what info to send to the browser?

  11. 11

    angularjs logged in user info to be available throughout the app

  12. 12

    Check if user logged in / persist user info react native

  13. 13

    User restriction page when logged in

  14. 14

    Find When a user last logged in

  15. 15

    Check role of user logged in (PHP)

  16. 16

    php check if user is currently logged in

  17. 17

    PHP + Html tables displaying correctly info

  18. 18

    Displaying posts made by user that is logged in - c#, asp.net

  19. 19

    Displaying a user profile page PHP

  20. 20

    Powershell, getting user input for computer name and displaying that computers BIOS info, OS info and disk info

  21. 21

    PHP drupal 7 Menu_item won't show when user is nog logged in. _permission

  22. 22

    is_user_logged_in() return false even when logged in to WordPress?

  23. 23

    Why are my INFO logs still getting logged when level off?

  24. 24

    How to add user info when creating a user?

  25. 25

    Change NavigationView items when user is logged

  26. 26

    schtasks parameter to disable "execute when user logged on"

  27. 27

    Strange terminal navigation when logged in as new user

  28. 28

    Exception when checking if user is logged out

  29. 29

    schtasks parameter to disable "execute when user logged on"

HotTag

Archive