Page 1 of 1

Undefined Index Issue - Error

Posted: Thu Mar 02, 2006 9:07 pm
by JMichaels22
All,

I am trying to create the following short variable name:

$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];

When I try to do this I get the following error and im not too sure why:

Notice: Undefined index: DOCUMENT_ROOT in C:\Inetpub\wwwroot\processorder.php on line 68



All the tags are in place and correct and when I comment out that piece of code everything else will work. Any ideas on what I am missing, seems like I am missing something obvious, possibly PHP setup?

Thanks

Posted: Thu Mar 02, 2006 9:20 pm
by neophyte
I've never encountered this problem before but I'm gathering from the manual that this variable isn't always available. Here is a tip in the suggestions on how to make it your self:

Code: Select all

if(!isset($_SERVER["DOCUMENT_ROOT")){
$_SERVER["DOCUMENT_ROOT"]=substr($_SERVER['SCRIPT_FILENAME'] , 0 , -strlen($_SERVER['PHP_SELF'])+1 );
 }

Posted: Fri Mar 03, 2006 6:32 am
by shiznatix
isset will still give you a undefined index error. instead of !isset() use empty()

Posted: Fri Mar 03, 2006 8:35 am
by feyd
isset() does not give undefined index errors.

Posted: Fri Mar 03, 2006 8:54 am
by shiznatix
feyd wrote:isset() does not give undefined index errors.
well i'll be damned. i thought it gave undefined index errors and undeclared variable errors. well i guess you learn somtin new every day eh?

How does this code work?

Posted: Fri Mar 03, 2006 9:32 am
by JMichaels22
What exactly does this piece of code do? I understand that isset is testing to see if the variable is empty, then if it is it sets the variable. What I dont get is the last part of this code, what is it doing?

Thanks for the replies and for being patient =)

-Jassen

Posted: Fri Mar 03, 2006 9:50 am
by feyd
attempts to extract a substring from another variable.

Posted: Fri Mar 03, 2006 11:54 am
by JMichaels22
Thanks that did do the trick!

Im still confused as to what this if statement does, but i'll get it sooner or later. =)

Posted: Fri Mar 03, 2006 12:57 pm
by RobertGonzalez
The if statement is checking to see if there is a value set for $_SERVER["DOCUMENT_ROOT"]. If there isn't one, then it makes one for you based on values from a few other $_SERVER vars.

Code: Select all

<?php
if(!isset($_SERVER["DOCUMENT_ROOT"])) {
    // If $_SERVER["DOCUMENT_ROOT"] does not have a set value...
    // make one using this scripts file name
    $_SERVER["DOCUMENT_ROOT"]=substr($_SERVER['SCRIPT_FILENAME'] , 0 , -strlen($_SERVER['PHP_SELF'])+1 );
} 
?>