-2

im still new to php , and i got this error .. pls help me out here , thx .

Notice: Undefined index: sid in C:\wamp\www\test\inc\template.php on line 205

so , here is my code :

<?php
}
elseif($_SESSION['sid'] != '')
{
?>
Zeko
  • 15
  • 1
  • 2
  • 1
    `$_SESSION['sid']` see the index, 'sid' ? Well, is undefined, so either you don't have a 'sid' index set, or you don't have the whole $_SESSION set. Did you use `session_start()` ? – Damien Pirsy Jan 08 '14 at 15:10

2 Answers2

1

$_SESSION is an array so you set it like so $_SESSION['sid'] = 'some sid'; if you try to use $_SESSION['sid'] without setting it then PHP throws a notice.

If you want to avoid the notice then one of these options will work:

Option 1

<?php
// check if the variable isset before using it
if(isset($_SESSION['sid']) && $_SESSION['sid'] != '')
{
    // do something
}
?>

Option 2

<?php
// turn off error reporting
// NEVER DO THIS IN A DEVELOPMENT ENVIRONMENT
// error_reporting(E_ALL) should be used in development
error_reporting(0);

if($_SESSION['sid'] != '')
{
    // do something
}
?>
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
0

If you want to see if a session variable is set (or any variable for that matter), use isset(). If you just want to see if it is empty, use empty() after using isset()

elseif(isset($_SESSION['sid']) && !empty($_SESSION['sid']))
John Conde
  • 217,595
  • 99
  • 455
  • 496