Problem :
I want to print the user's name, to greet him, if he is logged in to the page. If you are 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 correct, it redirects me to the index.php !!! So far so good, 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 is started, I only get the name of the logged in user, but not the "Welcome + $username"...
Index.php code:
<?php
session_start();
?>
---- Follow the HTML -------
And he comes:
<h1 id="titulo"> <?php echo "Bienvenido ".isset($_SESSION["usuario"]) ? $_SESSION["usuario"] : "TIENDA X MAYOR";?></h1>
Okay, I'll explain again. What I want to do is that if the user is logged in, the H1 says "Welcome + the name of the user who logged in" .... if he is not logged in, I want it to say "Store X Mayor"
You can determine the state of the session using either a ternary:
And then you add it to your html.
I like to write the code without mixing, using variables:
I have executed a code similar to yours and it works for me:
login.php
index.php
From the error you get,
$usuario
it has nothing, and so undefined is assigned. Make avar_dump($usuario);
tentative comment on the redirect like thisheader("Location: index.php");
and see if it gives you any value or not.Try putting it in input without borders, and after that you style it with css