I am trying to make a login that allows me to greet who logged in, i.e. saying "Hello [person who logged in]!"
I think I am close, using
authenticate.php
<?php
session_start();
require_once('database.php');
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM users WHERE username='".$username."' AND password='".$password."'";
$run = mysql_query($sql);
$row = mysql_fetch_array($run);
if (mysql_num_rows($run) ==1)
{
$_SESSION['logged_in'] = true;
$_SESSION['id'] = $row['id'];
$_SESSION['name'] = $row['name'];
header("Location: index.php");
}
else
{
header("Location: login.php");
}
?>
and login.php
<?php
?>
<form action="authenticate.php" method="post">
Username: <input type="text" name="username" /><br />
Password: <input type="password" name="password" /><br />
<input type="submit" value="Login" />
</form>
But in index.php, I am trying to show Hello [login]:
<?php
session_start();
if($_SESSION['logged_in'])
{
?>
<h3>Hello <?= $_SESSION['name'] ?> (<?= $_SESSION['id'] ?>), </h3>
<h3>You are logged in.</h3>
<a href="logout.php">logout</a>
<?php
}
else
{
?>
<h3>You are not logged in.</h3>
<a href="login.php">login</a>
<?php
}
?>
It will show: Hello ( )
Are my parenthesis wrong in index.php? I thank you in advance!