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
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!");

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.