How to make a welcome screen in PHP?

Asked

Viewed 703 times

1

I want to create a welcome page for the user who has logged on to the site.

My difficulty is in creating the query as I do and showing the user name through an echo?

login.php form

<?php 
    session_start();
    if(isset($_POST['submit'])){
        $user = $_POST['username'];
        $pwrd = $_POST['pwrd'];
        //include database connection
        include('includes/db_connect.php');
        if(empty($user) || empty($pwrd)){
            echo 'Nada informado';
        }else{
            //prevenção de sql injection
            $user = strip_tags($user);
            $user = $db->real_escape_string($user);
            $pwrd = strip_tags($pwrd);
            $pwrd = $db->real_escape_string($pwrd);
            $pwrd = md5($pwrd);
            $query = $db-> query("SELECT user_id, username FROM user WHERE username='$user' AND password='$pwrd'");

            //echo $query->num_rows; ver se tem algo no banco

            if($query->num_rows === 1){
                while($row = $query->fetch_object()){
                    $_SESSION['user_id'] = $row->user_id;       
                }


                header('Location: admin/index.php');
                exit();
        }else{
            echo 'Nada informado';
        }
      }
    }

    ?>

index php. from the welcome page

    <?php 

    include('../includes/db_connect.php');

    $query = $db-> query("SELECT user_id, username FROM user WHERE username='$user'");


    echo 'bem vindo: $ não sei o que colocar aqui';


    ?>

database inserir a descrição da imagem aqui --login.php-- inserir a descrição da imagem aqui

--index.php-- This is what I want to happen, take the login username and show on a welcome screen.

inserir a descrição da imagem aqui

2 answers

1


In this case your index page should be:

 <?php 
 session_start();
 echo "bem vindo: ".$_SESSION['username'];
 ?>

Note that in the login file, in this excerpt:

$_SESSION['username'] = $row->username;

Is being assigned the result of SELECT the session variable.

Another detail, the while is unnecessary, since to assign the user id to the session variable we have this condition:

if($query->num_rows === 1)//se o resultado da query === 1...execute

then that stretch could be:

if($query->num_rows === 1)
{    $row = $query->fetch_object();
     $_SESSION['username'] = $row->username;       
     header('Location: admin/index.php');
     exit();
}else
{    echo 'Nada informado';
}

I think that’s it. I can’t remember if in index, you will need to log in, but PHP warns... Test if error warns, agent corrects.

  • in that passage $_SESSION['username'] = $row->username ; when I put the username in place of the Undefined variable user_id

  • Look, it’s $_SESSION['user_id'], why are you trying username? Oh though I saw an error here I will edit my answer...

  • I edited take a look, I was almost asleep when I answered this question..:)

  • yes I came to change for this but keeps giving Undefined variable that strange user_id shows right however username no

  • Look I did a test, here it worked perfectly. https://chat.stackexchange.com/rooms/61362/xxxxx get here

0

You executed this:

$query = $db-> query("SELECT user_id, username FROM user WHERE username='$user'");

echo 'bem vindo: $ não sei o que colocar aqui';

But it was missing to fetch the data to an object: $dado = $query->fetch_object();

Then you can use it in two ways.

echo 'bem vindo: '.$dados->username; (In your SQL, you are not searching for the user name in the Database. Do you have a named column there? If you have changed your SQL, and put $data->name)

or

echo "bem vindo: $dados->username";

Noticed the difference with double quotes?

  • how to change my SQL? I have a column called username where I store users' names.

  • but the username wouldn’t be your login? Example my username is: macondesmacaneiro, but my name is: Marcondes Doorknob.

  • i want to show login name juxtaposition.

  • ok, just like I showed in the example, you can display anything. Got it?

  • Your answer presents errors, consider reviewing it, and avoid being negative.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.