I am trying to keep my PHP code on one server, while calling the functions from a separate server.
Server 1
<?php
echo 'Server 1';
function testingStuff(){
echo 'Testing Stuff';
}
function testingStuff2(){
return "Testing Stuff 2";
}
?>
Server 2
<?php
include 'fullURLtoServer1.php';
testingStuff();
echo testingStuff2();
?>
I know the include statement is working, as "Server 1" is being properly echoed to the screen, but neither of the function calls displays anything. Am I missing something? Why do neither function calls work?
EDIT 1
The ideal situation would be having a single .php file on Server 1 that contains multiple functions, which I can call as often as I'd like.
When you "include" a remote file, you are included the output of that file.
For server 2, the file you are trying to include just says:
Server 1
If you want to execute remote functions, you can make a different file for each function, and execute the function when the file is called. That way, you can "include" the remote file and show the output. You can replace the "different file for each function" for a single file with a parameter.
Keep in mind, trough, that any user/server/bot can call those functions by simply loading the file from the webserver.
Related
(I'm starting this question over from scratch. If you want to know what the original question was that MrMook attempted to answer, or the revised question that Progman commented on, see the edit history. I have now boiled down the behavior to the barest elements and ask the question anew based on that.)
I have created five files and their entire contents are as follows:
test-forward.php
<?php
require_once('script1.php');
require_once('script2.php');
?>
test-backward.php
<?php
require_once('script2.php');
require_once('script1.php');
?>
script1.php
<?php
SayHello()
?>
script2.php
<?php
function SayHello() {echo 'Hello';}
?>
test-together.php
<?php
SayHello();
function SayHello() {echo 'Hello';}
?>
When I run either test-backward.php or test-together.php, the browser says Hello, and there is nothing added to PHP log file.
When I run test-forward.php, the browser is blank, and the PHP log file reports:
PHP Fatal error: Uncaught Error: Call to undefined function SayHello() in ...\script1.php:2
The script test-together.php shows that a function can be called before it is defined. My understanding is (as MrMook said) that included (or required) scripts are all merged into the script that includes them before the whole thing is processed as one. But if that is true, then test-forward.php should behave the same as test-together.php, but it is not. Instead, it appears that in test-forward.php, script2.php is not being read.
The code above is the full contents of the five files, so you should be able to reproduce this behavior on your system. I'm running PHP 7.2 under Windows 7.
Why is SayHello() not defined when running test-forward.php?
PHP checks the executet file for requried files and just add the content of the required file into the executet file.
For Example if you execute script0.php which contains two required_once. It will first merge the content of both required_once into script0.php and then compile it.
So your executet file will be:
function1();
exit();
function function1() {}
When a script is loaded with require or include the content gets executed at the time when the require or include statement is called. When the script is trying to access a function which isn't defined (yet), you will get the generated error message.
I have 2 pages :
functions.php
function get_test($name) {
return 'Yo '.$name.' !';
}
test.php
include('http://www.exemple.com/functions.php');
echo get_test(Thomas);
When I execute my script on the server :
Fatal error: Call to undefined function get_test() in /htdocs/test.php on line 5
Note: I don't use Wordpress
Thanks
Don't do this:
include('http://www.exemple.com/functions.php');
It causes PHP to issue a full-blown HTTP request to your own server, causing Apache to EXECUTE that script, and return its output. That means you're not getting PHP code. You're getting the OUTPUT of that php code, which is not likely to be valid PHP code.
include/require via HTTP is almost always a sign of bad design, and also a major security vulnerability, if you're include/requiring from an actual remote url. Nothing says that the remote url can't return something like <?php system('rm -rf /'); ?>, which your server will then happily start executing.
Almost certainly you only need something like this:
include('functions.php');
I have a primary "driver" script written in PHP, and based on certain criteria, I'd like this script to pull code from one or more supporting servers via HTTP in order to load functions into memory as though I had done so using the PHP include statement.
Is it possible to use HTTP requests in PHP to pull data that is actually interpreted as PHP code when it is returned?
For example, suppose I used cURL or a web service to return the following text that was stored in a variable, say $URLResponse:
function userKeyGet() {
return (isset($_SESSION['user_key']) ? $_SESSION['user_key'] : '-1');
}
If I then used something such as eval($URLResponse), would that create the function for use during the current execution of the calling PHP script? I've used cURL and Buzz to return JSON or similarly structured data that I've converted into an array, but not a function or class. Is this possible? Thanks.
You can load remote PHP codes with include(),include_once(),require() and require_once() functions, It requires allow_url_include enabled in php.ini.
require_once("http://www.yourserver.com/function.php");
Included file should contain codes and not interpreted by server as executable, so if you are using php supported web server maybe you can give another extension to remote file.
eval() will work too. If you use eval/include and declare same function two times it will raise fatal error since it is already declared. You can use object or anonymous functions to override the function.
$code="function userKeyGet() {
return (isset(\$_SESSION['user_key']) ? \$_SESSION['user_key'] : '-1');
}";
eval($code);
eval($code); # Second time using eval on $code with "Fatal error: Cannot redeclare"
# This will work
echo userKeyGet();
# An example for anonymous function way
$code="\$userKeyGet = function() {
return (isset(\$_SESSION['user_key']) ? \$_SESSION['user_key'] : '-1');
};";
eval($code); # this wont raise redeclare error
echo $userKeyGet();
I'm trying to include a remote file from one of LAN pcs using include, allow_url_fopen = On and allow_url_include = On.
One local PC (let's say pc2), I have remote.php, which contains:
<?php
echo $var_on_pc1; // this doesn't output
$remote_var = 'Var on pc2';
function square($num){
return $num * $num;
}
?>
In my PC (let's say pc1), I have test.php, which consists of this:
<?php
$var_on_pc1 = 'Var on pc1';
include "http://pc2/path/to/remote.php";
echo $remote_var; // this doesn't output
echo square(4); // this got error
?>
When I run the script test.php, i got the error:
"Fatal error: Call to undefined function: square() in
path/to/test.php on line 7.
What happened? I thought I could call the included functions and variables and vice versa?
If I cannot implement this, what is the best way?
I have no security concern because I use this locally for temporary development.
Type http://pc2/path/to/remote.php into your browser and see what you get. PHP gets exactly the same.
If the PHP file is being processed by the web server at pc2, you likely get zilch in that file, because the code as been processed. You'd need to configure the other server to not process the PHP file and serve its raw source code instead.
This is not a good idea overall.
I'd like to include the results of a given script (let's say a.php) into another script (let's say b.php).
Although there is the PHP include statement and their other counterparts (include_once, require and require_once), it seems all of them include the file contents instead of its processing results as I would expect.
So how can I get a.php processed first and then include its results (not the file contents itself) into b.php script ?
On the other hand, if my included file (a.php) has a return statement, is the entire file expected to be processed first or its contents are also included into b.php as is ?
Thanks in advance for your replies.
a.php:
<?php
echo "world";
b.php:
<?php
ob_start();
include("a.php");
$a= ob_get_clean();
echo "Hello, $a!";
perhaps?
Sounds like you want to use the backtick operator followed by eval.
Something like this:
$code = `/path/to/myscript.php`;
eval($code);
If myscript.php isn't executable you'll have to prefix it with php, as in:
$code = `php /path/to/myscript.php`;
Note that you need to completely trust myscript.php, otherwise this is an security problem.
This probably isn't a very good design, you might want to think hard about whether or not there's a better way to do what you want.
If a.php is accessible via the web, you could use file_get_contents with an http:// (or https://) URL. This will access your script like an ordinary web page, having your web server process it through PHP and return the results.
a.php:
<?php
echo 2+2;
b.php:
<?php
echo file_get_contents('http://example.com/a.php');
Output:
4
You can also use output buffering, as mentioned in DaveRandom's comment above. That said, this is generally not exemplary application design. It's preferred to have code that will return the results you need rather than echo them. So maybe something like this:
a.php:
<?php
function calculateA()
{
return 2+2;
}
b.php:
<?php
include('a.php');
echo calculateA();
Output:
4
Edit: Several alternatives are nicely detailed on the include page in the PHP documentation.
Summary:
If php file belongs to your server:
ob_start();
$result = eval(file_get_contents('/path/to/your/script.php'));
ob_end_clean();
If not, but it's accessible via HTTP:
$result = file_get_contents('http://path.to/your/script.php');