Problem :
I want to print the user's name, to greet him, if he has logged in on the page. If you have not logged in then I want the title to be the default title of the page.
Log in code:
<?php
include("conexion.php");
unset($_SESSION['error']);
if(!isset($_POST['usuario']) || !isset($_POST['contrasenia'])) {
}else{
$usuario = $_POST["usuario"];
$contrasenia = md5($_POST["contrasenia"]);
if ($resultado = mysqli_query($link, "SELECT * from Usuarios where usuario='" . $usuario . "' and contrasenia='" . $contrasenia . "'")) {
if (mysqli_num_rows($resultado) == 1) {
$row = mysqli_fetch_assoc($resultado);
if ($row['contrasenia'] == $contrasenia && $row['usuario'] == $usuario) {
session_start();
$_SESSION['usuario'] = $usuario;
header("Location: index.php");
}
} else {
unset($_SESSION['usuario']);
$_SESSION["error"] = "Usuario y/o contraseña incorrecta";
}
}
}
?>
As you can see, when I do the LogIn and the username and password are fine, it redirects me to index.php !!! Up here well, but in the index.php, in the title or label, if the session is NOT started I get this:
Notice: Undefined index: user in /var/www/html/PaginaFinal/php/index.php on line 24
And if it's started, I only get the name of the logged-in user, but not the "Welcome + $ username" ...
Code of the index.php:
<?php
session_start();
?>
---- Follow the HTML -------
And here comes:
<h1 id="titulo"> <?php echo "Bienvenido ".isset($_SESSION["usuario"]) ? $_SESSION["usuario"] : "TIENDA X MAYOR";?></h1>
I mean, I explain again. What I want to do is that if the user is logged in, H1 says "Welcome + the name of the user who logged in" .... if it is not logged in, I want it to say "Tienda X Mayor"