• PHP Help PHP Coding
  • 2 part question about log_errors to a file and undefined index/variable from function

Hi All,

I am trying to improve my coding and I am reading about saving out the erorrs to a log file, from the 'Essential PHP Security' book. They give this example:

ini_set('error_reporting', E_ALL | E_STRICT);
ini_set('display_errors', 'Off');
ini_set('log_errors', 'On');
ini_set('error_log', 'usr/local/apache/logs/error_log');

But I am running on a Windows Server 2003 machine and I tried to change out the path to 'c😛HP/logs/error_log' and I get a permision error, and when I check that directory, Read Only is marked and even logged in as Admin, it reverts back to Read Only after I change it.

Also, when I do a

error_reporting(E_ALL & ~E_STRICT);

On a page that I use to display a list of countries I get from the db and use this function to make the drop-down:

//--------------- start makeDropListSession -----------------//
function makeDropListSESSION($name,$list,$selected="")
{
  // $name       select name
  // $list       array of value, label pair
  // $selected   selected value
  // $x tabindex value
  global $x;
  global $label;
foreach($list as $value=>$label)
  {
  if(($_SESSION[$label] != '') || ($selected == $value))
    {
    $options .= '<option selected value="'.$value.'">'.$label.'</option>';
    }
  else
    {
    $options .= '<option value="'.$value.'">'.$label.'</option>';
    }
  }
  $dropList = '<select name="'.$name.'" id="' . $name . '" tabindex="'.$x.'">'.$options.'
</select>'."\n";
  return $dropList;
}
//--------------- end makeDropListSession -----------------//

I get a list of undefined index and variable errors for:

//Undefined index: Choose your Country - for this line of code
if(($_SESSION[$label] != '') || ($selected == $value)) //

//and
//Undefined variable: options - for this line of code
$options .= '<option selected value="'.$value.'">'.$label.'</option>';

I know that is has been suggested to test for the variable first:

if(isset($_SESSION['Country']))

but how would I incorporate that into that function is what I do not understand.

I appreciate the help, as always,

Don

    For the first question: Best and simplest is to use a path where you have write permissions - a good place are the user documents. (Or study the Windows Server 2003 documentation on how to change permissions)

    Second:

    undefined index: the index doesn't exist. That means it has not yet been set.
    It's always safest to first check if the variable is set, as you have pointed out yourself already. and then see if there is a value at all, either by putting all into one if statement or by nesting. Maybe use empty() instead of checking for ''. have a good look at both isset() and empty() in the manual

    Nesting first:

    if(isset($_SESSION['Country'])) {
        if(!empty($_SESSION[$label]) || ($selected == $value)) {
            //your code goes here
        }
    }

    integrated:

    if(isset($_SESSION['Country']) && (!empty($_SESSION[$label]) || ($selected == $value))) {
        //your code goes here
    }

    Third:
    undefined variable: the variable has not been assigned a value, but you use the ".=" operator to "add" to it, i.e. to concatenate.

    instead of

    $options .= '<option selected value="'.$value.'">'.$label.'</option>'; 

    do:

    $options = '<option selected value="'.$value.'">'.$label.'</option>'; 

    i.e.: get rid of the concatenation operator

    hth

    Bjom

      Isn't this two questions? One for the Windows forum?

        Write a Reply...