Undefined variable problem (not to do with register_globals)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
m1k3
Forum Newbie
Posts: 1
Joined: Fri Jun 21, 2002 10:38 am

Undefined variable problem (not to do with register_globals)

Post by m1k3 »

i am running apache & PHP (4.2.1), and i have the following script...

Code: Select all

<?php

$max_count = "10";
$os = "win";   // win or unix

If ($submit == "Ping!") &#123;
  If ($count > $max_count)
  &#123;
     echo 'Maximum for count is '.$max_count;
     echo '<a href="$script">Back</a>';
     $again = True;
  &#125;
  else
  &#123;
     If (ereg(" ",$host))
     &#123;
          echo 'No Space in Host field allowed !';
          echo '<a href="ping.php">Back</a>';
          $again = True;
     &#125;
     else
     &#123;
          echo("Ping Output:<br>");
          echo("<pre>");
          $host = escapeshellarg($host);
          $count = escapeshellarg($count);
          if ($os == "win")
          &#123;
             system("ping -n $count $host", $list);
          &#125;
          else
          &#123;
             system("ping -t $count $host", $list);
          &#125;;
          echo("</pre>");
      &#125;
   &#125;
&#125;
else
&#123;
  echo '
  <html><body>
  <form methode="post" action="ping.php">
  Enter IP or Host <input type="text" name="host"></input>
  Enter Count <input type="text" name="count" size="2" value="4"></input>
  <input type="submit" name="submit" value="Ping!"></input>
  </form>
  </body></html>';
&#125;
?>
when i try to run it, i get the following error:

Notice: Undefined variable: submit in d:\apache\htdocs\ping.php on line 6

This happends to all my scripts! (a diffrenent variable & line each time)
I have tryed changing register_globals on and off...
i cant seen to get the script to work!
Zmodem
Forum Commoner
Posts: 84
Joined: Thu Apr 18, 2002 3:59 pm

Post by Zmodem »

Once you tweak the register_globals option, you will need to restart the web server.

THe code you have will require reg globals to be ON. Make sure it's on, then reboot, and it should work
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

It really does have nothing to do with register_globals and more to do with the fact that you are trying to do conditional statements with variables that don't exist. You probably want to start using isset() to check if a variable exists before you test it against a condition:

Code: Select all

If (isset($submit) && $submit == 'Ping!') &#123;
Mac
Zmodem
Forum Commoner
Posts: 84
Joined: Thu Apr 18, 2002 3:59 pm

Post by Zmodem »

If register globals were on, the code he has would work just fine. $submit doesn't exist because he probably has reg globs off.

Code: Select all

If (isset($submit) && $submit == 'Ping!')
...is the same thing as what he has. Just a better way of doing it. And that code will still fail if reg globs is off.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Zmodem wrote:If register globals were on, the code he has would work just fine. $submit doesn't exist because he probably has reg globs off.
It was stupid of me to say that it had nothing to do with registered globals as that is mildly relevant but $submit doesn't exist the first time the script is run, registered globals on or off.
Zmodem wrote:

Code: Select all

if (isset($submit) && $submit == 'Ping!')
...is the same thing as what he has
Isn't the same thing as what he has because the notice about $submit will be displayed (using his code) whether or not register globals is on or off. With reg globals on, the notice will only appear the first time the code is run (before the form is submitted). With reg globals off it will continue to display on each submission. However using isset() means that the code will not run as expected with reg globals off but there shouldn't be any errors (at that point) with reg globals on.
Zmodem wrote:Just a better way of doing it.
Which surely makes it different from what he had.
Zmodem wrote:And that code will still fail if reg globs is off.
The code will not fail (ie. stop executing) whether reg globals is on or off - you will only get notices which do not stop the code from running just point out errors that might make the code run in a way that is unexpected.

Mac
Post Reply