Include through HTTP - php

I have a file on another server which I can trust since it's my own code that in it there is an array with some settings.
When I include it using the include_once and print_r the variable in it, I am getting an undefined variable.
I also tried to return the variable from the file I am including and assign it to a variable in the script like this:
$var = include($url);
where $url has:
$array = array(1,2,3); return $array;
when I print_r($var) I only get 1.

It's because the other server will parse the file and return the RESULT, not the actual code. you need to get the actual contents of the file, which will only be possible if you disable php on the other server or ftp to it.
I would recommend copying the file over to the server you're working on, safer an easier
The file you are trying to get will come back the same as if you went there in your web browser because the remote web server will parse the contents of the file though the php engine.

remote host:
echo serialize($array);
local host:
$array = unserialize(file_get_contents($url));

Related

Where can I see what print_r display on PHP?

I have a REST API with Android and Slim framework. I'm using XAMPP to connect it with a MySQL database, .
I don't know where print_r is displayed.
The api.php file is in C:\Program Files\xampp\htdocs\project\app\api\api.php.
I get the following message when I try to access localhost/project/app/api/api.php:
Access denied.
It's a message that I put with define function. In index.php:
define("CONSTANT",true);
In connect.php and api.php:
if(!defined("CONSTANT")) die("Access denied");
print_r is outputted to the file whereever it is called. Just check the source of your web page and the output will be there somewhere.
If you need to find it quickly try echoing something unique above the print_r. E.g. echo "I AM OVER HERE!!!";
Let's say you've a page test.php where you're using print_r($stuff);
and I assume that you've installed xampp in C:\Program Files\xampp and your code is inside here C:\Program Files\xampp\htdocs\droidApp
droidApp is folder where this file test.php is residing.
test.php can have
<?php
print_r($stuff_you_want_to_print);
?>
go to this url from your browser
localhost/droidApp/test.php
just make sure your xampp is running before doing that.
Try this, I use this way.
echo "<pre>";
print_r($array);
echo "</pre>";
You can use Rest API extension of chrome:
https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop?hl=en
In that you can hit the url of your service and can see all your print_r and var_dump values.
Finally, testing different options during some hours, I get the solution.
If you are changing three values in your PUT method of your REST API, for example, name, description and idCar, after setting their new values (that you send from your Android application) you can put on your PHP script:
print_r([$name,$description,$idCar]);
Here all it's equals than before. So, how can you see what print_r it's displaying?
After executing your response on your Android application, you can put these two lines:
HttpEntity entity = response.getEntity();
String responseText = EntityUtils.toString(entity);
And then you can put a Log to see what it's displaying:
Log.d("response",responseText);
Now you will be able to see what print_r it's displaying.
print_r prints the specified variable to the screen, similar to the way the echo command does. There's a second argument which is a boolean that is set to false by default. If you set it to true, it will allow you to store the contents in a variable rather than print it to screen.
Thus,
print_r($_GET)
is the same as
$inputs = print_r($_GET,true);
echo $inputs;
See http://php.net/manual/en/function.print-r.php for more information.

Get .php file locally using curl

I'm using curl in PHP to return the content of a PHP file. I want to do this locally because I will be accessing multiple PHP files during the same script, so it would be faster to open the file directly.
However, I want to be able to push parameters into the PHP files (treat them exactly as PHP files on the web, but grabbing them locally), as I want to push parameters into the scripts which will be generating additional dynamic content when I grab it.
Is this possible to do if I call the files locally? I've tried using the file:///, calling the file directly, but this won't run the PHP code found in these files.
Any ideas?
edit
Sorry for the confusion:
-This is currently running on a webserver, and I am currently calling http:// (and not file:///) so the PHP contained in those files can be executed. However, I find this to be slow because I'm generating multiple curl() calls that are essentially calling the server itself multiple times.
you can do trick like:
function php_to_string($php_file, $new_GET = false, $new_POST = false) {
// replacing $_GET, $_POST if necessary
if($new_GET) {
$old_GET = $_GET;
$_GET = $new_GET;
}
if($new_POST) {
$old_POST = $_POST;
$_POST = $new_POST;
}
ob_start();
include($php_file);
// restoring $_GET, $_POST if necessary
if(isset($old_GET)) {
$_GET = $old_GET;
}
if(isset($old_POST)) {
$_POST = $old_POST;
}
return ob_get_clean();
}
$content = php_to_string('my_file.php');
$content = php_to_string('my_file.php', Array('id'=>23)); // http://localhost/my_fie.php?id=23
But please mind it may overwrite your existing variables, causing bugs (for example duplicate defines) etc. so you may use sandbox solution
I believe you would want to set up a web server (e.g. Apache) on your local machine so you can go to http://localhost/script.php?param1=foo&param2=bar instead of file:///path/to/script.php. The difference is that when you do file:///, the files are just opened, but if you go through Apache, the scripts are actually executed.
As for passing arguments to your scripts, use the query string for that (e.g. ?param1=foo, etc.).
I don't know why you're doing what you're doing, but hopefully that helps you do it.

How to load the result of a php function into a variable

I have a php file on my server that takes in two inputs through the URL and then comes back with a result. When a page is loaded, I'd like to have the result of that calculation already loaded. For example:
$var = load("http://mysite.com/myfile.php?&var1=var1&var2=var2");
I know that load isn't a real function for this, but is there something simple that suits what I'm looking for? thanks
Use file_get_contents
$foo = file_get_contents('http://mysite.com/myfile.php?&var1=var1&var2=var2');
Or, a better solution if the file is located on your server:
include('myfile.php');
and either set the $_GET variables in the included script itself, or prior to including it.
If they are running on the same server, consider calling the script directly?
$_GET["var1"] = "var1";
$_GET["var2"] = "var2";
include "myfile.php";
You could use file_get_contents, but it may be a more practical solution to simply include the file and call the function directly in the file, rather than trying to manually load the file.

Include external class php

How can include a external class in a php file?
example:
//Test.class.php
<?php
class Test{
function print($param){
echo $param;
}
}
?>
//######################################################
//test.php
<?php
include('http://www.test.com/Test.class.php');
$obj = new Test();
echo $obj->print("hola");
?>
The class is on another server. I have enabled the allow_url_include and allow_url_fopen.
Why can't I call the function print?
The remote file must output the php source code, not execute it.
To output the PHP code instead of executing you could simply remove the .php extension from the file.
PS: Are you really, really, really sure you need remote inclusion? It's a BIG security risk!
What you're including from the other server isn't the code behind the PHP but the output from it (if you visit that page in a browser you aren't seeing the PHP code if you view source right?)
You either need to reconfigure the other server not to execute the code but display it (not a good idea if it's in any way shared or needs to execute it's own code), or rename the other file to something that isn't first interpretted (try otherfile.php.txt)
Have a look at the documentation:
(...) 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.
Probably the server, you are trying to get the file from, executes the PHP file and only the result (which is empty) is included. You would have to configure the server in such a way that it outputs the PHP code. But this is not a good idea if it is sensible code.

Problem with AJAX and PHP

I have a small problem, I want to load data from a PHP file and put them on a DIV.
Here's the Jquery code
// Store the username in a variable
var jq_username = $("#txt_checkuser").val();
// Prepare the link variable
var link = 'user.php?action=check&username=' + jq_username;
$('div #checkuser_hint').load(link);
So it works! but instead of loading the result (compiled PHP) it loads the PHP code.
If I write the long URL "http://localhost/project..." it doesn't load anything!
Any idea how to do that?
I think you might be accessing your javascript file as a file on your local filesystem, a request to the same directory would go through the filesystem and not through your webserver, processing the PHP into the desired output. This also explains why http://localhost/project for the AJAX call doesn't work: Javascript might be enforcing the same-origin policy on you.
Verify that you're actually accessing this javascript file through http://localhost/ (as opposed to something like file://C:/My PHP Files/ ).
Does the page return anything when you use your browser?
Are you sure it should not be 'div#checkuser_hint' instead of 'div #checkuser_hint' ?
And this looks like the correct way according to the documentation.
var link = 'user.php';
$('div#checkuser_hint').load(link, {'action':'check', 'username':jq_username});
Are you able to access the script manually on your own? (try accessing it via your browser: htp://localhost/...) It may be the case that you're missing your opening <?php and/or closing ?> in the script-file itself.

Categories