Print session name (if it exists) in h1 tag? PHP

1

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"

    
asked by Nacho Zve De La Torre 08.10.2018 в 22:20
source

3 answers

3

You can determine the status of the session by using a ternary:

$usuario=!(empty($_SESSION["usuario"])) ? $_SESSION["usuario"] : "Tienda al Por Mayor";

And then you add it to your html.

I like to write the code without mixing, using variables:

<?php
  $usuario = $_SESSION["usuario"];
  $html="<h1 id=\"titulo\">Bienvenido $usuario</h1>";
  echo $html;
?>
    
answered by 08.10.2018 / 22:39
source
0

I have executed a code similar to yours and it works for me:

login.php

<?php
  session_start();
  $_SESSION["usuario"] = "Usuario2";
  header("Location: index.php");
?>

index.php

<?php
  session_start();
?>
<h1 id="titulo"> <?php echo "Bienvenido ".isset($_SESSION["usuario"]) ? 
$_SESSION["usuario"] : "TIENDA X MAYOR";?></h1> 

Because of the error it gives you, $usuario has nothing, and then undefined is assigned. Make a var_dump($usuario); and temporarily comment the redirect in this way header("Location: index.php"); and see if it gives you some value or not.

    
answered by 08.10.2018 в 22:51
0

Try it by putting it in in input without borders, and after that you give it style with css

<?php

    session_start();

    if($_SESSION["usuario"] == TRUE) {

    if (isset($_SESSION["usuario"])) {
        //asignar a variable
    $usernameSesion = $_SESSION["usuario"];
        //asegurar que no tenga "", <, > o &
    $username = htmlspecialchars($usernameSesion);}}


?>

<input type="text" style="width: 50%; border: none;" <?php if( $username <> "") { ?> value="Bienvenido  <?php echo $username; ?>  a TIENDA X MAYOR" <?php }else{ ?> value="Bienvenido a TIENDA X MAYOR" <?php } ?>>
    
answered by 08.10.2018 в 22:55