Php include внешний php файл

including php file from another server with php

I have two PHP files located on different servers, one at http://www.mysite.com/main.php , the other at http://www.sample.com/includeThis.php . I want to include the second file from the first one. The content of the second file looks like this:

Just searched on Google to arrive here. What if the other server is in the intranet of the same server? This could be very useful for code reusing at a corporate level and PHP native load balancing.

what about the use of API uploaded on server 1 and than use that API in your php code on other server.

6 Answers 6

Nope, this setting is disabled/not allowed by default in most web servers (php.ini) so you can not use the include to include the files from a remote addresss for security reasons.

If you still want to allow inclusion of remote files, the directive allow_url_include must be set to On in php.ini

But again it is a bad practice, in a security-oriented point of view ; and, so, it is generally disabled (I’ve never seen it enabled, actually)

If you want to read the contents of a remote file though, you can use the file_get_contents function instead BUT this will be returned as pure HTML markup code, there won’t be any server-side code.

Yes, you could use file_get_contents and than file_put_contents to store it on your server, and than use include on it. Though it’s a dangerous and risky thing to do IMO. But possible. And you can even extract certain code from it also and write only the code that you need from the file.

I have the permissions to play with php.ini I have already enabled the allow_url_include. but when I use the mentioned method. I get nothing in the variable. means the variable is null. I dont know how to pass it to the main.php I’m using this method to prevent people from cracking my scripts. so there will be part of the script on my server. thats why I need to use this method

if you allow_url_include, I need to know why is risky and not good practice. Because I need to stablish a system where I can make Php sheets with license expiration date.

Читайте также:  Openserver обновить версию php

After reading your comments — in which you state that you want to do this as a means of copy protection — my answer is an emphatical, forget it. This is not how copy protection works.

The only thing you can do using include() is fetch source code from elsewhere to be interpreted on the local interpreter. This is childishly easy to crack: A malicious customer would to just have to echo() the fetched code.

Executing the remote script remotely (on your server) won’t help you, because the state of that script (variables, functions. ) won’t be present in the script you call it from.

  • Compiling / encoding / obfuscating the script, possibly requiring a specific PHP module to execute it (lots of questions about this on SO)
  • Creating a real web service (e.g. using SOAP) that runs on your server, and performs the requested operations

For what it’s worth, though, I personally do not purchase, nor recommend to clients to purchase, encoded scripts and scripts that need to «phone home» in order to work. I believe in protecting your products through a stringent license agreement (that will scare business customers into buying your product, because the risks of getting caught stealing are too expensive.)

Источник

include external php files

I am using some php code like database connection which is common for all the pages, so I created a php file which contains the php code and then I am including this file in my HTML code, So I want to know the better way to include php file, better alternative for include function. my example code is here

i always use require_once if the type of file is as critical as you have specified. Though, i must say that its my preference. Otherwise, it depends on situation.

2 Answers 2

You have 4 options to choose from.

include ‘yourfile.php’;
include_once ‘yourfile.php’;
require ‘yourfile.php’;
require_once ‘yourfile.php’;

of course you can also use » instead of ‘.

they all will do the same thing except minor differences.

if yourfile.php does not exist, the include ones will ignore that fact and your php page will move on — without any fatal errors.

Читайте также:  Base64 encoding for html

require ones on the other hand will create fatal error.

if you know that that file is there, it makes no difference as to which one you pick.

As to the options with the _once postfix, well, they tend to be slower compared to their none _once postfixed counterparts. Cause when you use the include_once or the require_once, PHP will do some extra work to make sure that those files are truly included ONCE — protecting you from a possible double include situation if you carelessly code and many files use many includes and you may run into situations where the same file gets included twice. Well, _once option will prevent that. And that checking will come with some processing cost.

I also noticed you’ve used » as opposed to ‘ for your file delimiters. There is no reason to choose » over ‘ unless you will be referring to variable names in your files such as

$thefile = ‘yourfile.php; include «$thefile»;

so which ones to pick out of these 4? it all depends, if you think you do need to force the _once issue, then you pick either the include_once or require_once, and if you think that’s not needed, then you go with the include or require. As to include vs require, it all comes down to would you like your PHP script die or move on if yourfile is for some reason not accessible.

If you are curious about some speed tests, here is a link for you to check out. http://php.net/manual/en/function.require-once.php

I also found this on the subject matter.

Источник

How do I include an external file in PHP?

I need to include a file (belonging to another server) inside a php web page. More precisely : let us assume I have this web page : http://www.example.com/mypage.html and when viewed in a browser it displays : Hello World ! I want to include this contents (not the source): Hello World ! inside my php page (assume on www.myexampleserver.com). What should I do ? I tried : include «http://www.example.com/mypage.html»; but it does not work. I would also like to be able to do something like this. On my php page source : echo «My other page contains «.SomeCodeThatIDontKnow.» and that is all»; and display when viewed in a browser : My other page contains Hello World ! and that is all Thanks for any indication.

Читайте также:  Long type in php

4 Answers 4

$otherpage = file_get_contents("http://www.example.com/mypage.html"); echo "My other page contains: " . $otherpage; 

If you expect the «other page» to only contain text, you might also want to use htmlentities to ensure there’s no HTML injected into your page..

$otherpage = file_get_contents("http://www.example.com/mypage.html"); echo "My other page contains: " . htmlentities($otherpage); 

You can also use file_get_contents() or file() for this.

But don’t use include/require.

If you want to do something you should load the webpage and start actions through the content. Here an example:

The remote webpage returns the content «do-something». So you can check whether the remote file returns this or another phrase and start an function:

$command = file_get_contents('http://remote-domain.tld/file.html'); if($command == 'do-login') < //. do something >. 

If you want to display this text in your browser look at iframe tag.

I’ve done this on my sites. I manage 3 internal websites which are related so I often share code (and complete source files) between them.

If you don’t want to include the source of the other file, you could make the content portion of that file a separate file and then include that in both files.

EG:
www.example.com/ContentSource.php:
hello world and other content.

www.example.com/mypage.php:
source code
include «www.example.com/Contentsource.php»;
remainder of source

www.myexampleserver.com/otherpage.php:
source code
My other page contains
include «www.example.com/Contentsource.php»;
and that is all.
remainder of source

Then you just manage the content you want displayed in both spots in the one file (ContentSource.php) and include it where you want.

One bank of servers I work on uses MySQL and the other Oracle. On my MySQL servers, I’m including a file located on the Oracle server that displays a table of codes applicable to both websites, but is only available in the Oracle database tables. The Oracle server has the php that creates the html and I just include that in my MySQL server web page — voila — information from another server that I did not have to manually re-create.

If both sites are managed by same person/company, I see no reason why include cannot be used.

Источник

Оцените статью