How to do echo of a superglobal variable in HTML

0

I have a superglobal variable $ _SESSION with a value inside and I want to do an echo in an HTML page but nothing appears. My code is as follows:

<header class='main' id='h1'>
  <span class="right"><a href="layout.html">LogOut</a> </span> 
  <?php echo $_SESSION['login']; ?>
<h2>Quiz: crazy questions</h2>
</header>

The thing is that the echo does not run. Does anyone know how to fix it?

    
asked by UnaiLopez 12.11.2017 в 16:13
source

1 answer

2

At the bottom of your site you have to put the following sentence:

<?php session_start(); ?>
  

I clearly assume that the variable $_SESSION is assigning it from a login.

Your html code if it should be the same as you have it:

<header class='main' id='h1'>
  <span class="right"><a href="layout.html">LogOut</a> </span> 
  <?php echo $_SESSION['login']; ?>
<h2>Quiz: crazy questions</h2>
</header>

Example:

<?php session_start();
$_SESSION['login'] = "wilson";
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <header class='main' id='h1'>
      <span class="right"><a href="layout.html">LogOut</a> </span> 
      <?php echo $_SESSION['login']; ?>
    <h2>Quiz: crazy questions</h2>
    </header>
</body>
</html>

You can check that your variable $_SESSION exists with the following conditional:

if(!isset($_SESSION["login"]))  
 {  
    echo "No existe o no esta seteada la SESSION";
    die();  
 }else {
    echo "La SESSION login si existe $_SESSION['login']";
}

That conditional you should put it just after of session_start(); , always goes first of all session_start();

    
answered by 12.11.2017 / 16:20
source