My (shared) host provider has upgraded to php5 from php4 and now I am getting errors in the php code. They will not provide help or further info about .ini file.
Can someone guide me as to what's wrong:

Error 1:

<?php if ($submit && !$errors) {

gives error msg:
Notice: Undefined variable: submit in line 196.

Error 2:

<?php
if ($_SERVER['HTTP_X_FORWARDED_FOR'])
  $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
else
  $ip   = $_SERVER['REMOTE_ADDR'];
$two_letter_country_code=iptocountry($ip);

function iptocountry($ip)
{
  $numbers = preg_split( "/./", $ip);
  include("ip_database.php");
  $code=($numbers[0] * 16777216)
+ ($numbers[1] * 65536) + ($numbers[2] * 256) + ($numbers[3]);
  foreach($ranges as $key=>$value)
  {
    if($key<=$code)
    {
      if($ranges[$key][0]>=$code)
    {
      $country=$ranges[$key][1];break;}
    }
  }
  if ($country=="")
  {
    $country="unknown";
  }
  return $country;
}

if ($two_letter_country_code =="ZA") {
	echo "ERROR 404 : PAGE NOT AVAILABLE";
  	die();
  	}
?>

Gives error messages:
Notice: Undefined index: HTTP_X_FORWARDED_FOR ....on line 2
-and-
Notice: Undefined variable: country in .... on line 20

    instead of

    if ($_SERVER['HTTP_X_FORWARDED_FOR'])

    try

    if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))

    instead of

    if ($country=="")

    try

    if (empty($country))

    Ideally, you would declare the variables at the beginning of the function and give it a default value.

      Also, for the undefined variables problem, where is $country ever defined? You probably meant $POST['country'] (or $GET['country']). In short, your old server configuration had register_globals enabled. This directive has been deprecated for some time now (and isn't even available in PHP6); you'll need to go through your PHP scripts and correct this.

      Here's a manual page explaining the various superglobal arrays ($GET, $POST, $_COOKIE, etc. etc.) and how to use them: [man]variables.predefined[/man].

      EDIT: Also, this:

      $numbers = preg_split( "/./", $ip);

      should probably be replaced with this:

      $numbers = explode('.', $ip);

        Thanks to your help, I'm well on my way to solving problem 2.

        Can someone tell me what's wrong with problem 1.
        ie.

        <?php if ($submit && !$errors) {
        ....

          Yes, I already did; re-read the first two paragraphs of my post above.

          EDIT: Oops - sorry, I had your problems mixed up. For problem two, you need to use [man]empty/man or [man]isset/man. If $_SERVER['HTTP_X_FORWARDED_FOR'] isn't defined, and you try to access it in an if() statement, PHP will throw a notice telling you that the array index doesn't exist. Same for $country; the if($country == "") statement assumes $country exists at the point when in fact it doesn't if the country wasn't found in the foreach() statement.

          For the latter of the two errors, I would suggest moving $country = "Unknown"; above the foreach() loop; that way if the country is found inside the loop then the variable's value will be overwritten. If it isn't, the "Unknown" value won't be changed and it'll still return what you wanted it to.

            Thanks, but I haven't understood your answer.

            I asked two questions. The 2nd of which you have answered/I have understood.

            Let me restate the 1st question again (hopefully better).

            1 - I have a contact.php form - which contains the basic form, and is re-used to send the email and to respond to the visitor.

            It contains all the usual html to display the form & fields, and a submit button.
            Here are the important lines of code: (... represents lines removed for clarity)

            <form name="form1" method="POST" action="contact.php">
            ...
            <?php
            if ($submit && !$errors) {
            echo ("Thank you for contacting us. Your Contact form has been submitted.");}
            ?>
            ... 
            <input name="firstName" type="text" class="TEXTFIELD" id="firstName" size="24" maxlength="30">
            <input name="lastName" type="text" class="TextField" id="lastName" size="21" maxlength="50">
            ...
            <input name="submit" type="submit" class="submit" value="Submit"></td>
            </form>

            at the bottom of the code is a section (see below) that sends the email:

            <?php
            if ($submit && !$errors){
            $message="
            Name: $firstName $lastName
            //lots more variables removed here for clarity
            ";
            
            $headers = "From: $firstName $lastName <$eMail>\r\n";
            ...
            mail("our_email address", "title", $message, $headers');             
            } ?>

            The code works with php4, but with php5 I get the following errors/warnings:

            Notice: Undefined variable: submit in ...contact.php on line 199
            Notice: Undefined variable: submit in ...contact.php on line 380

              It's not that it "worked" in PHP4, it's just that the configuration for the previous installation was very poor and had register_globals turned on.

              The problem you're describing stems from the fact that there is no such variable called $submit, or $firstName, or ... etc. (unless of course you defined them someplace that you're not showing us). Instead, you should be using the $_POST superglobal array.

                Thanks very much,
                I've read the articles you suggested, and (hopefully) now understood the $POST .
                But when I now use:
                <?php
                if ($
                POST[submit]) {

                ... and later...
                $firstName =$_POST['firstName'];

                I get errors that 'submit' and 'firstName' are not defined, even though they are input fieild 'names' in my form.

                I've googled for "example php mail forms", but the examples I've found are either :
                - too simple (ie use a 2nd page to say 'Thank you').
                - or use a single form, but all the form is within the <?php .... ?>
                so the form doesn't show in dreamweaver (which I use for devloping the site.)

                Could you possiblly point me to a good example that uses a single page for the input form, and on submit the same page to send the mail and give a respons to the user.

                What (I think) i need is something like:

                <html>
                <body>
                some PHP to test:
                if form has been submitted - to respond to the use and send the mail
                if form hasnt been submitted - to say ..please fillin the form

                some HTML (not in php tags) to display the form

                Hope this makes sense:-)

                Thanks again for your help.

                  gretsch;10897610 wrote:

                  I get errors that 'submit' and 'firstName' are not defined, even though they are input fieild 'names' in my form.

                  brad is right, the reason it worked before is, because the previous version of PHP was configured to automatically create variables with the same name as the $_POST variables. This was later removed, and for good reason.

                  instead of

                  <?php
                  if ($_POST[submit]) {
                  
                  ... and later...
                  $firstName =$_POST['firstName'];

                  try

                  <?php
                  if (!empty($_POST['submit'])) {
                  
                  ... and later...
                  if (!empty($_POST['firstName'])) $firstName = $_POST['firstName'];

                    Just to offer an alternative, here's how I often set variables based on incoming data:

                    $firstname = (isset($_POST['firstname']) ? $_POST['firstname'] : '');
                    // etc. for other variables

                    This code makes use of the "ternary syntax" - a shorthand method of writing an if/else statement.

                      Thank you both for your time and patience.
                      I've now understood, and the problem is now resolved (and it all looks neater code now:-)

                      PS Happy New Year
                      Your assistance makes the Internet a really nice place to work - thank you.

                        Write a Reply...