Lacrifilm Lacrifilm - 1 month ago 8
PHP Question

Receive PHP code with an POST request

I need to send a POST request to another file called

global.php
, for this I try this code below:

$url = 'global.php';
$data = array('stack' => 'overflow');

$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);

$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);

var_dump($result);


This is the
global.php
file that should process the request:

if(isset($_POST['stack'])){
echo 'exists';
}else{
echo 'error';
}


The problem is that instead of the command
var_dump ($ result);
show exists, it shows the PHP code? how can I solve this problem?

And why when I try to do the same thing using
ajax
it returns me the text exists and not PHP code?

Answer

You should use full url, to process php file through server.

$url = 'http://YOURURL.com/global.php';

AJAX call is made from browser, to absolute URL, thats why You are getting desired response.