XAMPP Undefined Index - $_SESSION, $_POST, $_GET, $_REQUEST

Problems with the Windows version of XAMPP, questions, comments, and anything related.

XAMPP Undefined Index - $_SESSION, $_POST, $_GET, $_REQUEST

Postby yodrica » 29. January 2012 01:36

Hello all, this is my second time installing XAMPP onto my Windows XP OS, and I didn't have this problem before.

I'm trying to make a Google Chrome tab to make users' experiences with Chrome more personalized.

I just started it, and so I decided to start functions.php:

Code: Select all
<?php
if(isset($_SESSION["username"])){
$user=$_SESSION["username"];
}
else{
$user="";
}
?>


Here is the error that I am getting:
Notice: Use of undefined constant username - assumed 'username' in C:\xampp\htdocs\chrome\functions.php on line 2

I've searched around, and have found no answers. Again, I did not have this problem with XAMPP before, at least, I don't think I did.

I tried configuring php.ini and set error reporting to just E_ALL just to see if PHP would "shut up". No luck.

Could you please help me understand why things such as $_SESSION, $_COOKIE, $_GET, $_POST, $_SERVER, and $_REQUEST are not working and why they are thought to be undefined indexes?

Thank you. Your answers are appreciated.
-Ricky
yodrica
 
Posts: 1
Joined: 29. January 2012 01:29
Operating System: Windows XP

Re: XAMPP Undefined Index - $_SESSION, $_POST, $_GET, $_REQU

Postby Altrea » 29. January 2012 09:51

Hi Ricky,

  1. Your sourcecode doesn't fit to any of the two error messages
  2. I don't believe you didn't find anything about that two error messages. They are very common.

To prevent undefined index messages, proof your array keys with isset() or empty() before you use them. You have done this in your posted code.
Code: Select all
//wrong
$key = $_SESSION['key'];

//correct
if( isset( $_SESSION['key'] ) ) {
    $key = $_SESSION['key'];
}


To prevent Use of undefined constant messages, put your array keys into single or double quotes. You have done that too in your posted sourcecode.
Code: Select all
//wrong
echo $_SESSION[key];

//correct
echo $_SESSION['key'];

So, nothing more to do.

best wishes,
Altrea
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 136 guests