-2

I have php code from which i got my domain name and I need to do include header.php(heder part) to my webpage and it is not including. What i do wrong?

It is code for getting domain name

$actual_link = "http://$_SERVER[HTTP_HOST]";

Here is code where i try to include

<?php include $actual_link."/header.php"; ?>

Why it is not working for inlcude, but working for everething except include?

Orik00
  • 73
  • 1
  • 9
  • 1
    Do you have `allow_url_include` enabled? – brombeer Jan 15 '18 at 10:33
  • 1
    Most likely you want to do this via the file system, and not via HTTP. – CBroe Jan 15 '18 at 10:34
  • @kerbholz no, I do not have access to php config files( – Orik00 Jan 15 '18 at 10:36
  • try `required ` function and check the error – Bilal Ahmed Jan 15 '18 at 10:37
  • @CBroe i want to add header for each file and do not care in which folders are files. I thing better o do this with file system than http. Anyway if i write `http://domain.name/header.php` it is also not working – Orik00 Jan 15 '18 at 10:38
  • That's bad, because _"This option allows the use of URL-aware fopen wrappers with the following functions: include, include_once, require, require_once."_ – brombeer Jan 15 '18 at 10:38
  • Why would you make an HTTP request through the internet to your own domain to include a file? Does going to `http:///header.php` in your browser yield the desired file? Don't do that. Use `include 'header.php'` to include *the local file*. – deceze Jan 15 '18 at 10:38
  • @BilalAhmed tried also, no errors, shows me white screen – Orik00 Jan 15 '18 at 10:39
  • _“shows me white screen”_ - then go first of all configure your PHP error reporting properly - https://stackoverflow.com/questions/1475297/phps-white-screen-of-death – CBroe Jan 15 '18 at 10:40
  • @long `__FILE__`?! – deceze Jan 15 '18 at 10:40
  • @deceze but for the rest file i should do `inclued "../header"; `. I want to add one code for all – Orik00 Jan 15 '18 at 10:40
  • Fine, then ask a question particularly about how to get the right local file path. Don't use HTTP as a workaround. – deceze Jan 15 '18 at 10:41
  • @CBroe i have no access to php or apache files( – Orik00 Jan 15 '18 at 10:42
  • _“ i have no access to php or apache files”_ - Then go _get it_. You can not develop “in the dark” like this. – CBroe Jan 15 '18 at 10:44
  • @deceze i have changed name as u wrote. – Orik00 Jan 15 '18 at 10:44
  • @CBroe yes, thats the problem. I can not get access to them and i am looking for another approch – Orik00 Jan 15 '18 at 10:45
  • 1
    If you want a single line of code that is able to locate the file no matter where you call it from, then you should use the full, absolute file system path - use `$_SERVER['DOCUMENT_ROOT']` as prefix to assemble it. – CBroe Jan 15 '18 at 10:48
  • @CBroe it is right answer, add it, i will mark it as right one. But still can not understand why my code is not working – Orik00 Jan 15 '18 at 11:02

1 Answers1

0

To include locally available PHP files, you should go via the file system, instead of using HTTP.

(The result will be different - using the file system, the code of the file will be included; using HTTP, only the output would be included. So for example any variables you define in your header file would not be available later on in the main script, if you included the file via HTTP.)


If you want a single line of code that is able to locate the file no matter where you call it from, then you should use the full, absolute file system path - use $_SERVER['DOCUMENT_ROOT'] as prefix to assemble it.

CBroe
  • 91,630
  • 14
  • 92
  • 150