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';
?>
--index.php-- This is what I want to happen, take the login username and show on a welcome screen.
in that passage
$_SESSION['username'] = $row->username ;
when I put the username in place of the Undefined variable user_id– Marcelo T. Cortes
Look, it’s
$_SESSION['user_id']
, why are you tryingusername
? Oh though I saw an error here I will edit my answer...– MagicHat
I edited take a look, I was almost asleep when I answered this question..:)
– MagicHat
yes I came to change for this but keeps giving Undefined variable that strange user_id shows right however username no
– Marcelo T. Cortes
Look I did a test, here it worked perfectly. https://chat.stackexchange.com/rooms/61362/xxxxx get here
– MagicHat