Jump to content

Undefined index: username HELP NEWBIE


justineaguas

Recommended Posts

I am trying a simple login/logout for my website. It works well with checking if the username exists in the database to logging in. I used $_SESSION['username']=$myusername; However, when I log-out, then I go to a page with this code

 

<?php

session_start();

if($_SESSION['username']) //line5
echo "Welcome, ".$_SESSION['username']."!<br /><a href='logout.php'>Logout.</a>";
else
die ("You must be logged in!");

?>

 

It gives me an error. It says Notice: Undefined index: username in C:\wamp\www\datbas\member.php on line 5

 

I just want to know how to fix this. Thank you.

Link to comment
https://forums.phpfreaks.com/topic/180964-undefined-index-username-help-newbie/
Share on other sites

Notices are extremely low level errors. It's telling you that there isn't an item in the $_SESSION array called 'username' and you are trying to check it's value. The solution is to first check it exists before checking it's value.

 

session_start();

if(isset($_SESSION['username'])) //line5
   echo "Welcome, ".$_SESSION['username']."!<br /><a href='logout.php'>Logout.</a>";
else
   die ("You must be logged in!");

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.