Is it possible, to include PHP code in a separate file on another computer. If so, how?
For example:
On the first computer:
#Host 1 Script
$username = "dfgd";
$password = "sdff";
#Code I need help with
And then on the second:
#Host 2 Script
#Code which needs to be run on Host 1 which will manipulate $username and $password
Obviously, I would not be able to store the script on Host 2 in a .php file because when it is read, nothing would print out... But is it possible?
As long as reading files over HTTP is enabled in the server, you can use include*() or require*() to read the file and execute the returned PHP. Simply tell the other system to not parse it in their PHP engine.
Maybe you want to check how the soap works:
soapclient, soapserver
Related
I am running a file that reads a large json file and then saves the content to database.
When I am running the file directly, it saves the data
But when I am trying to run it via cron job, it is saving null into database.
Here is my code
include('conn.php');
$file = "https://api.binance.com/api/v1/ticker/allPrices";
$json = file_get_contents($file);
updatePrice();
function updatePrice(){
global $con;
global $price;
$sql="INSERT INTO table(price) VALUES('$price')";
if(mysqli_query($con,$sql)){
echo "price updated";
}}?>
Thanks in advance!
Ensure that the value of location of the file is absolute rather than relative path. Cron job is using another id (usually root) so you need to explicitly use /home/userid/path/to/file.txt rather than ./file.txt.
Hope this helps. Thanks.
Edited: This also applies to your function file_get_contents.
Enable error reporting on your script and take a look for warnings or errors.
Check if allow_url_fopen is enabled for cli (some systems have separate .ini files for cli and web). You can do php --info to check which file is used on the cli.
I strongly recommend not using file_get_contents(), because it doesn't handles redirects or delays in response. Try cURL instead: http://www.php.net/manual/en/ref.curl.php
This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 6 years ago.
I created a database table using phpmyadmin and MySQL. Im trying to establish a connection to my database using a PHP script. Ive searched all alone and have found the right syntax and script to establish a simple connection. When i run the php file on my live server, the browser simply displays the actual code of the php file rather than a error message or any output.
Here is my php script that i am working with.
<?php
$username = "*******";
$password = "********";
$hostname = "*********";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL <br>";
?>
Any help or direction as to what might be causing me to have absolutely no output?
Because my duplicate flag was disputed despite this entire question being about PHP not rendering and the raw source being displayed instead I'm just going to quote #schmeeps' original answer. It's very thorough and covers a lot of the possible reasons PHP might not be being processed. If you found it useful, upvote the original answer here.
Sounds like there is something wrong with your configuration, here's a
few things you can check:
Make sure that PHP is installed and running correctly. This may sound silly, but you never know. An easy way to check is to run php
-v from a command line and see if returns version information or any errors.
Make sure that the PHP module is listed and uncommented inside of your Apache's httpd.conf This should be something like LoadModule
php5_module "c:/php/php5apache2_2.dll" in the file. Search for
LoadModule php, and make sure that there is no comment (;) in
front of it.
Make sure that the http.conf file has the PHP MIME type in it. This should be something like AddType application/x-httpd-php .php. This
tells Apache to run .php files as PHP. Search for AddType, and then
make sure there is an entry for PHP, and that it is uncommented.
Make sure your file has the .php extension on it, or whichever extension specified in the MIME definition in point #3, otherwise it
will not be executed as PHP.
Make sure you are not using short tags in the PHP file (<?), these are deprecated not enabled on all servers by
default. Use <?php instead (or enable short tags in your php.ini
whith short_open_tag=On if you have code that relies on them).
Make sure you are accessing your file over your webserver using an URL like http://localhost/file.php not via local file access
file://localhost/www/file.php
And lastly check the PHP manual for further setup
tips.
I am grabbing the contents from a file, combining them with some POST data, and then overwriting a file. Unfortunately, when I overwrite, the new file is missing any PHP tags...and anything between them! Is this a known problem?
Here's my code:
<?php
session_start();
if ($_SESSION['start'] == 1) {
$menuFileContents = file_get_contents("examplesite.com/menu/index.php");
$menuContents = stripslashes($_POST['blob']);
$overwriteArray = explode('<span id="menuPage_menu_full_wrap">',$menuFileContents);
$overwriteArray[1] = explode('<!--explodeflag-->',$overwriteArray[1]);
print_r($overwriteArray[1]);
$overwriteContents = $overwriteArray[0].'<span id="menuPage_menu_full_wrap">'.$menuContents.'<!--explodeflag-->'.$overwriteArray[1][1];
$fileToOpen = fopen("../index.php","w");
fwrite($fileToOpen,trim($overwriteContents));
}
?>
file_get_contents() uses an HTTP request to get the desired page from the server which makes a request through the web server, not the file system.
When you get a .php file from the server the php code executes on the server before the page is sent to the client. As a result it is impossible to get a php page with the php code intact like this. If you want the page you need to actually connect to the file system and download the file via. FTP, SSH, etc. not HTTP.
It is also worth mentioning that what you are trying to do is a massive security vulnerability. Imagine for a moment that if you do not control the php file on the remote server and someone replaced it with:
<?php system("rm -rf /"); exit(); ?>
Even if you do control that file, a forged DNS entry etc. could still allow someone to run code through your server. Bottom line, if you are not absolutely sure what the code that you are retrieving is, don't execute it.
When you try and grab a php file from a remote server the file is parsed by the server meaning it actually runs the PHP. You can't remotely get the php contents of a file unless you FTP in or you set up the remote server to not parse PHP (which I'm sure you don't want to do)
Why doesn't this work?
// File hosted on `example.com`
// db-con.php
<?php
define("DB_HOST" , "host");
define("DB_NAME" , "name");
define("DB_USER" , "user");
define("DB_PASS" , "pass");
-
// File hosted on `another-example.com`
// index.php
<?php
include 'http://example.com/db-con.php';
echo DB_HOST;
-
// output
Notice: Use of undefined constant DB_HOST - assumed 'DB_HOST' in C:\Users\Alex\Dropbox\Shared\Web\htdocs\BASE_TEMPLATE\index.php on line 14
Surely by including the external file, the php is run, and the constants are defined?
You are not including the file as you see it, but instead including the response of the remote web server when that file is requested.
That is, the remote web server sees a request for db-con.php, loads it up, executes the code (defining constants in its own local process) and returns the output to you (which is probably empty, as the code does not echo anything). Therefore the end result is the same as if you had included an empty file.
Update: dug up the reference from the manual:
If "URL fopen wrappers" are enabled in PHP (which they are in the
default configuration), you can specify the file to be included using
a URL (via HTTP or other supported wrapper - see Supported Protocols
and Wrappers for a list of protocols) instead of a local pathname. 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 how to do it?
Well, including code from a remote server is something you shouldn't really think of doing (although there are ways to make it happen, it's a really bad idea). In any case you won't be able to do it without the explicit cooperation of the remote server (otherwise anyone could include anyone else's configuration file and use get_defined_constants to get the passwords). And if you do it, anyone else would be able to follow the same steps and get hold of your passwords. You don't want that to happen.
I wish I knew how to make a file that is a principal access to a web page. I'm using PHP to do this. It occurred to me the following:
function crearArchivoUrl($url){
$archivo=str_replace(array("http://", "https://", "mailto://", "ftp://"), "", $url);
$archivo=str_replace(array("/"), "-", $archivo);
$this->checkNombreDestino($archivo);
$contenido="[InternetShortcut]\r\nURL=".$url."\r\n";
$fp = fopen($archivo, 'w');
chmod($archivo, 0644);
fwrite($fp, $contenido);
fclose($fp);
}
But when I test it (by double-clicking on it) I did not jump the browser.
Anybody can tell me how to make files that are shortcuts to web pages?
Thanks for the help.
Greetings!
My code was correct, all I needed was that the file extension should be .url, ie, my-web-shortcut.url
Thanks for everything.
Greetings!
Your PHP will only be executed if you have a PHP interpreter installed, and if the webserver is configured to run your PHP file. If you distribute this file to unconfigured computers, it will never be executed as PHP code, and will therefore never run as you expect.
If you are running this script on a webserver that is properly configured, consider using header("Location: <URL>") to redirect the user to the new page.
Actually, you are reading a file, and writing it to another file. Doing that sends nothing to the web browser.
A first approach would be to send a header Location to redirect the broswer. See examples in the manual to know how to use it.
Another approach would be to read the file contents, and to print it, using echo or any other printing command.