Jump to content

[SOLVED] Why does string from Get method later give undefined error??


Archimedees

Recommended Posts

$strMyServer = $_GET["MyServer"];

 

Above string is passed from a form by get method and it contains several server names.

 

if (isset($_GET["submit"]))
    {
      if (!empty($_GET["MyServer"]))
      {
      $strMyServer = $_GET["MyServer"];
    
      }// end of (!empty($_GET['MyServer']))
      else
      {
      die(" I died here");
      }//end of else
    }//end of isset($_GET["submit"]))

 

later in the script I want to search for each of  the elements in $strMyServer ( which are server names) in array, $arPageServers and only if it's not there in $arPingServers. When match is found the server is set to inactive in the $arPageServer and $arPingServer respectively.

Below is complete script to this effect.

 

<?php

// Process $_Get variables
     
    if (isset($_GET["submit"]))
    {
      if (!empty($_GET["MyServer"]))
      {
      $strMyServer = $_GET["MyServer"];
    
      }// end of (!empty($_GET['MyServer']))
      else
      {
      die(" I died here");
      }//end of else
    }//end of isset($_GET["submit"]))
    
    // Load page.conf
   
    $arPageConf = read_contents_pageconf("page.conf");
    $arPageServers = create_servernamelist_pageconf($arPageConf);
  
    $i=0; $MaxServers = count($arPageServers);
    for($i=0; $i<$MaxServers; $i++)
    {
      $arCurrentPageServer = $arPageServers[$i]; 
      $nFound = false; 
    }//end of for($i=0; $i<$MaxServers
    
      
    if($arCurrentPageServer["name"] == $strMyServer)
    {
       $Found = true;
       $arCurrentPageServer["active"] == true;
     //write new page.conf
    
     $nFileHandle = fopen("page.conf", "w");
     $i=0;
     $nMaxlength = count($arParsedLines);
    for ($i=0; $i<$nMaxlength; $i++)
    {
     $arCurrentLine = $arParsedLines[$i];
     $strNewLine ="";
     $bIsActive = $arCurrentPageServer["active"]; 
     
    if ($bIsActive==false)
    {
      $strNewLine = $strNewLine."#";
    }//end if ($bIsActive==false)
     
      //add URL to string
      $strNewLine = $strNewLine.$arCurrentLine["url"]."\t";
      $strNewLine = $strNewLine.$arCurrentLine["email"]. "\t";
      $strNewLine = $strNewLine.$arCurrentLine["size"]. "\t";
      $strNewLine = $strNewLine.$arCurrentLine["command"]. "\r\n";
      fwrite($nFileHandle,$strNewLine);
     } //end for ($i=0; $i<$nMaxlength; $i++) 
    
      fclose($nFileHandle);
    }
    else
    {
      // load ping.conf
   
      $arPingConf = read_contents_pingconf("ping.conf");
      $arPingServers = create_servernamelist_pingconf($arPingConf);
      
      $i=0; $MaxServers = count($arPingServers);
    
    for($i=0; $i<$MaxServers; $i++)
    {
      $arCurrentPingServer = $arPingServers[$i];
      $nFound = false; 
    }
    
    if($arCurrentPingServer["name"] == $strMyServer)
    {
      $nFound = true;
      $arCurrentPingServer["active"] == true;
    //write new ping.conf
    
      $nFileHandle = fopen("ping.conf", "w");
      $i=0;
      $nMaxlength = count($arParsedLines);
    for ($i=0; $i<$nMaxlength; $i++)
    {
      $arCurrentLine = $arParsedLines[$i];
      $strNewLine ="";
      
      $bIsActive = $arCurrentPingLine["active"];
      
    if($bIsActive==false)
    {
      $strNewLine = $strNewLine."#";
    }//end if ($bIsActive==false)
      
      //add URL to string
      $strNewLine = $strNewLine.$arCurrentLine["url"]."\t";
      $strNewLine = $strNewLine.$arCurrentLine["email"]. "\t";
      $strNewLine = $strNewLine.$arCurrentLine["size"]. "\t";
      $strNewLine = $strNewLine.$arCurrentLine["command"]. "\r\n";
      
      fwrite($nFileHandle,$strNewLine);
      
    }//end for ($i=0; $i<$nMaxlength; $i++)
    
      fclose($nFileHandle);
    }// end of if($arCurrentPingServer["name"] == $strMyServer)
      
    }// end of else
   
    
?>

 

My problem is that  I get

Notice: Undefined variable: strMyServer
on below lines  respectively.

Should I first make an array out of $strMyServer for me to compare individual values with the name of the servers in the PageServer and Ping Server lists? I would appreciate some help.

 

if($arCurrentPageServer["name"] == $strMyServer)

and

 

if($arCurrentPingServer["name"] == $strMyServer)

 

 

You haven't defined your $strMyServer variable UNTIL you get into your first isset if case.

If that case fails, then $strMyServer never gets "made" or "set". Thus the variable never exists if your statement fails at the top. So if you try and use it later on PHP can't access that variable as it doesn't exist.

Hence your error.

 

ALWAYS initialise your variables.

Put this at the top of your code.

 

$strMyServer = "";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.