Issue 2 - 09 June 2009 Go Back

My First Experience with PHP and IIS - Undefined index errors

I'd like to share my experience with deploying PHP e-commerce application on an IIS web server. I've found there are some issues which you need to keep in mind when developing PHP sites. I've designed and tested it on Apache server and was surprised when my client complained that it does not work. The problem was that his web server was running IIS.

The first two errors we came up to were

Notice: Undefined index: DOCUMENT_ROOT and Notice: Undefined index: REQUEST_URI

I have used $_SERVER['REQUEST_URI'] and $_SERVER['DOCUMENT_ROOT'] in the config file and also on some administrative pages. Luckily I've found a solution quickly. All you need to do is add extra lines before you start using the variables.

if(!isset($_SERVER['DOCUMENT_ROOT'])){

       if(isset($_SERVER['SCRIPT_FILENAME'])){
              $_SERVER['DOCUMENT_ROOT'] = str_replace( '\\', '/', substr($_SERVER['SCRIPT_FILENAME'], 0,
                                                                0-strlen($_SERVER['PHP_SELF'])));
       };
};
if(!isset($_SERVER['DOCUMENT_ROOT'])){
       if(isset($_SERVER['PATH_TRANSLATED'])){
              $_SERVER['DOCUMENT_ROOT'] = str_replace( '\\', '/', substr(str_replace('\\\\', '\\',
                                                                $_SERVER['PATH_TRANSLATED']), 0,
                                                                0-strlen($_SERVER['PHP_SELF'])));
       };
};

if (!isset($_SERVER['REQUEST_URI'])) {

       $_SERVER['REQUEST_URI'] = substr($_SERVER['PHP_SELF'],1 );
       if (isset($_SERVER['QUERY_STRING'])) {
              $_SERVER['REQUEST_URI'].='?'.$_SERVER['QUERY_STRING'];
       }
}

So the first round was through, but not all has worked yet. The next error we were getting was

Notice: Undefined index: hidCartId in

For some reason the system could not read the POST variables send by a form. I've spent a day to track the problem and finally realised that it did not like using <?php echo $_SERVER['PHP_SELF']; ?> in the following line:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>?step=3" method="post" ...

The link was displayed correctly but all the POST variables returned empty. So the simple solution for me was to specify directly the script name:

<form action="checkout.php?step=3" method="post" ...

 

So far they were the only issues I had related to IIS web server. Another thing I also noticed that the MySQL PASSWORD() function returned different encryption codes on different servers. So when client tried to log in into the admin page an error was returned saying that THE PASSWORD WAS WRONG. I had to run a SQL script to update passwords in the admin table.




© 2011 E-SIGHT PTY.LTD. Mentone Vic 3194 Melbourne                   :: PHP and IIS Undefined index errors:: |    Home    |    Contact Us    |    Sitemap    |