Skip to content
Advertisement

How to display user log in information at the corner of the page using php

I created a web page that the user needs to log in. I want the user to see his/her information. That says hello user1 and click here to logout. Here my code, I can’t even see the logout button.

update: I see the username that I did not log in with that username. I entered the website with the username student. But it shows a different username. See the picture below.it supposed to write welcome student not yasemin

login code :

// LOGIN USER
if (isset($_POST['login_user'])) {
    $username = mysqli_real_escape_string($db, $_POST['username']);
    $password = mysqli_real_escape_string($db, $_POST['password']);

    if (empty($username)) {
        array_push($errors, "Username is required");


    }
    if (empty($password)) {
        array_push($errors, "Password is required");

    }


    if (count($errors) == 0) {

        $password = md5($password);
        $query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
        $results = mysqli_query($db, $query);
        $row = mysqli_fetch_assoc($results);


        if('student' == $row['user_type']) {
                 header('Location: ogrenciarayuzu.php');
                 exit();
    } elseif ('department' == $row['user_type']) {
                 header('Location: bolumsekreterligiarayuzu.php');
                 exit();

    } elseif ('institute' == $row['user_type']) {
                 header('Location: enstituarayuzu.php');
                 exit();    

    } 
    if (mysqli_num_rows($results) == 1) {
            $_SESSION['username'] = $username;
            $_SESSION['success'] = "You are now logged in";
            header('location: index.php');
        }else {
            array_push($errors, "Wrong username/password combination");
        }
        }

     }

     ?> 

I put this code where I want to see the login information.

  <?php session_start(); ?> // at the beginning of my code

   .
   .
   .
    <?php  
    if (isset($_SESSION['username'])) :
    ?>
    <p>Welcome <strong><?php echo $_SESSION['username']; ?></strong></p>
    <p ><a href="index.php" style="color: black;"> <input type="submit" value="Logout" name="logout" 
    class="button"/></a>  </p>

      <?php endif ?>

To make it log out successful, I put this code in my index.php file

<?php 
session_start(); 

if (isset($_GET['logout'])) {
session_destroy();
unset($_SESSION['username']);
header("location: login1.php");
 }
 ?>

Please help me why I can’t still see. What is the problem here?

Advertisement

Answer

Error in your code, your code failed to set the session, when the condition found true, then it redirect user to another .php page as per the condition which you have used.

Try this code.

<?php session_start(); ?>

    if (isset($_POST['login_user'])) {
        $username = mysqli_real_escape_string($db, $_POST['username']);
        $password = mysqli_real_escape_string($db, $_POST['password']);

        if (empty($username)) {
            array_push($errors, "Username is required");


        }
        if (empty($password)) {
            array_push($errors, "Password is required");

        }


        if (count($errors) == 0) {

            $password = md5($password);
            $query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
            $results = mysqli_query($db, $query);

        if (mysqli_num_rows($results) == 1) {
            $row = mysqli_fetch_assoc($results);
                $_SESSION['username'] = $username;
                $_SESSION['success'] = "You are now logged in";

                //if you want to redirect user as per its type, you can use this condition

                if('student' == $row['user_type']) {
                         header('Location: ogrenciarayuzu.php');
                         exit();
                } elseif ('department' == $row['user_type']) {
                             header('Location: bolumsekreterligiarayuzu.php');
                             exit();

                } elseif ('institute' == $row['user_type']) {
                             header('Location: enstituarayuzu.php');
                             exit();    

                } 
                else
                {
                    header('location: index.php');
                }
            }else {
                array_push($errors, "Wrong username/password combination");
            }
            }

         }
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement