-1

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”

im trying to create a simple login and register page and i am getting these errors on my index .php page,why is my index undefined these are the errors i am getting

Notice: Undefined index: username in C:\xampp\htdocs\mysite\forum part two\index.php on line 7

Notice: Undefined index: password in C:\xampp\htdocs\mysite\forum part two\index.php on line 8

the codes on line 7 and 8 these respectively

$username = $_SESSION['username'];
$password = $_SESSION['password'];

this is my index page

<?php
//This will start a session
session_start();

$username = $_SESSION['username'];
$password = $_SESSION['password'];

//Check do we have username and password
if(!$username && !$password){
echo "Welcome Guest! <br> <a href=login.php>Login</a> | <a href=register.php>Register</a>";
}else{
echo "Welcome ".$username." (<a href=logout.php>Logout</a>)";
}
?> 
Community
  • 1
  • 1
theuserkaps
  • 300
  • 2
  • 6
  • 15

3 Answers3

2

You are getting those errors because your session has not been declared earlier,So you need to check if your session is declared earlier. you can use php empty or isset function

<?php
//This will start a session
session_start();
$username ='';$password ='';
if(!empty($_SESSION['username']))//check if it is defined
    $username = $_SESSION['username'];

if(!empty($_SESSION['password']))//check if it is defined
    $password = $_SESSION['password'];

//Check do we have username and password
if($username=='' && $password==''){
   echo "Welcome Guest! <br> <a href=login.php>Login</a> | <a href=register.php>Register</a>";
}else{
echo "Welcome ".$username." (<a href=logout.php>Logout</a>)";
}
?> 
Sibu
  • 4,609
  • 2
  • 26
  • 38
  • This will just give a "Notice: Undefined variable" instead if the session's index is not set, because `$username` and `$password` never get set if they don't exist in the session. – animuson Dec 05 '12 at 06:02
  • now this will throw an undefined variable errors on username and pasword on the if condition when they are not set. Declare them to be false first. – dakdad Dec 05 '12 at 06:02
  • @Sibu i tried the errors are going but its not registering – theuserkaps Dec 05 '12 at 06:17
  • @theuserkaps where are you declaring your session variable, if it is declared it will show username, otherwise guest details will be shown – Sibu Dec 05 '12 at 06:20
  • i figured it out i think i have another problem with the register page – theuserkaps Dec 05 '12 at 06:23
2

Check to see if the $_SESSION's store any values..

I'll give you an example:

  <?php
 //This will start a session
 session_start();

 if(isset($_SESSION['username']))
 {
     $username = $_SESSION['username'];
}else{
  $username = null;
}
var_dump($username);

// Would return NULL. SESSION IS NOT SET.
// if I add $_SESSION['username'] = "Phorce" session set, will return phorce.
?> 

Or: var_dump($_SESSION['username']);

Hope this helps :) It could be that $_SESSION['username'] isn't registering.

Phorce
  • 2,632
  • 13
  • 43
  • 76
  • This will just give a "Notice: Undefined variable" instead if the session's index is not set, because `$username` and `$password` never get set if they don't exist in the session. – animuson Dec 05 '12 at 06:04
1

Please check that there is value assigned to session variable before using it.

<?php
//This will start a session
session_start();

$username ="";
$password ="";
if(isset($_SESSION['username']))
$username = $_SESSION['username'];

if(isset($_SESSION['password ']))
$password = $_SESSION['password'];

//Check do we have username and password
if(!$username && !$password){
echo "Welcome Guest! <br> <a href=login.php>Login</a> | <a href=register.php>Register</a>";
}else{
echo "Welcome ".$username." (<a href=logout.php>Logout</a>)";
}
?> 
Uttam Kadam
  • 458
  • 1
  • 7
  • 20