0

I've looked around for an answer and hopefully I didn't miss it or am not repeating it but here it goes. I have two files:

/var/www/index.php

/var/www/include/php/header.php

Index includes the header.php file. I want the header file to be able to traverse up two levels so I could include /include/css/index.css. Here's the problem, The header is also included by files such as

/var/www/sub/page.php

so I cannot simply use ../../ since location of the php file changes. I tried using $_SERVER['PHP_SELF'] but that changes depending on the location of the php file that includes the header file. I also tried dirname(__FILE__) but that gives me the /var/www/ part which cause complications for including.

Essentially I would like the value of $_SERVER['PHP_SELF'] from the included header.php file and not from the php script that includes it. I want the site to be modular so it can be posted anywhere without having to worry about file locations getting messed up. Is there an easier way to do this?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Cody
  • 578
  • 3
  • 13
  • Are you including your `CSS` as an HTML include? Or combining using PHP `include` into the document? Because you would do a different approach... – Jakub Apr 10 '13 at 17:33
  • As an HTML include. My original idea was to have those includes prepended with the root folder absolute path so that if this website was moved onto different levels it wouldn't be effected. – Cody Apr 10 '13 at 17:42
  • Check this answer to get an Url from a path http://stackoverflow.com/a/36101073/3626097 – fallinov Jun 17 '16 at 12:35

2 Answers2

2

Think you need use __DIR__ constant to be able include files relatively from any file. But better I think define some constant for you root file-system path, like

define( 'FS_ROOT', 'root path here' );

and when include files relatively by this constant. Also it will allow to you add constants for each used important folder and in case to change paths you will need just change some constants values

  • The problem with __dir__ is that it gives you the disk absolute path and not the web absolute path which causes problems when I am trying to include my css. – Cody Apr 10 '13 at 17:51
  • For .CSS ant other URL's just add one more constant for base URL + same constants for deeper levels. And not forget about – Павел Осипов Apr 10 '13 at 19:02
  • The base tag works amazing that's exactly what I wanted. Now I just need the equivalent of that for php. – Cody Apr 10 '13 at 19:47
1

Based on your comment, that it is an include for CSS you basically don't worry about WHERE your PHP file is, just do it from the servers perspective (web side):

<link rel="stylesheet" type="text/css" href="/include/css/index.css">

For a file in /include/css folder of your app (web exposed)

Update:

Per your updated comment, you should store a variable for your app (config), and call a function like CodeIgniter does:

$domain_url= "http://".$_SERVER['HTTP_HOST'];
$domain_url.= tr_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);

function base_url($url){
    return $domain_url + $url;
}

echo base_url("include/css/index.css");

Ref: http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html

Jakub
  • 20,418
  • 8
  • 65
  • 92
  • The issues is my website is not necessarily at the web root so it could be /mywebsite/include/css/index.css and I don't want to have to program that /mywebsite/ part into it I want it to just go two levels up from where that header file is to look for includes. – Cody Apr 10 '13 at 17:47
  • Then you need to do something like CodeIgniter's `base_url()` where you define the root URL (http://domain.com/subsite/) and use that fn in your code prefixing your CSS resource. – Jakub Apr 10 '13 at 17:51
  • Thanks for the update. I thought about doing this I just wondered if there was an automatic way of doing it instead of having to type in the string myself. – Cody Apr 10 '13 at 17:57
  • Updated my answer, however this has constraints, CLI won't work, etc; I've seen some issues with the automated way, good for testing but for PROD I would keep it static – Jakub Apr 10 '13 at 18:01