-1

I am programming in PHP.

I want to do a page that if the user didn't signed in, the system will say "Hello Guest" and if he signed in, the system will say "Hello $username".

It's working, however, when I entered the system for the first time I receive a error undefined $Username. After I refresh the page or going to another page it's working good - the problem is in the first time.

The $username received in the login page and there is session_start();.

This code is from the page I am trying to do the Hello Username:

<?php
    if (!isset($_SESSION))
        session_start();

    if ($_SESSION["Username"] == "")
        $_SESSION["Username"] = "Guest";    
?>

Need help

Yuval
  • 25
  • 1
  • 5
  • 1
    the code you posted doesn't support the question/error. `undefined $Username` unless you meant (minus the $ sign) `undefined Username` – Funk Forty Niner May 16 '16 at 12:12

2 Answers2

1

you should isset()

<?php
if (!isset($_SESSION))
    session_start();

if (!isset($_SESSION["Username"]))
    $_SESSION["Username"] = "Guest";    
?>
Naresh Kumar
  • 561
  • 2
  • 15
0

Wherever you have used $username you have to check if it exist using

if(isset($username)){

}

If not then you have to set it.

Amit Ray
  • 3,445
  • 2
  • 19
  • 35