Put site path in a PHP include

Asked

Viewed 3,063 times

0

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

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

You can do something like that?

  • Your example does not appear

  • I don’t think it justifies negativizing something that can be fixed with a simple edit...

  • 1

    What exactly do you want to do? Run the script from another server on your?

  • this example is fictitious!

  • I want to include a small code in php on two pages...product page and product details.

  • If you want to run the script on your server, this is not possible, but if you want the result, you can use the curl.

  • @user3081 you want php to run this file or simply get the return of "http://www.site.com.br/include/categorias.php" ?

  • receive the return of this page of categories, because if I am going to do a maintenance in the future, do not need to change two pages, and only this page of categories

Show 3 more comments

3 answers

5

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

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

To make a include "for real" with PHP, the directive allow_url_include needs to be on in your php.ini, as well as the directive allow_url_fopen.


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

If the directive allow_url_fopen is enabled, you can make an HTTP request very simple, as if the URL was 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;
?>

2

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

Your website should have a directory structure similar to this if you want to perform categorias.php

.
|_ index.php
|_ categorias.php

In your file index.php do the include only of categorias.php

<?php

include "categorias.php";

echo getCategorias();

And his categorias.php

<?php

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

If you just want to display the return of the file categorias.php it is possible that it is on an external website, but in the archive index.php it will have the same return as if it were run in the browser.

There are two approaches, such as curl as per the @Bacco or Simplestente response using get_file_contents

Note: it is important to remember that the php configuration allow_url_fopen shall also be entitled.

categorias.php on the external website:

<?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");

1

If you trust the source the best option is

<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 set width in width and height in height.

When I’m talking about "trust the source," I’m talking about the third-party website and not the code.

At @brasofilo’s request I did some research and I came to the conclusion that talk a lot about the subject 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 was almost to fall into disuse for being a code with proprietary.

My opinion:

When you insert a iframe of a third party page our page is at the mercy and therefore our users of what is injected into that page and as we have no control over it we can only remove the iframe when we realize, so my reminder to "If you trust the source"

  • It would be nice if you put a link that explains the question of "trusting the source".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.