Include in absolute address

Asked

Viewed 330 times

1

I am trying to include the config.php file that is in the site root folder. However, I also have the header.php file at the root of the site and all the pages are in a folder with this name, so the file is as follows.

<?php
include('../config.php');
include('../header.php'); ?>
conteudo...
</?php include('../footer.php);

For absolute link already tried:

include("http://".$_SERVER['SERVER_NAME']."/config.php");

Error displayed on the online server:

I didn’t succeed either. Warning : include(../config.php): failed to open stream: No such file or directory in /home/u655246247/public_html/pages/articles.php online 2

Line 2: that’s exactly where I’m going with this -> include('.. /config.php');.

1 answer

4


This is probably what you’re looking for:

include( $_SERVER['DOCUMENT_ROOT'] . '/config.php' );

So you have the absolute path of include in relation to the disk, and not to the website URL.

If you are curious to understand, use this line in a test script:

echo $_SERVER['DOCUMENT_ROOT'];

The result will be something like /var/www/httpdocs on Linux, or something like c:\apache\httpdocs on Windows.

Notes:

  • make sure the file name is correct. Config.php and config.php are not the same file on Linux.

  • also make sure that the files involved have read permission to the page server.

  • obviously, the file has to be in the same folder as the given path :)

  • On the localhost it still works perfectly. However, errors persist online, exactly the same. I echo the config.php file to test, and echo was included, but the connection variable is not found. Error: Warning : include(/home/u655246247/public_html/config.php): failed to open stream: No such file or directory in /home/u655246247/public_html/pages/texts.php on line 2

  • I will add the config.php file to the question for notes..

  • @Thiagobarros no use adding, it is not being read. Better include a directory listing public_html, to see permissions and names.

  • Yes, but when I give an echo, the text is displayed on the page... why?

  • When do you give an echo where? Include a list of the public_html directory to see the permissions.

  • You don’t want to kill me, but the file was under another name on the online server.. rsrs After fixing, the online server showed me another error: Warning: include(): http://wrapper is disabled in the server Configuration by allow_url_include=0 in /home/u655246247/public_html/pages/texts.php on line 54

  • @Thiagobarros is because you are using http:// instead of the absolute path. And no, I don’t want to kill anyone, see item 3 that I had already added in the answer :) Remember that when you give include, the absolute path is the HD, not the web. The $_SERVER['DOCUMENT_ROOT']; gives you the root of your site, but in the way of HD.

Show 3 more comments

Browser other questions tagged

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