php 5 and uninitialized variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • steve

    php 5 and uninitialized variables

    i've setup a include page that's responsible for building the basic layout
    of my web pages (header, menus, etc.). each page includes this
    "sysheader.php" .

    the first page i've built with this header is having odd results. its job is
    to add users to the system. the variables i expect to be there are: id,
    userName, password...stuf f like that. after the require_once
    "sysheader.php" , i've put an "exit;" statement. the header shows perfectly.
    if i try and access or set one of the expected variables, only part of the
    header shows...crappin g out around the time it is building the company
    logo - so that i see "<img src="http://www" and nothing else.

    i've tried to set my own variable order and initialze the variables myself
    like this:

    $id = (isset($_POST['id']) ? $_POST['id'] : $_GET['id']);

    but that doesn't work either. i've been running similarly constructed pages
    successfully under php < 5...what am i missing now that i'm moving to php 5?

    tia,

    steve



  • John Bokma

    #2
    Re: php 5 and uninitialized variables

    steve wrote:
    [color=blue]
    > i've setup a include page that's responsible for building the basic
    > layout of my web pages (header, menus, etc.). each page includes this
    > "sysheader.php" .
    >
    > the first page i've built with this header is having odd results. its
    > job is to add users to the system. the variables i expect to be there
    > are: id, userName, password...stuf f like that. after the require_once
    > "sysheader.php" , i've put an "exit;" statement. the header shows
    > perfectly. if i try and access or set one of the expected variables,
    > only part of the header shows...crappin g out around the time it is
    > building the company logo - so that i see "<img src="http://www" and
    > nothing else.
    >
    > i've tried to set my own variable order and initialze the variables
    > myself like this:
    >
    > $id = (isset($_POST['id']) ? $_POST['id'] : $_GET['id']);[/color]

    REQUEST[ 'id' ] if my memory serves me correctly has either POST or GET


    --
    John MexIT: http://johnbokma.com/mexit/
    personal page: http://johnbokma.com/
    Experienced programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html

    Comment

    • Janwillem Borleffs

      #3
      Re: php 5 and uninitialized variables

      steve wrote:[color=blue]
      > i've tried to set my own variable order and initialze the variables
      > myself like this:
      >
      > $id = (isset($_POST['id']) ? $_POST['id'] : $_GET['id']);
      >
      > but that doesn't work either. i've been running similarly constructed
      > pages successfully under php < 5...what am i missing now that i'm
      > moving to php 5?
      >[/color]

      You could also do something like:

      $id = @$_REQUEST['id'];

      This will catch the id when it's posted or passed as a GET parameter or
      create an empty variable without warnings, whichever is appropriate.

      Anyways, I'm almost certain that your code relies on register_global s being
      enabled, while with PHP 5 it's disabled by default as it should.

      Enable the register_global s directive in your php.ini file and check if the
      code works. When it does, you will have to convert all code that relies on
      register_global s, which also includes the session system.


      JW



      Comment

      • steve

        #4
        Re: php 5 and uninitialized variables

        | REQUEST[ 'id' ] if my memory serves me correctly has either POST or GET

        thank for the info...didn't fix the problem though. any other ideas on how
        to handle expected, yet uninitialized, variables? can i change something in
        the php.ini file?


        Comment

        • Janwillem Borleffs

          #5
          Re: php 5 and uninitialized variables

          steve wrote:[color=blue]
          > thank for the info...didn't fix the problem though. any other ideas
          > on how to handle expected, yet uninitialized, variables? can i change
          > something in the php.ini file?[/color]

          Use isset() to check if they exist or prepend the @ sign to surpress error
          messages. Don't mess with the error_reporting settings, because surpressing
          notices and warnings at this level will only cause errors to be harder to
          find.


          JW



          Comment

          • steve

            #6
            Re: php 5 and uninitialized variables

            | You could also do something like:
            |
            | $id = @$_REQUEST['id'];
            |
            | This will catch the id when it's posted or passed as a GET parameter or
            | create an empty variable without warnings, whichever is appropriate.
            |
            | Anyways, I'm almost certain that your code relies on register_global s
            being
            | enabled, while with PHP 5 it's disabled by default as it should.
            |
            | Enable the register_global s directive in your php.ini file and check if
            the
            | code works. When it does, you will have to convert all code that relies on
            | register_global s, which also includes the session system.

            thanks for the input...it doesn't take care of the problem though. i have
            register_global s=Off and did not have it set otherwise in previous versions
            of php. enabling that setting in php 5 sends it merrily running on and on
            until i have to kill the process manually.

            i do get output but the buffer only spews until it hits the line of code
            where a variable is utilized (set/get) but not initialized.

            any further input is greatly apprecieated.


            Comment

            • steve

              #7
              Re: php 5 and uninitialized variables

              | Use isset() to check if they exist or prepend the @ sign to surpress error
              | messages. Don't mess with the error_reporting settings, because
              surpressing
              | notices and warnings at this level will only cause errors to be harder to
              | find.

              i have tried using isset() and the @ sign previously...bu t to no avail.
              should i just turn on logging and see what php reports?


              Comment

              • Janwillem Borleffs

                #8
                Re: php 5 and uninitialized variables

                steve wrote:[color=blue]
                > thanks for the input...it doesn't take care of the problem though. i
                > have register_global s=Off and did not have it set otherwise in
                > previous versions of php. enabling that setting in php 5 sends it
                > merrily running on and on until i have to kill the process manually.
                >
                > i do get output but the buffer only spews until it hits the line of
                > code where a variable is utilized (set/get) but not initialized.
                >[/color]

                Okay, then it's time to try changing the error_reporting level. This will
                only be a test, ensure to restore it to its original level when done
                testing.

                Go to the ini file and check that the error_reporting directive looks as
                follows:

                error_reporting = E_ALL & ~E_NOTICE

                Then check if the scripts work now.


                JW



                Comment

                • Janwillem Borleffs

                  #9
                  Re: php 5 and uninitialized variables

                  steve wrote:[color=blue]
                  > i have tried using isset() and the @ sign previously...bu t to no
                  > avail. should i just turn on logging and see what php reports?[/color]

                  Ah, so you don't see any error output, but the script just stops? Yes, then
                  you definitely need to enable logging.

                  You can enable the display_errors directive in the ini file for easy
                  debugging.


                  JW



                  Comment

                  • steve

                    #10
                    Re: php 5 and uninitialized variables

                    | Ah, so you don't see any error output, but the script just stops? Yes,
                    then
                    | you definitely need to enable logging.
                    |
                    | You can enable the display_errors directive in the ini file for easy
                    | debugging.

                    right...but this is also part of my quandry. display_errors is set to On
                    *and* i don't see any errors in my browser prior to the script crapping out.


                    Comment

                    • steve

                      #11
                      Re: php 5 and uninitialized variables

                      | Okay, then it's time to try changing the error_reporting level. This will
                      | only be a test, ensure to restore it to its original level when done
                      | testing.
                      |
                      | Go to the ini file and check that the error_reporting directive looks as
                      | follows:
                      |
                      | error_reporting = E_ALL & ~E_NOTICE
                      |
                      | Then check if the scripts work now.

                      that's what it was originally set to...i changed it to only show errors. in
                      combination with that, i've added:

                      $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : 0;

                      and cleared my browser's history. there doesn't seem to be a problem with
                      the above now.

                      i guess that solve the script-stoppage problem, but i'd like to know why
                      uninitialized variable useage would cause the problem *and* not even give an
                      error message of some kind.

                      thanks for your help.


                      Comment

                      • Janwillem Borleffs

                        #12
                        Re: php 5 and uninitialized variables

                        steve wrote:[color=blue]
                        > right...but this is also part of my quandry. display_errors is set to
                        > On *and* i don't see any errors in my browser prior to the script
                        > crapping out.[/color]

                        Can you post some of the code which then is encountered?


                        JW




                        Comment

                        • steve

                          #13
                          Re: php 5 and uninitialized variables

                          sure:

                          here's the "menu" page where it is intended to give a web page a
                          description, whether is requires user access rights, it's url is defined,
                          etc...

                          <?
                          $pageTitle = "User Menus";
                          require_once "../inc/head.inc.php";

                          $sql = "";
                          // echo $_REQUEST['id']; // script craps out here if uncommented
                          $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : 0; // this is ok.
                          exit;
                          ?>

                          here's the "head.inc.p hp" script (i can include all dependent files
                          (site.cfg.php, base.css.php, etc. if you are trying to fully recreate the
                          problem):

                          <?
                          include_once "../htdocs/site.cfg.php";
                          $pageAccess = "";
                          $userId = -1;
                          if (isset($userNam e) && isset($userPass word))
                          {
                          $sql = "
                          SELECT Id,
                          FirstName,
                          LastName,
                          (
                          SELECT COUNT(*) IsValid
                          FROM people
                          WHERE UserName = '$userName'
                          AND Password = '$userPassword'
                          ) IsValid
                          FROM people
                          WHERE UserName = '$userName'
                          AND Password = '$userPassword'
                          ";
                          $records = odbc_exec($db, $sql);
                          $validUser = odbc_result($re cords, "IsValid") > 0;
                          $userFullName = odbc_result($re cords, "FirstName" ) . " " .
                          odbc_result($re cords, "LastName") ;
                          $userId = odbc_result($re cords, "Id");
                          }
                          if ($validUser)
                          {
                          if ($userMenu == "")
                          {
                          $sql = "
                          SELECT w.Category,
                          w.Description,
                          w.Url
                          FROM accessList a,
                          webPages w
                          WHERE w.Id = a.Url
                          AND a.person = $userId
                          ORDER BY w.Category,
                          w.Description,
                          ViewOrder
                          ";
                          $records = odbc_exec($db, $sql);
                          $currentCat = "";
                          $userMenu = "";
                          $i = 0;
                          while(odbc_fetc h_row($records) )
                          {
                          $category = odbc_result($re cords, "Category") ;
                          $description = odbc_result($re cords, "Descriptio n");
                          $url = odbc_result($re cords, "Url");
                          if ($category != $currentCat)
                          {
                          if ($category != ""){ $userMenu .= "</div>\r\n"; }
                          $userMenu .= "<div class=\"items\"
                          onclick=\"docum ent.getElementB yId('_$i').styl e.display =
                          (getElementById ('_$i').style.d isplay == 'none' ? 'block' : 'none');\">";
                          $userMenu .= "<a alt=\"$category \" href=\"\" onclick=\"retur n
                          false;\">$categ ory</a>";
                          $userMenu .= "</div>\r\n<div id=\"_$i\" style=\"display :'none';
                          margin-left:'15px';\"> \r\n";
                          $i++;
                          }
                          $userMenu .= "<span style=\"padding :'2px';\"><a href=\"$url\"
                          alt=\"$descript ion\">$descript ion</a></span><b><b></b></b><br>\r\n";
                          }
                          if ($i != 0){ $userMenu .= "</div>\r\n"; }
                          setcookie("user Menu", $userMenu);
                          }
                          }
                          echo $sessionHeader;
                          ?>
                          <!------------ marker for top ------>
                          <a name="#top"></a>
                          <!------------ top menu ------------>
                          <div class="menuTop" >
                          <div class="title">
                          <b><big><?= $siteTitle ?></big></b>
                          </div>
                          <?
                          if ($validUser)
                          {
                          ?>
                          <span class="title" style="font-size:'<?= $sessionFonts["NORMAL"]
                          ?>';">
                          <?= $userFullName ?>
                          </span>
                          <br>
                          <?
                          }
                          ?>
                          <span class="items">
                          <?
                          // generate menu items

                          $stdMenu = array(
                          "Home" => $siteUrl
                          );
                          foreach($stdMen u as $description => $url)
                          {
                          echo " <a href=\"$url\" alt=\"$descript ion\"><span
                          class=\"item\"> $description</span></a>\r\n";
                          }
                          if ($validUser)
                          {
                          echo " <a href=\"?logout\ " alt=\"Log Out\"><span class=\"item\"> Log
                          Out</span></a>\r\n";
                          }
                          ?>
                          </span>
                          </div>
                          <!------------ left menu ----------->
                          <span class="menuLeft ">
                          <br>
                          <div class="logo">
                          <img src="<?= $siteCompanyLog o ?>" alt="Company Logo">
                          </div>
                          <div style="font-size:'<?= $sessionFonts["NORMAL"] ?>'; margin:'5px';
                          width:'90%'">
                          <b><i><?= $siteDescriptio n ?></i></b><br>&nbsp;
                          </div>
                          <div class="items">
                          <?
                          // generate menu items

                          $stdMenu = array(
                          "Home" => $siteUrl
                          );
                          foreach($stdMen u as $description => $url)
                          {
                          echo " <a href=\"$url\"
                          alt=\"$descript ion\">$descript ion</a><br>\r\n";
                          }
                          ?>
                          </div>
                          <?
                          if ($userMenu != "")
                          {
                          echo $userMenu;
                          }
                          ?>
                          <br>
                          <form method="get" target=_blank
                          action="http://www.google.com/custom">
                          <div class="row">
                          <span class="label" style="backgrou nd-color:'#000067' ; border:'1px
                          solid white'; width:'100%';">
                          <span class="label" style="color:'w hite'; font-size:'<?=
                          $sessionFonts["LARGE"] ?>'; margin:'5px';">
                          <b>Search<b>
                          </span>
                          </span>
                          <a href="http://www.google.com/search" target=_blank></a>
                          <br>
                          <br>
                          <input class="value" type="text" name="q" maxlength="255"
                          autocomplete="o ff" style="font-size:'<?= $sessionFonts["NORMAL"] ?>';
                          margin-right:'2px'; width:'100%';">
                          <br>
                          <br>
                          <span class="label">< ?= $siteHost ?></span>
                          <input style="width:'1 0px';" type="radio" name="sitesearc h"
                          value="<?= $siteHost ?>" checked>
                          <span class="label">T he Web</span>
                          <input style="width:'1 0px';" type="radio" name="sitesearc h"
                          value="">
                          <br>
                          <br>
                          <span class="label" style="text-align:'center';
                          width:'100%'">< input type="submit" name="sa" value="Search"> </span>
                          </div>
                          <br>
                          <a href="http://www.google.com/search" target=_blank>
                          <img src="http://www.google.com/logos/Logo_25wht.gif" border="0px"
                          alt="Google" name="Google"></a>
                          <br>
                          <span class="label" style="text-align:'left';
                          width:'100%';"> <i>[Search Powered By Google]</i></span>
                          <input type="hidden" name="cof" value="LW:170;L :<?= $siteCompanyLog o
                          ?>;LH:36;AH:lef t;AWFID:b16640e 8f2fd59e3;">
                          <input type="hidden" name="domains" value="<?= $siteHost ?>">
                          </form>
                          </span>
                          <!------------ body text ----------->
                          <span class="body">
                          <br>
                          <div class="pageTitl e">
                          <img src="<?= $siteLogo ?>" style="float:'r ight';" alt="<?=
                          $pageTitle ?>">
                          <br clear="all">
                          <span style="margin:' 20px';"><big><b ><?= $pageTitle
                          ?></b></big></span>
                          <hr>
                          </div>


                          Comment

                          • Janwillem Borleffs

                            #14
                            Re: php 5 and uninitialized variables

                            steve wrote:[color=blue]
                            > <?
                            > $pageTitle = "User Menus";
                            > require_once "../inc/head.inc.php";
                            >
                            > $sql = "";
                            > // echo $_REQUEST['id']; // script craps out here if uncommented
                            > $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : 0; // this is
                            > ok. exit;
                            >[/color]

                            This probably doesn't break the script, try the following:

                            <?
                            echo $_REQUEST['newvar'];
                            echo "hello";
                            ?>

                            This will probably print the expected "hello". When it does, the following
                            question would be, where is $id used in the code? When it's used in a
                            database query and you do something like "WHERE id=$id" while $id is empty,
                            the error would originate from the ODBC driver, because the query will be
                            invalid.

                            However, when you don't see any output and nothing is recorded in the log
                            files, it's time to check the installation of PHP. Perhaps something went
                            wrong during the compilation of PHP.


                            JW


                            Comment

                            • steve

                              #15
                              Re: php 5 and uninitialized variables


                              "Janwillem Borleffs" <jw@jwscripts.c om> wrote in message
                              news:41d08d28$0 $83977$1b2cd167 @news.euronet.n l...
                              | steve wrote:
                              | > <?
                              | > $pageTitle = "User Menus";
                              | > require_once "../inc/head.inc.php";
                              | >
                              | > $sql = "";
                              | > // echo $_REQUEST['id']; // script craps out here if uncommented
                              | > $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : 0; // this is
                              | > ok. exit;
                              | >
                              |
                              | This probably doesn't break the script, try the following:
                              |
                              | <?
                              | echo $_REQUEST['newvar'];
                              | echo "hello";
                              | ?>
                              |
                              | This will probably print the expected "hello". When it does, the following
                              | question would be, where is $id used in the code? When it's used in a
                              | database query and you do something like "WHERE id=$id" while $id is
                              empty,
                              | the error would originate from the ODBC driver, because the query will be
                              | invalid.
                              |
                              | However, when you don't see any output and nothing is recorded in the log
                              | files, it's time to check the installation of PHP. Perhaps something went
                              | wrong during the compilation of PHP.

                              i'm re-installing now as a matter of fact. i'll let you know how things go.

                              thanks again for all your help.


                              Comment

                              Working...