Hi guys,

I am new to php just started learning today but I am having very little success so far and need some help.

I am running Microsoft IIS 5.1 on WinXP platform with php 4.3.7 installed. I am sure php is installed correctly because when I run <? phpinfo(); ?> all relevant php info is returned correctly.

I am starting with the basics first, and I created a php file with the following code:

<?php
echo $_SERVER['DOCUMENT_ROOT'];
?>

and I am returned with the error message:

Notice: Undefined index: DOCUMENT_ROOT in C:\Documents and Settings\PC\My Documents\WWW\home\index.php on line 2

I know that DOCUMENT_ROOT should be a pre-defined php variable, but why doesn't it work? 🙁

    What does your PHPInfo look like?

    Are registered_globals on?

      Originally posted by Kudose
      Are registered_globals on?

      Hi Kudose, I think you are right on target, my registered_globals are OFF.

      How can I turn them on?

      Also, any other php settings I might need to change?

        Just another question..

        I used the following code in a php file called index.php

        <?php
        if ($link == page2){
        $page="pages/page2.html";
        }
        elseif($link == page3){
        $page="pages/page3.html";
        }
        else{
        $page="pages/page1.html";
        }
        ?>

        <html>
        <head><title></title></head>
        <body>
        <a href="index.php?link=page1"> Page 1 </a><br>
        <a href="index.php?link=page2"> Page 2 </a><br>
        <a href="index.php?link=page3"> Page 3 </a><br>

        <?php include($page); ?>
        </body>
        </html>

        and when i tried to access index.php like so

        index.php?link=page1

        It gives me these errors:

        Notice: Use of undefined constant page2 - assumed 'page2' in C:\Documents and Settings\PC\My Documents\WWW\home\index.php on line 2

        Notice: Use of undefined constant page3 - assumed 'page3' in C:\Documents and Settings\PC\My Documents\WWW\home\index.php on line 5

        And when i access index.php without passing the variable 'link', i get even more errors like so:

        Notice: Undefined variable: link in C:\Documents and Settings\Paul Chang\My Documents\WWW\home\index.php on line 2

        Notice: Use of undefined constant page2 - assumed 'page2' in C:\Documents and Settings\Paul Chang\My Documents\WWW\home\index.php on line 2

        Notice: Undefined variable: link in C:\Documents and Settings\Paul Chang\My Documents\WWW\home\index.php on line 5

        Notice: Use of undefined constant page3 - assumed 'page3' in C:\Documents and Settings\Paul Chang\My Documents\WWW\home\index.php on line 5

        I can understand where the error is coming from, but I have very little idea how to fix it. 🙁

        Is it absolutely necessary to use index.php?link=page1 to access page 1? Can't I just access index.php and that's all?

        By the way, I've turned on globals now.

          You need to use quotes when comparing strings...

          if ($link == 'page2'){ 
          $page="pages/page2.html"; 
          } 
          elseif($link == 'page3'){ 
          $page="pages/page3.html"; 
          } 
          else{ 
          $page="pages/page1.html"; 
          } 
          

          Save Kudose some time 😉

            From the code you just used, it may be faster to use [man]switch/man instead of an if tree.

            But as ZibingsCoder pointed out, you do need quotes when checking a string in either a switch OR if statement.

              Originally posted by ZibingsCoder
              You need to use quotes when comparing strings...

              Thank you ZibingsCoder. So it's just like C/C++? Now I understand it. The code above was actually from a website example I found earlier, I guess whoever wrote the example omitted the quotation marks... :rolleyes:

              Now it comes to this other rather less important but annoying problem. If I access just using index.php, I still get two errors.

              eg. http://localhost/index.php

              Notice: Undefined variable: link in C:\Documents and Settings\PC\My Documents\WWW\home\index.php on line 2

              Notice: Undefined variable: link in C:\Documents and Settings\PC\My Documents\WWW\home\index.php on line 5

              but when i access it like http://localhost/index.php?link=page1

              It would run fine. Any idea how i can do it without passing the variable name? My god I am such a newbie... I feel awfully embarassed here.

                That's actually a problem with the amount of errors you're showing. Those are not "errors" persay, just notices, as they say. They are there to tell you when you've done something wrong. The preferable thing to do with this page would be something more along the lines of this...

                if ( isset($link) )
                {
                    switch ( $link )
                    {
                        case "page2":
                            $page = "pages/page2.html";
                            break;
                        case "page3":
                            $page = "pages/page3.html";
                            break;
                        default:
                            $page = "pages/page1.html";
                            break;
                    }
                }
                
                else
                {
                    $page = "pages/page1.html";
                }
                

                Give that a try, and you shouldn't get ANY notices...at least in theory 😉

                  Oh wow! I see. You guys are obviously excellent in PHP. I am sure you guy will see more of me here in the future.

                  Is there a way to turn off Notice or Warning messages so they do not appear? Like how SQL some warnings or messages can be turned off.

                  Now my original problem is back again...

                  With the code

                  <?php 
                  echo $_SERVER['DOCUMENT_ROOT']; 
                  ?>
                  

                  I am still getting the:

                  Notice: Undefined index: DOCUMENT_ROOT in C:\Documents and Settings\PC\My Documents\WWW\home\index.php on line 2

                  Registered_globals are turned On. What could be wrong?

                    Here's a question that may help us out...

                    Are you running PHP as an ISAPI module on IIS? If not, DOCUMENT_ROOT isn't supported.

                      I strongly recommend you do not turn them off. Having them on forces you to write good code.

                      However, if you must turn them off, take a look at the [man]error_reporting[/man] function.

                        To be honest, I do not have a clue.

                        I just understand that ISAPI has something to do with CGI but what it is really I have no idea.

                        Is there a way to find out?

                        I guess whether I use DOCUMENT_ROOT or not isn't very important 🙁

                        Hi BuzzLY, you are right, I think I will leave them on for now. Thanks for the tip 🙂

                          Will be honest here, not very familiar with IIS after a certain point. I -believe- that it's not running as an ISAPI module if you're using the CGI...but, don't quote me on that. Someone with more IIS experience wanna give us a hand?

                            Write a Reply...