Hi all,

Having trouble finding an answer to my question so i thought i would try here.

I have multiple sites that I want to include some php files from a main site. So when I change the files on the main site all the other sites will be updated as well.

http://www.example.com/include.php

include 'http://www.example.com/include.php';

Not a problem.

The problem I'm having is after i include this file I can't call any functions/classes in this file.

Am I missing something or doing something wrong?

All help is appreciated. 😕

    I'm going to refer to the manual on this:

    If the target server interprets the target file as PHP code, variables may be passed to the included file using a URL request string as used with HTTP GET. This is not strictly speaking the same thing as including the file and having it inherit the parent file's variable scope; the script is actually being run on the remote server and the result is then being included into the local script.

    So if the other server interprets php files, all you'll get is the response after parsing. You can try changing the extension to .php.inc or .inc; however, I'm not sure if it will help since you will then be able to view the code on-line if you browse to it.

      let me see if i can explain this better.

      site1.com/index.php
      -> include test.com/incude.php

      include.php has just functions in it

      when i call a function in test.com/include.php after i have included it
      i get this error

      Call to undefined function

      now if the include file is on the same domain it is fine.
      i turned on allow_url_include to be able to include php files from one of my other domains.

        When you include via a HTTP request (which is what you are doing if you specify a URL for the include file), it is just like when your browser requests that file from the server: the request is processed by the web server on the include file's server, which sends it to the PHP processor, then sends the resulting output of that script back to the requesting server and script. Since your include file does not actually output anything, your calling file receives nothing, and thus those functions are not defined in that script.

          Thanks NogDog.

          Do you know a way to accomplish what I am trying to do or maybe point me in the right direction?

            Did you not read my post?

            You can try two things:

            1.) Use a different extension for the file, e.g. ".inc", instead of ".php" and include it that way. You'd have to of course exec() the code in it.

            2.) Use FTP to go and grab the contents of the file into a string, and exec() that way.

            Of course, the best way is to actually rework your structure to use an API. If you have 100 sites, and each one includes from the main site, there are options for you:
            1.) Use rsync to synchronize specific files or folders between sites. Then you can do local includes and be assured that it's the most "up-to-date" code available.

            2.) Create an API so that you can call somepage.php?func=somefunction;args=something;arg2=something which calls the appropriate function sending the given arguments, returning the output (either as a serialized array / object, or string).

              I don't know why but i did miss your last post. Sorry about that. I wasn't ignoring you 🙂

              I changed the file name to ".inc" and kept the <?php tags in there.

              That works but the parameters aren't accepted.

              I haven't used exec() a lot in the past so after looking at it I have another question.

              Would I just do exec("http://www.example.com/include.php")?
              Or would the exec() function be in the include file?

              Thanks ahead of time.

                It's okay. I'm usually ignored in real life 😉

                I'm not too versed in it, but I know there's a way you can parse plain-text by the PHP parser to be current executed code.... can't think of how to do it right now though...

                  It may be time to pull back a bit and figure out what it is you are trying to accomplish and how it should be done. From a security standpoint, a script on any web server that outputs raw PHP code is usually not a very good idea. And if you are willing to accept that risk, you still add another security risk on the calling script if you ask it to use [man]eval/man or a similar mechanism to parse the output of an external script as PHP code.

                  If you need to get information from another server, I would think it would be better to have the "include" script to instead be a self-contained script that will output the necessary data in some organized manner, that would then be used by the calling script. That output might be XML to make it generally accessible by any type of application, or if PHP-specific it might be a serialized array or even a serialized object.

                  But this is all speculation on my part, since I do not know what it is that you are really trying to do.

                    Ok, here is the setup i want.

                    I have 5 different sites that I want to include one php file with functions in it on a different site.

                    test1.com
                    test2.com
                    test3.com
                    test4.com
                    test5.com

                    i want them all to include this file

                    test.com/include.php

                    with a function like this

                    function pField($i){
                        echo array[$i]."asdfasdfadsf";
                    }

                    //****************

                    now the array would be created in the php files on the test1-5.com sites
                    in the test1-5.com sites i create and array

                    include "http://www.test.com/include.php";
                    $a = new array("test","test2");
                    $b = new array(pField(0),pField(1));
                    

                    i would like it this way so when i update the one include file all the other sites will be updated as well.

                    include functions from a php file located on a different domain // as simple as i can put it

                      include.php:

                      <?php
                      // stuff to create array $foobar, then...
                      echo serialize($foobar);
                      

                      test1.com/example.php:

                      <?php
                      $b = unserialize(trim(file_get_contents("http://www.test.com/include.php")));
                      // $b is now same array as $foobar in the include script
                      
                        Write a Reply...