Question

PHP Newbie - PHP Notice: Undefined index: DOC_ROOT

Asked by: globalwm

I installed PHP and have the phpinfo() working just fine. I'm working with a text book example that writes orders to a text file and I'm getting this error:

PHP Notice: Undefined index: DOC_ROOT in c:\Inetpub\wwwroot\PHP_Stuff\chapter01\processorder.php on line 8 PHP Warning: fopen(/Orders/orders.txt) [function.fopen]: failed to create stream: No such file or directory in c:\Inetpub\wwwroot\PHP_Stuff\chapter01\processorder.php on line 63 PHP Warning: flock(): supplied argument is not a valid stream resource in c:\Inetpub\wwwroot\PHP_Stuff\chapter01\processorder.php on line 66

The code:

Line 8:   $DOCUMENT_ROOT = $HTTP_SERVER_VARS['DOC_ROOT'];
Line 63:  $fp = fopen("$DOCUMENT_ROOT/Orders/orders.txt", 'a');

I and have setup:

In php.ini:  doc_root = "c:/Inetpub/wwwroot/PHP_Stuff"
(This is the same root as my website (IIS5) too)

The orders.txt file is located:

C:\Inetpub\wwwroot\PHP_Stuff\Orders\orders.txt

Orders.txt file has proper permissions too...

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2003-04-21 at 16:00:41ID20591770
Tags

undefined

,

php

,

index

Topic

PHP for Windows

Participating Experts
4
Points
300
Comments
24

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. no php.ini
    I use phpinfo it tell me that php.ini is in /tec but when i go to /etc no php.ini I use commnd find but not found . It mean that i not have php.ini or not Thank
  2. SMTP settings in php.ini file not showing up on a <?php p…
    I'm developing a web form for our IT department and need assistance with the PHP side. When I submit the form, it goes to a confirmation page for the EU to review. When that last "Submit" button is clicked to mail the form i get the following error... Warning: mail(...
  3. php.ini is being ignored
    I've spent the last few days building a Linux server. I've installed OpenSSL, OpenVPN, Apache 2.2.4, PHP 5.2.1, and MySQL 5.0.27. I had to build OpenSSL, OpenVPN, Apache, and PHP from source. PHP was built as a DSO. Everything seemed to be working fine. However, late last nig...
  4. PHP: fOpen url problem
    When I include this line in my php page, $fp = fopen ("http://www.yahoo.com","r"); It would take a long long time to load the page. If I remove this line, it will loads just real fast. There's no error, warning or anything. Does anyone know what could be...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: RQuadlingPosted on 2003-04-22 at 02:51:35ID: 8372067

Try ...

<?php phpingfo(); ?>

to see what variables are supplied by your server.

You will need to confirm that this variable is present.

e.g.

if (!isset($_SERVER['DOC_ROOT']))
 {
// Use some other variable ...
 }
else
 {
 $sDR = $_SERVER['DOC_ROOT'];
 }

Richard.

 

by: RQuadlingPosted on 2003-04-22 at 04:08:38ID: 8372340

Oh.

And I REALLY meant to type ...

<?php phpinfo(); >?

fingggggers slipped on the g.

Though ph ping fo sounds a LOT better!

<grin>

Richard.

 

by: globalwmPosted on 2003-04-22 at 07:28:56ID: 8373558

On phpinfo, I see this:

doc_root c:/Inetpub/wwwroot/PHP_Stuff

So, why doesn't $fp = fopen("$DOCUMENT_ROOT/Orders/orders.txt", 'a');  work if the file is located: C:\Inetpub\wwwroot\PHP_Stuff\Orders\orders.txt

??





ps. I liked the phping...  :)

 

by: RQuadlingPosted on 2003-04-23 at 02:13:49ID: 8378733

Can you cut and paste the entire line showing the doc_root variable?

Is it ...

_SERVER["DOCUMENT_ROOT"]

Also, from the PHP Manual ...

doc_root string
PHP's "root directory" on the server. Only used if non-empty. If PHP is configured with safe mode, no files outside this directory are served. If PHP was not compiled with FORCE_REDIRECT, you SHOULD set doc_root if you are running php as a CGI under any web server (other than IIS) The alternative is to use the cgi.force_redirect configuration below.



The following steps are optional.


Edit your new php.ini file. If you plan to use OmniHTTPd, do not follow the next step. Set the doc_root to point to your webservers document_root. For example:


doc_root = c:\inetpub        // for IIS/PWS

doc_root = c:\apache\htdocs // for Apache
 



Choose which extensions you would like to load when PHP starts. See the section about Windows extensions, about how to set up one, and what is already built in. Note that on a new installation it is advisable to first get PHP working and tested without any extensions before enabling them in php.ini.

On PWS and IIS, you can set the browscap configuration setting to point to: c:\windows\system\inetsrv\browscap.ini on Windows 9x/Me, c:\winnt\system32\inetsrv\browscap.ini on NT/2000, and c:\windows\system32\inetsrv\browscap.ini on XP.





Installed as CGI binary
Possible attacks
Using PHP as a CGI binary is an option for setups that for some reason do not wish to integrate PHP as a module into server software (like Apache), or will use PHP with different kinds of CGI wrappers to create safe chroot and setuid environments for scripts. This setup usually involves installing executable PHP binary to the web server cgi-bin directory. CERT advisory CA-96.11 recommends against placing any interpreters into cgi-bin. Even if the PHP binary can be used as a standalone interpreter, PHP is designed to prevent the attacks this setup makes possible:


Accessing system files: http://my.host/cgi-bin/php?/etc/passwd

The query information in a url after the question mark (?) is passed as command line arguments to the interpreter by the CGI interface. Usually interpreters open and execute the file specified as the first argument on the command line.

When invoked as a CGI binary, PHP refuses to interpret the command line arguments.

Accessing any web document on server: http://my.host/cgi-bin/php/secret/doc.html

The path information part of the url after the PHP binary name, /secret/doc.html is conventionally used to specify the name of the file to be opened and interpreted by the CGI program. Usually some web server configuration directives (Apache: Action) are used to redirect requests to documents like http://my.host/secret/script.php to the PHP interpreter. With this setup, the web server first checks the access permissions to the directory /secret, and after that creates the redirected request http://my.host/cgi-bin/php/secret/script.php. Unfortunately, if the request is originally given in this form, no access checks are made by web server for file /secret/script.php, but only for the /cgi-bin/php file. This way any user able to access /cgi-bin/php is able to access any protected document on the web server.

In PHP, compile-time configuration option --enable-force-cgi-redirect and runtime configuration directives doc_root and user_dir can be used to prevent this attack, if the server document tree has any directories with access restrictions. See below for full the explanation of the different combinations.




From this, the doc_root setting in the PHP ini file is NOT the one that you get from $_SERVER (or $HTTP_SERVER_VARS on older versions of PHP).

e.g.

On my system, doc_root is blank, but $_SERVER['DOCUMENT_ROOT'] is set.

NOTE: DOCUMENT_ROOT, not doc_root. Difference.

Richard.

 

by: globalwmPosted on 2003-04-23 at 07:49:39ID: 8380663

Hi, still working thru your comments but I wanted to clarify something.

You mentioned the PHP.ini:  doc_root = c:\inetpub    // for IIS/PWS

My docs (I have several sites) are located at:

c:\inetpub\wwwroot\[site folder]

In this case:

c:\inetpub\wwwroot\PHP_Stuff


???

 

by: ni2kPosted on 2003-04-23 at 08:39:29ID: 8381084

if
$doc_root=c:/Inetpub/wwwroot/PHP_Stuff
and
you want to open:
$doc_root\Orders\orders.txt

don't mix slash and backslash!!

try
str_replace("/","\\",$doc_root)\Orders\orders.txt

 

by: RQuadlingPosted on 2003-04-24 at 01:12:14ID: 8386801

DON'T USE DOC_ROOT!!!!

doc_root is an internal setting to control where files are allowed to be used when in safe mode.

PLEASE use $_SERVER['DOCUMENT_ROOT']

Richard.

 

by: RQuadlingPosted on 2003-04-24 at 01:15:14ID: 8386816

If you are not sure what I mean ...

<?php
error_reporting(E_ALL);

echo "This is what you are trying to get from doc_root : {$_SERVER['doc_root']}\n";
echo "This is what you are trying to get from doc_root : {$HTTP_SERVER_VARS['DOC_ROOT']}\n";

echo "This is what you are REALLY looking for : {$HTTP_SERVER_VARS['DOCUMENT_ROOT']} or {$_SERVER['DOCUMENT_ROOT']}\n";


$_SERVER is for V4.1+ of PHP and is now the preferred method.

$HTTP_SERVER_VARS is the old method and may no longer exist in V5.

Richard.

 

by: jimmePosted on 2003-04-24 at 01:18:16ID: 8386824

doc_root is a variable defined in php.ini, I guess you cannot access it through the script.
you can access the $_SERVER['DOCUMENT_ROOT'] or $HTTP_SERVER_VARS['DOCUMENT_ROOT'] which would be 'c:\\inetpub\\wwwroot'.

so that would work for you:
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
$fp = fopen("$DOCUMENT_ROOT/PHP_Stuff/Orders/orders.txt", 'a');

in fact, you don't even need to know the DOCUMENT_ROOT.
"/PHP_Stuff/Orders/orders.txt" would also do.

 

by: RQuadlingPosted on 2003-04-24 at 01:25:09ID: 8386846

jimme. Only repeating for the gazillionth time!!!!

 

by: jimmePosted on 2003-04-24 at 03:11:10ID: 8387245

sorry. 2 dumb 2 read ;-)

 

by: RQuadlingPosted on 2003-04-24 at 03:20:52ID: 8387284

But reinforcement is always a good thing!

 

by: globalwmPosted on 2003-04-24 at 08:39:12ID: 8389532

Ok, I see. I got some results now to report back.

I tried doing:

 $fp = fopen("$DOCUMENT_ROOT/Orders/orders.txt", 'a');

and

 $fp = fopen("$DOCUMENT_ROOT/PHP_Stuff/Orders/orders.txt", 'a');

with $DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT']; and

$DOCUMENT_ROOT = $HTTP_SERVER_VARS['DOCUMENT_ROOT'];

still got the error. So I just hardcoded the stupid path:

$fp = fopen("c:/Inetpub/wwwroot/PHP_Stuff/Orders/orders.txt", 'a');

and I got it to write the order info to the file. :)

I'm still getting this msg though:

PHP Notice: Undefined index: DOCUMENT_ROOT in c:\Inetpub\wwwroot\PHP_Stuff\chapter01\vieworders.php on line 3

Line 3 = DOCUMENT_ROOT = $HTTP_SERVER_VARS['DOCUMENT_ROOT'];


???


 

by: jimmePosted on 2003-04-24 at 14:35:40ID: 8391424

If your Line3 in your code matches the one you posted, i got the error.

you forgot the $ :
Line 3 = $DOCUMENT_ROOT = $HTTP_SERVER_VARS['DOCUMENT_ROOT'];

the result is that your $DOCUMENT_ROOT variable won't be defined.

Maybe you should just try to print your $DOCUMENT_ROOT out to see if it is set correctly:

$DOCUMENT_ROOT = $HTTP_SERVER_VARS['DOCUMENT_ROOT'];
echo $DOCUMENT_ROOT;

check also phpinfo() for the system variable.
(search for DOCUMENT_ROOT in the page printed out by phpinfo())

by the way, which version of php do you have?

 

by: globalwmPosted on 2003-04-24 at 15:46:38ID: 8391740

Yeah, it was a cut&paste error - my code has:

 $DOCUMENT_ROOT = $HTTP_SERVER_VARS['DOCUMENT_ROOT'];

When I echo the value with:

<?php
  //create short variable name
  $DOCUMENT_ROOT = $HTTP_SERVER_VARS['DOCUMENT_ROOT'];

?>
<html>
<head>
  <title>Holly's Auto Parts - Customer Orders</title>
</head>
<body>
 echo $DOCUMENT_ROOT;
<h1>Holly's Auto Parts</h1>
<h2>Customer Orders</h2>

I just see (literally):

echo $DOCUMENT_ROOT;  on the webpage.  :0





I don't have  $DOCUMENT_ROOT in my phpinfo() - just DOC_ROOT as mentioned above. (?)

 

by: RQuadlingPosted on 2003-04-25 at 02:14:12ID: 8393808

Can you cut and paste the ENTIRE page of results from phpinfo(); please.

You should NOT be looking in the PHP Core section (the first configuration part), but in the PHP Variables section (the last configuration part) for DOCUMENT_ROOT or whatever your server says the document root is.

DOCUMENT_ROOT is what Sambar Server and Apache call it. IIS may decide to call it something totally different. Why? Because Microsoft do not know how to spell C O M P A T I B I L I T Y. (And I had to check that twice otherwise the point would not be so important!).

Mine reads ...

_SERVER["DOCUMENT_ROOT"] /home/xxxxxxxx/public_html

So, $_SERVER['DOCUMENT_ROOT'] === '/home/xxxxxxxx/public_html'

Richard.

 

by: RQuadlingPosted on 2003-04-25 at 02:16:23ID: 8393817

If you have ...

$array['indexname']

and you get 'Undefined index', then the indexname you are using does NOT exist for the array.

So, your indexname may be case sensistive, or not set or mis-spelled.

Richard.

 

by: globalwmPosted on 2003-04-25 at 08:52:51ID: 8396228

Here's my PHPINFO page archived at:

http://globalwm.net/exchange/phpinfo.htm

 

by: RQuadlingPosted on 2003-04-25 at 09:04:25ID: 8396308

Right.

Your webserver is not supplying a document root variable.

Instead you will need to use ...

$DOCUMENT_ROOT = substr($_SERVER['PATH_TRANSLATED'],0,strlen($_SERVER['SCRIPT_NAME']) * - 1);

instead.

NOTE: $_SERVER['DOCUMENT_ROOT'] SHOULD be present and is in Apache and Sambar Server.

Richard.

 

by: RQuadlingPosted on 2003-04-25 at 09:07:44ID: 8396326

Just to explain ...

$DOCUMENT_ROOT = substr($_SERVER['PATH_TRANSLATED'],0,strlen($_SERVER['SCRIPT_NAME']) * - 1);

The substr() function is going to return a part of $_SERVER['PATH_TRANSLATED'].

The part starts at the beginning of the string (the 0 in the substr()) and stops x number of characters short.

x is the length of the $_SERVER['SCRIPT_NAME'] (I suppose you could also use $PHP_SELF). The * - 1 provides a negative length and this, to substr() means drop this number of letters from the end of the string.


NOTE the line at the end  ...

$rest = substr("abcdef", 0, -1);  // returns "abcde"

From the PHP manual.

substr
(PHP 3, PHP 4 )

substr -- Return part of a string
Description
string substr ( string string, int start [, int length])


substr() returns the portion of string specified by the start and length parameters.

If start is non-negative, the returned string will start at the start'th position in string, counting from zero. For instance, in the string 'abcdef', the character at position 0 is 'a', the character at position 2 is 'c', and so forth.

Example 1. Basic substr() usage

<?php
$rest = substr("abcdef", 1);    // returns "bcdef"
$rest = substr("abcdef", 1, 3); // returns "bcd"
$rest = substr("abcdef", 0, 4); // returns "abcd"
$rest = substr("abcdef", 0, 8); // returns "abcdef"

// Accessing via curly braces is another option
$string = 'abcdef';
echo $string{0};                // returns a
echo $string{3};                // returns d
?>
 
 

If start is negative, the returned string will start at the start'th character from the end of string.

Example 2. Using a negative start

<?php
$rest = substr("abcdef", -1);    // returns "f"
$rest = substr("abcdef", -2);    // returns "ef"
$rest = substr("abcdef", -3, 1); // returns "d"
?>
 
 

If length is given and is positive, the string returned will contain at most length characters beginning from start (depending on the length of string). If string is less than start characters long, FALSE will be returned.

If length is given and is negative, then that many characters will be omitted from the end of string (after the start position has been calculated when a start is negative). If start denotes a position beyond this truncation, an empty string will be returned.

Example 3. Using a negative length

<?php
$rest = substr("abcdef", 0, -1);  // returns "abcde"
$rest = substr("abcdef", 2, -1);  // returns "cde"
$rest = substr("abcdef", 4, -4);  // returns ""
$rest = substr("abcdef", -3, -1); // returns "de"
?>
 
 

 

by: globalwmPosted on 2003-04-25 at 09:15:30ID: 8396378

That was it! I needed to define as:

$DOCUMENT_ROOT = substr($_SERVER['PATH_TRANSLATED'],0,strlen($_SERVER['SCRIPT_NAME']) * - 1);


since I'm not using Apache.

As soon as I did this, no errors on the order processing page.  :)



 

by: RQuadlingPosted on 2003-04-25 at 09:33:01ID: 8396506

First thanks for the points.

Second, your script will work on your IIS server, but if you port this to a Unix platform, you will need to re-check what $_SERVER vars you have available.

Regards,

Richard.

 

by: fehaPosted on 2004-12-15 at 08:01:59ID: 12831229

This works on both NIX and WIN

http://".$_SERVER['SERVER_NAME']."".dirname(str_replace("\\\\", "/", $_SERVER['SCRIPT_NAME']))."/index.php

just tips
:-)
www.vision.to

 

by: fehaPosted on 2004-12-15 at 08:10:07ID: 12831334

sorry above was to find url path ...
but might be useful

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...