-1
<?php
session_start();
error_reporting(E_ALL); 
$id = $_SESSION["id"];  
$name = $_SESSION["name"];
$surname = $_SESSION["surname"];

if (!empty($id)) {
    echo "<div id=\"user\">";
    echo "<h4>Utente: $name $surname | <a href=\"http:/...\">Logout</a></h4>";
    echo "</div>";
}
?>

Problem:

Notice: Undefined index: id in... on line 4
Notice: Undefined index: name in ... on line 5
Notice: Undefined index: surname in ... on line 6

So I try:

<?php
 session_start();
error_reporting(E_ALL);
if (!empty($_SESSION["id"])) {
    echo "<div id=\"user\">";
    echo "<h4>Utente: $_SESSION["name"] $_SESSION["surname"] | <a href=\"http:/...\">Logout</a></h4>";
    echo "</div>";
}?>

But:

Parse error: syntax error, unexpected '"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in ... on line 7

Why?

Charles
  • 50,943
  • 13
  • 104
  • 142
user2998986
  • 15
  • 2
  • 4
  • 5
    This question appears to be off-topic because it is about various errors, most [already asked on this website](http://stackoverflow.com/q/4261133/871050), as well as syntax errors. – Madara's Ghost Nov 17 '13 at 15:17

3 Answers3

3

The error message says it all: this means that those variables are not set (i.e. do not exist). You need to check if they exist before trying to use them:

$id      = ($_SESSION["id"])      ?: null;  
$name    = ($_SESSION["name"])    ?: null;
$surname = ($_SESSION["surname"]) ?: null;

If you're running a version earlier than PHP 5.3 you would need to use the longer syntax:

$id      = (isset($_SESSION["id"]))      ? $_SESSION["id"]      : null;  
$name    = (isset($_SESSION["name"]))    ? $_SESSION["name"]    : null;
$surname = (isset($_SESSION["surname"])) ? $_SESSION["surname"] : null;
John Conde
  • 217,595
  • 99
  • 455
  • 496
0

Make use of isset() construct

<?php
session_start();
error_reporting(E_ALL);
if(isset($_SESSION["id"]))
{
$id = $_SESSION["id"];
}
if(isset($_SESSION["name"]))
{
$name = $_SESSION["name"];
}
if(isset($_SESSION["surname"]))
{
$surname = $_SESSION["surname"];
}
if (!empty($id)) {
    echo "<div id=\"user\">";
    echo "<h4>Utente: $name $surname | <a href=\"http:/...\">Logout</a></h4>";
    echo "</div>";
}
?>
zzlalani
  • 22,960
  • 16
  • 44
  • 73
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
0

For the Parse error: syntax error, unexpected, You can use

echo "<h4>Utente: ".$_SESSION["name"] .$_SESSION["surname"] ."| <a href=\"http:/...\">Logout</a></h4>";

instead of

echo "<h4>Utente: $_SESSION["name"] $_SESSION["surname"] | <a href=\"http:/...\">Logout</a></h4>";

Updated Code:

    <?php
        session_start();
        error_reporting(E_ALL);
        if (!empty($_SESSION["id"]) && isset($_SESSION["id"])) {
            echo "<div id=\"user\">";
              echo "<h4>Utente: ".$_SESSION["name"] .$_SESSION["surname"] ."| <a href=\"http:/...\">Logout</a></h4>";
            echo "</div>";
        }

    ?>
zzlalani
  • 22,960
  • 16
  • 44
  • 73
Krish R
  • 22,583
  • 7
  • 50
  • 59