How to access a file on my local server from a program residing on my own server?

StackOverflow https://stackoverflow.com/questions/23140710

  •  05-07-2023
  •  | 
  •  

Question

I need to run a localhost php file (without uploading it to my website).

I have a php file calld myfile.php on my website. Here is the code.

 <?php
    include "local_file.php";
 ?>

Is there any way to make this program work such that I store local_file.php on my local server(localhost), and still access it using the above code(myfile.php) which is stored on my website?

Était-ce utile?

La solution

You can use PHP's eval() function and load code as plaintext through HTTP (I assume that you have: installed HTTP server, public IP address, redirected ports on your router).

$code = file_get_contents("http://your_public_ip/local_code.txt");
eval($code);

Remember to use your public IP and store PHP code in file with other extension, so that code won't be executed on localhost.

Generally, I strongly discourage executing code from remote location, as it is a big security hole. Consider whether it is needed to execute included code. If you just want to include HTML code or some data - use file_get_contents() and print it usingecho.

More: http://www.php.net/manual/en/function.eval.php

Autres conseils

I believe you can, you have to disable a security feature in php that allows only local includes. Then you will need to have a static IP or dynamic dns setup to point to your local. Your local will then need to be setup to share the file. I don't recommend this though as it opens a lot of potential security holes. Here is some more info:

including php file from another server with php

The security setting you are looking for is: allow_url_include http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-include

If you are going to go this route I would highly recommend blocking all connections to your localhost other than a single white listed IP address.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top