Why do does this give me an error?

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

    Why do does this give me an error?

    I have a file called t.php:

    <html>
    <body>

    <?php
    require "http://localhost/~derek/l.php";
    rp(3);
    ?>

    </body>
    </html>


    and l.php:

    <?php
    echo "define function...";
    function rp( $num )
    {
    echo "rp: $num";
    }
    echo "defined";
    ?>


    When I load t.php into my browser it prints "define function...defi ned
    Fatal error: Call to undefined function: rp()
    in /home/derek/public_html/t.php on line 6"

    So, I know the library code is being loaded and executed (because the
    strings are echoed) but rp() isn't being defined. Experimentation shows
    it's not just functions which I'm not getting defined, as I get the same
    sort of error if I just define a variable and try to get it printed in
    t.php.

    Can someone tell me what I'm doing wrong? I'm using PHP-4.3.4 on SUSE
    LINUX-9.1.
  • Markku Uttula

    #2
    Re: Why do does this give me an error?

    Derek Fountain wrote:[color=blue]
    > So, I know the library code is being loaded and executed (because
    > the
    > strings are echoed) but rp() isn't being defined.[/color]

    Actually, Your webserver is executing the php-file (and thus outputs
    the lines) but you're receiveing only the executed outcome, not the
    php-file itself.

    So essentially, what you get from http://localhost/~derek/l.php is the
    following:

    define function...defi ned

    and not what you're expecting. Hope I'm making sense here :)

    --
    Markku Uttula

    Comment

    • Matthias Czapla

      #3
      Re: Why do does this give me an error?

      On 2005-01-04, Derek Fountain <nospam@example .com> wrote:[color=blue]
      > I have a file called t.php:
      >
      ><html>
      > <body>
      >
      ><?php
      > require "http://localhost/~derek/l.php";
      > rp(3);
      > ?>
      >
      > </body>
      ></html>
      >
      >
      > and l.php:
      >
      ><?php
      > echo "define function...";
      > function rp( $num )
      > {
      > echo "rp: $num";
      > }
      > echo "defined";
      > ?>
      >
      >
      > When I load t.php into my browser it prints "define function...defi ned
      > Fatal error: Call to undefined function: rp()
      > in /home/derek/public_html/t.php on line 6"
      >
      > So, I know the library code is being loaded and executed (because the
      > strings are echoed) but rp() isn't being defined. Experimentation shows
      > it's not just functions which I'm not getting defined, as I get the same
      > sort of error if I just define a variable and try to get it printed in
      > t.php.[/color]

      You are not including the source code of l.php but the result of it being
      run on the server, which is just the string "define function...defi ned",
      which in turn is interpreted as HTML and displayed by t.php. You must either
      include the file locally (e.g. require "/home/derek/l.php";) or make the
      server not interpret the file (e.g. by giving it another extension like
      ".inc" or ".txt").

      Regards
      Matthias

      Comment

      • Derek Fountain

        #4
        Re: Why do does this give me an error?

        > You are not including the source code of l.php but the result of it being[color=blue]
        > run on the server, which is just the string "define function...defi ned",
        > which in turn is interpreted as HTML and displayed by t.php. You must
        > either include the file locally (e.g. require "/home/derek/l.php";) or
        > make the server not interpret the file (e.g. by giving it another
        > extension like ".inc" or ".txt").[/color]

        Ah, right. Thanks. But just to clarify, if I rename it to something other
        than .php, and someone learns the pathname to the file, they can ask my
        webserver for the file directly and they'll get to see my source code -
        right?

        Comment

        • Matthias Czapla

          #5
          Re: Why do does this give me an error?

          On 2005-01-04, Derek Fountain <nospam@example .com> wrote:[color=blue][color=green]
          >> You are not including the source code of l.php but the result of it being
          >> run on the server, which is just the string "define function...defi ned",
          >> which in turn is interpreted as HTML and displayed by t.php. You must
          >> either include the file locally (e.g. require "/home/derek/l.php";) or
          >> make the server not interpret the file (e.g. by giving it another
          >> extension like ".inc" or ".txt").[/color]
          >
          > Ah, right. Thanks. But just to clarify, if I rename it to something other
          > than .php, and someone learns the pathname to the file, they can ask my
          > webserver for the file directly and they'll get to see my source code -
          > right?[/color]

          Correct.

          Regards
          Matthias

          Comment

          • CJ Llewellyn

            #6
            Re: Why do does this give me an error?

            "Derek Fountain" <nospam@example .com> wrote in message
            news:41da9304$0 $31836$5a62ac22 @per-qv1-newsreader-01.iinet.net.au ...[color=blue][color=green]
            > > You are not including the source code of l.php but the result of it[/color][/color]
            being[color=blue][color=green]
            > > run on the server, which is just the string "define function...defi ned",
            > > which in turn is interpreted as HTML and displayed by t.php. You must
            > > either include the file locally (e.g. require "/home/derek/l.php";) or
            > > make the server not interpret the file (e.g. by giving it another
            > > extension like ".inc" or ".txt").[/color]
            >
            > Ah, right. Thanks. But just to clarify, if I rename it to something other
            > than .php, and someone learns the pathname to the file, they can ask my
            > webserver for the file directly and they'll get to see my source code -
            > right?[/color]

            Yes, for which very reason you'll probably want to want to use a more
            appropriate technology such as SOAP





            Comment

            • JV

              #7
              Re: Why do does this give me an error?

              Derek Fountain wrote:[color=blue]
              > Ah, right. Thanks. But just to clarify, if I rename it to something other
              > than .php, and someone learns the pathname to the file, they can ask my
              > webserver for the file directly and they'll get to see my source code -
              > right?[/color]

              yup but if you place the files that you are wanting to display in this
              manner in a folder that is outside of the web-root server such as

              /var/www/txt/foo.bar

              instead of in

              /var/www/html/foo.bar

              where /var/www/html/ is the web-root directory

              then the files in the txt/ folder are not accessible directly to
              browsers and php files can load them by using ../txt/foo.bar

              this is a fairly basic principle of site design/management i think... :/

              hth
              JV

              Comment

              • George

                #8
                Re: Why do does this give me an error?

                Hey,

                While JV is right, that would only work if the files you include reside
                on the same server as your script (assuming your script would have
                access to the files).

                Otherwise, as I imagine you want, to execute remotely hosted functions
                and in the same time conseal the source, CJ Llewellyn is right that you
                should either use a technology like SOAP or just design a simple
                interface to your PHP functions (latter being, to my preference, the
                worse approach, because you can just use something standartized).
                George

                Comment

                Working...