0

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

Okay, so I am trying to build my site and I am having a minor issue. I am getting errors like "Undefined index: id in C:\xampp\htdocs\index.php on line 18" and "Undefined index: submit in C:\xampp\htdocs\index.php on line 39"

I am certain it's a matter of syntax but I can't seem to find the right answer. Here is the php code in my index.php file on lines 16-18

session_start();

if($_SESSION['id'] && !isset($_COOKIE['logRemember']) && !$_SESSION['rememberMe'])

The code worked before when I was running Apache 2 in linux. Now I am running Xampp on windows and all of a sudden it doesn't want to work right.

Whoever answers this, please write it in stupid. I'm still learning php. :) Thanks!

Community
  • 1
  • 1

6 Answers6

2

No, it's not a syntax error. You are trying to access a key that does not exist. It seems that warnings were disabled on your previous server.

Check if the actual variable is set (using isset) before trying to access it:

if(
    isset($_SESSION['id']) &&
    !isset($_COOKIE['logRemember']) &&
    isset($_SESSION['rememberMe'] &&
    !$_SESSION['rememberMe']
)
alexn
  • 57,867
  • 14
  • 111
  • 145
0

You use isset() on the $_COOKIE variable, so you must know how that works for checking the existence of a variable. Just apply that to $_SESSION['id'] and so on and the errors will stop.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

In your first run, $_SESSION['id'] and $_SESSION['rememberMe'] won't be set. You have to check if they're set (just as you did with your cookie)

if(isset($_SESSION['id']) && !isset($_COOKIE['logRemember']) && !isset($_SESSION['rememberMe']))
Matheus Azevedo
  • 878
  • 7
  • 18
0

The reason it "worked" was because the Apache 2 server probably wasn't configured to display errors. Your issue is that $_SESSION['id'] isn't set. Rather, check that it is with

isset($_SESSION['id'])

Which won't throw an error if it isn't set.

SomeKittens
  • 38,868
  • 19
  • 114
  • 143
0

Check the directory where the session data is storred is writeable by apache. You can get the path by using session_save_path ();

refugene
  • 383
  • 2
  • 16
0

Replce line with following code

session_start();

if(isset($_SESSION['id']) && !isset($_COOKIE['logRemember']) && !isset($_SESSION['rememberMe']))
Lalit Jain
  • 138
  • 7