PHP5 Include Doesn't Work?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LacrosseB0ss
    New Member
    • Oct 2006
    • 112

    PHP5 Include Doesn't Work?

    I'm currently working on an online directory system using PHP (due to server constraints). Normally I would use something else but c'est la vie! I have my pages on the server (Login.php, SomethingElse.p hp, etc.php). All of them have some form of php code associated with them.
    In my cgi-bin folder, I have the code which references my data files (XML for now, may switch to MySQL later). I am getting errors that say
    "cannot open file: ../cgi-bin/path.xml" and something about include.

    Now, I did some research and it claims that with php5, the code cannot use an include line for a file in a different directory. My question(s) are:
    1. Is this true?
    2. If so, is there another place I can put my files so the user can't see them? (code and data)

    Thanks all. I've just started working with php for this project so I really don't have a clue. Also, it's my first server project so the cgi-bin stuff was from stuff I've seen online. Thanks in advance for the help.

    - LB
  • ak1dnar
    Recognized Expert Top Contributor
    • Jan 2007
    • 1584

    #2
    a XML file? why do you want to include this in your php file. can I see specific coding that you have used so far, please !

    Comment

    • LacrosseB0ss
      New Member
      • Oct 2006
      • 112

      #3
      The reason for the XML was because I was originally going to code it as a servlet modifying an example from a college class. So I made up the XML files and then found out servlets won't work on our server.

      Having said that, here's the code:
      [php]
      <?php session_start;
      //include("../cgi-bin/PHP/dirlogin.php");
      //include("../other/dirlogin.php");

      $_SESSION['usedID']="Not Found";

      function login()
      {
      $blnFound = false; //was user found? default NO

      //open master XML
      $members=simple xml_load_file(' ../cgi-bin/XML/CongregationMas ter.xml');

      //search for user
      foreach($member s as $user)
      {
      //if found, store ID as session
      if ($user->FirstName == $_POST['txtFName'] && $user->LastName == $_POST['txtLName'] && $user->PostalCode == $_POST['txtPwd'])
      {
      $_SESSION['userID']=$user->ID;
      echo $_SESSION['userID']; //testing line
      $blnFound=true;
      break; //break out of the loop b/c user was found.
      }
      }

      if ($_SESSION['userID'] == "Not Found")
      {
      echo "User Not Found!"; //testing line
      $blnFound=false ;
      }

      return true; //testing - should be $blnFound
      } //*************** *************** *************** ****
      //END OF INCLUDE

      function checkLogin()
      {
      if(isset($_POST['cmdLogin']))
      {
      $found = login();

      if ($found)
      {
      header("Locatio n: DirConfirmTEST. php");
      exit();
      }
      else
      {
      echo "USER NOT FOUND!";
      }
      }
      }

      if(isset($_POST['cmdLogin']))
      {
      checkLogin();
      }
      ?>
      [/php]

      The commented stuff was in my original file. When it wasn't working I thought it was the cgi-bin folder so I moved it. Then I read php5 may not use includes. So I put it all together. Anyway, now the "simplexml_file _load()" line doesn't work. Any help is greatly appreciated. Thanks!

      - LB

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, LB.

        What error message are you getting?

        Comment

        • LacrosseB0ss
          New Member
          • Oct 2006
          • 112

          #5
          I am getting 2 consistently. I tried a few other things before replying back but they did no good. The error messages are:
          Warning: include(../PHP/dirlogin.php) [function.includ e]: failed to open stream: No such file or directory in /home/poqumtu/public_html/bloorcentral/DirLogin.php on line 35

          Warning: include() [function.includ e]: Failed opening '../PHP/dirlogin.php' for inclusion (include_path=' .:/usr/lib/php:/usr/local/lib/php') in /home/poqumtu/public_html/bloorcentral/DirLogin.php on line 35
          I also got an e-mail from our server owner/operator. According to him, php5 is installed. However, he did not know about HTTP includes (is this the entire path?) or why I was getting the include() error as he hasn't worked much with php.

          I have tried removing the php file from the cgi-bin folder, where it was originally, thinking that was the problem. Guess not. Any assistance anyone can provide would be greatly appreciated. This is really beginning to urk me....

          Thanks!
          - LB

          PS: that was my 100th post. Go Me! Can we have a party?
          Last edited by LacrosseB0ss; Nov 15 '07, 05:36 AM. Reason: milestone announcement :D

          Comment

          • LacrosseB0ss
            New Member
            • Oct 2006
            • 112

            #6
            as an aside, does anyone know of any good, free php debuggers out there? Right now I'm doing my HTML in Dreamweaver and I'm coding my php stuff in EditPlus. I'm not overly found of having to upload to the server whenever I want to test my code. Thanks for this as well!

            Comment

            • pbmods
              Recognized Expert Expert
              • Apr 2007
              • 5821

              #7
              Heya, LB.

              Try seeing if you can include /home/poqumtu/public_html/PHP/dirlogin.php.

              In terms of a 'good PHP debugger'...

              Try PHP :)

              Install PHP and Apache on your development machine so you can test your scripts before you upload them.

              Just be careful... you might have the latest version of PHP installed on your development machine, but if the server you're uploading to is running an earlier version (almost a complete certainty), you'll have to do some additional testing anyway to make sure your code is compatible with the older version.

              Comment

              • LacrosseB0ss
                New Member
                • Oct 2006
                • 112

                #8
                I fixed the include late last night. I took out the ../ before the cgi-bin and it works now. The problem I am having now is in the included file, my session variables are not working. I'm baffled by it honestly. I originally had the "session_start( );" line however it produced the following 2 errors:
                Warning: session_start() [function.sessio n-start]: Cannot send session cookie - headers already sent by (output started at /home/poqumtu/public_html/bloorcentral/DirLogin.php:8) in /home/poqumtu/public_html/bloorcentral/cgi-bin/PHP/dirlogin.php on line 2

                Warning: session_start() [function.sessio n-start]: Cannot send session cache limiter - headers already sent (output started at /home/poqumtu/public_html/bloorcentral/DirLogin.php:8) in /home/poqumtu/public_html/bloorcentral/cgi-bin/PHP/dirlogin.php on line 2
                Removing the session_start() ; line removed the errors but in both situations the $_SESSION['userID'] resulted in an empty variable. I'm so lost ....

                As for the "PHP" as a debugger???? Can you provide more info. I have PHP 5 on my system as well as Apache Tomcat. I can't figure out how I'm supposed to run my scripts off my machine first ... Thanks for the help so far.

                - LB

                Comment

                • jx2
                  New Member
                  • Feb 2007
                  • 228

                  #9
                  Originally posted by LacrosseB0ss
                  The problem I am having now is in the included file, my session variables are not working. I'm baffled by it honestly. I originally had the "session_start( );" line however it produced the following 2 errors:
                  you have to use session_start() as a very first thing you do
                  i mean :
                  [php]
                  <?php
                  //make sure that before<?php there is NOTHING no empty lines,
                  //no html, no any white spaces
                  session_star();
                  //the rest of yor code...
                  [/php]

                  regards
                  jx2

                  Comment

                  • pbmods
                    Recognized Expert Expert
                    • Apr 2007
                    • 5821

                    #10
                    Heya, LB.

                    Sounds like that particular file is getting included twice.

                    Consider using include_once instead of include.

                    This also works:
                    [code=php]
                    if( ! session_id() )
                    {
                    session_start() ;
                    }
                    [/code]

                    Comment

                    • ak1dnar
                      Recognized Expert Top Contributor
                      • Jan 2007
                      • 1584

                      #11
                      Originally posted by LacrosseB0ss
                      as an aside, does anyone know of any good, free php debuggers out there? Right now I'm doing my HTML in Dreamweaver and I'm coding my php stuff in EditPlus. I'm not overly found of having to upload to the server whenever I want to test my code. Thanks for this as well!
                      It's better to configure your own PC to work with php-apache. Try this tutorial to config. and there are tools Like XAMPP, AppServ you can use which has Installers to do the job for you.

                      Comment

                      • LacrosseB0ss
                        New Member
                        • Oct 2006
                        • 112

                        #12
                        Originally posted by jx2
                        you have to use session_start() as a very first thing you do
                        i mean :
                        [php]
                        <?php
                        //make sure that before<?php there is NOTHING no empty lines,
                        //no html, no any white spaces
                        session_star();
                        //the rest of yor code...
                        [/php]

                        regards
                        jx2
                        I have not tried this yet but that would make sense. All my php has been in between the </head> and <body> html tags. I'll give it a shot later this evening and let you all know. Thanks for the help!

                        Comment

                        • LacrosseB0ss
                          New Member
                          • Oct 2006
                          • 112

                          #13
                          So I am baffled again! I tried the session_start() ; at the beginning and, guess what! IT WORKED! However, when I transfer to the 2nd page, the variable is reset to what I initialized it as. Originally I set $_SESSION['userID'] = -1;
                          then it goes to the XML file and picks up whatever the ID is. I know this b/c I changed the action on the form to itself with an echo $_SESSION['userID'];. It displayed fine.

                          Knowing the session variable was setting properly I switched the action back to the 2nd page. The 2nd page has an echo $_SESSION['userID']; which displays -1. This is better than before when NOTHING was being displayed however why did the variable reset? I've included the code for the 2nd page this time:

                          [php]
                          <?php session_start() ; ?>

                          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                          "http://www.w3.org/TR/html4/loose.dtd">
                          <html>
                          <head>
                          <title>Confir m Login Details TESTING!</title>
                          <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
                          </head>

                          <?php
                          function getParam($param )
                          {
                          $member = simplexml_load_ file('/cgi-bin/XML/' . $_SESSION['userID'] . '.xml');
                          //$member = simplexml_load_ file('cgi-bin/XML/' . $testint . '.xml');

                          return $member->$param;
                          }
                          ?>

                          <body>
                          <form name="frmConfir m" method="request " action="DirConf irmTEST.php">
                          <p>
                          <h1><?php echo $_SESSION['userID']; ?></h1>
                          ID: <?php echo getParam('ID'); ?>
                          </p>
                          <p>
                          Name: <?php echo getParam('First Name') . " " . getParam('LastN ame'); ?>
                          </p>
                          <p>
                          Address: <?php echo getParam('Addre ss'); ?>
                          </p>
                          <p>
                          City: <?php echo getParam('City' ); ?>
                          </p>
                          <p>
                          Postal Code: <?php echo getParam('Posta lCode'); ?>
                          </p>
                          <p>
                          Phone: <?php echo getParam('Phone '); ?>
                          </p>
                          <p>
                          EMail: <?php echo getParam('EMail '); ?>
                          </p>
                          </form>
                          </body>
                          </html>
                          [/php]

                          Comment

                          • pbmods
                            Recognized Expert Expert
                            • Apr 2007
                            • 5821

                            #14
                            Heya, LB.

                            Not directly related, but allow me to advise:

                            [code=php]
                            .
                            .
                            .
                            function getParam($param )
                            {
                            static $member;
                            if( empty($member) )
                            {
                            $member = simplexml_load_ file('/cgi-bin/XML/' . $_SESSION['userID'] . '.xml');
                            }

                            return $member->$param;
                            }
                            [/code]

                            That way, you only have to simplexml_load_ file() once per page.

                            Comment

                            • LacrosseB0ss
                              New Member
                              • Oct 2006
                              • 112

                              #15
                              thanks for the tip. This has actually sped up the process. And it still works (as far as I can tell). Thanks.

                              However, as you suggested, it didn't help my session variable error. Thanks to anyone who can advise.

                              Comment

                              Working...