Put site path in a PHP include

0

How to put the whole site path in an include? For example:

<?php
include "http://www.site.com.br/include/categorias.php";
?>

Can you do something similar?

    
asked by anonymous 29.09.2014 / 16:12

3 answers

5

If you want to run a remote PHP in your code:

Warning: this is a danger, for a number of reasons, but there it goes:

To make a include "true" with PHP, the directive allow_url_include must be on in your php.ini, as well as the directive allow_url_fopen .


If you only want the contents of the result of the remote URL:

If the allow_url_fopen directive is enabled, you can do an HTTP request in a very simple, as if the URL were a local file:

$arquivo = fopen( 'http://www.exemplo.com.br/', 'r' );
if ( $arquivo ) {
   $resultado = '';
   // As 3 linhas abaixo podem ser substituidas por essa no PHP 5+
   // $resultado = stream_get_contents( $arquivo );
   while (!feof( $arquivo ) ) {
      $resultado .= fread( $arquivo , 8192 );
   }
}
fclose($file);

Alternatively, if your PHP has cURL installed, as @KaduAmaral commented, you can do something like this:

<?php
   $curl= curl_init();
   curl_setopt( $curl, CURLOPT_URL, 'http://www.exemplo.com.br/' );
   curl_setopt( $curl, CURLOPT_HEADER, false ); // para não retornar os Headers
   curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true ); // para retornar na variável
   $resultado = curl_exec( $curl );
   curl_close( $curl );

   // Processe o resultado para formatar adequadamente
   // e/ou extrair apenas as partes desejadas.
   echo $resultado;
?>
    
29.09.2014 / 16:28
2

It is not possible to do include or require of files outside of the same site file structure in the default php configuration.

Your website should have a directory structure that looks like this if you want to run categorias.php

.
|_ index.php
|_ categorias.php

In your file index.php make include only categorias.php

<?php

include "categorias.php";

echo getCategorias();

And your categorias.php

<?php

function getCategorias(){
    return "Minha Lista de Categorias";
}

If you only want to see the return of the file categorias.php it is possible that it is on an external site, but in the file index.php it will have the same return as if it were executed in the browser.

There are two approaches, such as curl according to @Bacco's response or simplest using get_file_contents

Note: It is important to remember that the php configuration allow_url_fopen should also be enabled.

categorias.php on external site:

<?php

function getCategorias(){
    return "Minha Lista de Categorias";
}

// Aqui ele faz essa saída no browser
echo getCategorias();

Your new index.php

<?php

// Include não vai funcionar
//include "http://www.site.com.br/include/categorias.php";

// Muito menos essa função
//echo getCategorias();

// Aqui você faz a requisição
echo get_file_contents("http://www.site.com.br/include/categorias.php");
    
29.09.2014 / 16:39
1

If you trust the source the best option will be

<iframe align=top width='135' height='60' marginwidth=0 marginheight=0 
hspace=0 vspace=0 frameborder=0 scrolling=no 
src='http://www.site.com.br/include/categorias.php'> 
</iframe>

where you can adjust width in width and height in height.

When I'm talking about "trust the source" I'm talking about the third-party site and not the code.

At the request of @brasiofilo I did some research and I came to the conclusion that they talk a lot about it but do not say anything concrete so I can only give my logical opinion and not a practical explanation because I am not a programmer.

I saw that it was a code developed by Microsoft in 1994 and that it was almost in disuse because it is a proprietary code.

My opinion:

When you insert a iframe from a third page, our page is at the mercy and consequently our users of what is injected in this page and since we have no control over it we can only remove iframe when we realize , so my call to "If you trust the source"

    
29.09.2014 / 16:35