Why does the require_once function work on the localhost but does not work on the server?

Asked

Viewed 851 times

1

Dear I have a framework that accesses data from other servers, I programmed the entire system on localhost, on my machine. However, when playing for the server the external page call does not work, for example:

require_once("http://meusite.com.br/pagina.php");   

I have already allow_url_include = On and allow_url_fopen = On, but it doesn’t work.

Errors appear: Warning: require_once('http://meusite.com.br/pagina.php'): failed to open stream: Connection timed out in /var/www/pagina.php on line 231 Fatal error: require_once(): Failed Opening required 'http://meusite.com.br/pagina.php' (include_path='.: /usr/share/php:/usr/share/Pear') in /var/www/i9/page on line 231

Detail: Although I wanted to include pages from other servers, the server I refer to "meusite.com.br" is the server itself where the "page.php" is. Could that be the problem? That instead of using require_once through url, I am forced to user the way DIR?

Will I need to use file_gets_content()?

  • Timeout on production server.

  • Even file_gets_content() gives timeout, which will be, which variable should I set on the server?

  • 1

    Are you trying to include a page from within the same server? Or are you on another?

  • That’s inside the same server!

  • Try using this: <?php&#xA;define('__ROOT__', dirname(dirname(__FILE__))); &#xA;require_once(__ROOT__.'/config.php'); &#xA;?>

  • @Is there any property that blocks this from making any kind of reference to the server itself? Because I gave a Url on an external site and it worked, and even from my localhost to the production server it responds with the pages. But when I threw it to himself it doesn’t work anymore.

  • 2

    There are many things that may be causing the problem, but none of them are related to your code or PHP configuration. The error message clearly indicates that the request failed by timeout, so there is something blocking the connection, just take a paper and pen and write all the route your server will have to do to connect correctly to this URL, the error will be in one of the points. The most common is that some firewall is blocking the connection, including, if you use Cloudflare in the domain reported in include, it is most likely the reason for the problem.

  • @Claydersonferreira I also think that’s the problem I’ll check.

  • 2

    Okay, and one more thing. If you don’t have a very reasonable reason to be using include via URL (since as you said, the file to be included is on the same server), it is best to use the conventional method per directory. The reason is very simple and obvious. But... however, if you set your server to respond to the domain internally (without having to solve the DNS), it already reduces the problem a little.

  • Guys, the problem now is that passing parameters in require_once (even using the local directory) gives error. "require_once(ROOT."/i9tax_new/teste.php? group=". $_GET['group']." &idUsuario=". $_SESSION['i9_usuario']);" Not allowed to pass parameters? Locally I managed to do this on a PHP server (XAMPP WINDOWS) but on the production server that is LINUX I cannot, some parameter to adjust?

  • Warning: require_once(/var/www/i9tax_new/teste.php?group=731db9563f89bd76f55f9619b61d3da4&idUsuario=400379d4eaa6ec24826dd44cd5cf65a3): failed to open stream: No such file or directory in /var/www/i9/principal.php on line 238 Fatal error: require_once(): Failed Opening required '/var/www/i9tax_new/teste.php? group=731db9563f89bd76f55f9619b61d3da4&idUsuario=400379d4eaa6ec24826dd44cd5cf65a3' (include_path='.:/usr/share/php:/usr/share/Pear') in /var/www/i9/principal.php on line 238

  • 2

    require_once with parameters? Restructure your code so that testing.php only has functions/classes, require_once in this file without parameter, and anywhere in your code call the functions/classes.

  • Verdade @Andrélfsbacci great idea, thanks! I hadn’t thought about it, that tosco kk, is that I migrated a platform to another there many concepts I tried to maintain and I did not connect in the orientation to objects.

  • 1

    Thank you all, I did it! I will do the following: 1 - flag the platform modules to see if they are internal modules (from the server itself) or if they are third party server modules (other programmers other servers), if it is internal it uses the ROOT if it is external it uses require_once on the third party address server.

  • Guys, I have a similar problem, this time with regard to a page simulation ". html", I wanted to use the function to fetch the contents of the page file_get_contents("http://meusite.com.br/pagina.html") that is on my own server, but I don’t want only the code of the page I want it to be "processed by file_get_contents"that simulates the opening of the page by an external user, i.e., the "pegconteudo.php" page is on the same server as "pagina.html" and the same time_out error occurs.

Show 10 more comments

1 answer

2


Actually, most of the time_out problems if they are not due to your apache server settings, might be like in my case.

I managed to solve the problem, the point is that I was pointing the function file_gets_content() to the server itself that runs the application I want. That is, instead of using "localhost" I used "http://meusite.com.br" generating a certain type of looping on the server.

Problem solved!

Browser other questions tagged

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