1

I'm pretty familiar with WordPress, and a fan of this CMS. So I'm used to the get_header() function that includes the header.php file. But now I'm developing a raw PHP project, and I want a similar ease here too. I designed a folder structure like below:

project/
       css/
            bootstrap.css
       js/
            jquery.js
            project.js
       inner-folder/
            some-page.php
       header.php
       index.php
       footer.php
       style.css

I made a custom function is_page() just like WordPress to load page-specific items. Everything's working just fine where my header.php contains:

<html>
   <head>
      <link rel="stylesheet" href="css/bootstrap.min.css">
      <link rel="stylesheet" href="style.css">
   </head>
<body>
...

and my footer.php contains:

      ...
      <script src="js/jquery.js"></script>
      <script src="js/project.js"></script>
   </body>
</html>

And my index.php contains:

<?php require_once( 'header.php' ); ?>
   ...
<?php require_once( 'footer.php' ); ?>

And everything's working just fine, until...
I started working on the inner-folder/. In some-page.php I used the same code like the index.php and you can guess, all the CSS and JS file links get broken.

How can I make a function or something so that regarding anywhere of my project folder (within any sub directory or sub-sub directory) I can load any of my resources without any hamper? And it should be performance-savvy too.

Mayeenul Islam
  • 3,857
  • 5
  • 39
  • 89
  • use `$_SERVER['DOCUMENT_ROOT']."/path/to/header.php"` – iam-decoder Jan 28 '15 at 16:26
  • no this isn't. it's returning `C://wamp/www/` but I want `http://localhost/` instead you know. – Mayeenul Islam Jan 28 '15 at 16:32
  • 1
    do a `var_dump($_SERVER);` to see what data it holds, use something from there, I'm sure something will work for what you're after. but I use $_SERVER['DOCUMENT_ROOT'] in my requires and it works flawlessly. – iam-decoder Jan 28 '15 at 16:40

3 Answers3

0

If you have access to your php.ini file, and assuming you are running Apache 2 and have access to the Apache configuration file, you can edit these files to solve your problem.

Step 1: Editing the Apache configuration

You need to set the Directory and Document Root in your Apache configuration to be the path to your web project folder. I'm assuming this is already done, since you are successfully running PHP. If not, have a look at this question.

Step 2: Editing the PHP configuration

There is a setting in the PHP configuration, php.ini, that allows you to set paths that the PHP engine will look for when including files.

;;;;;;;;;;;;;;;;;;;;;;;;;
; Paths and Directories ;
;;;;;;;;;;;;;;;;;;;;;;;;;

; UNIX: "/path1:/path2"
;include_path = ".:/php/includes"
;
; Windows: "\path1;\path2"
;include_path = ".;c:\php\includes"
;
; PHP's default setting for include_path is ".;/path/to/php/pear"
; http://php.net/include-path

(Chart from this question)

If you don't have access to the PHP configuration or don't want to change it, you can always use the [set_include_path][3] function, which essentially does the same thing, just in the script.

Once you have completed both of these steps, you can include files as follows:

Non-PHP resources such as CSS, JavaScript, etc: /path/to/css

PHP resources: path/to/php

Note the leading slash for non-php resources, and no leading slash for PHP resources.

Community
  • 1
  • 1
samrap
  • 5,245
  • 5
  • 29
  • 55
  • Thanks for your approach. But if the existing settings in my `php.ini` works in WordPress then I want the exact setting for my raw PHP project too. And I want a solution that will also work on live server without any change. – Mayeenul Islam Jan 28 '15 at 17:38
0

Okay, I found a most solid solution: the HTML <base> tag –

<html>
    <head>
        <base href="http://example.com/">
        <link rel="stylesheet" href="css/bootstrap.min.css">
        <link rel="stylesheet" href="style.css">
    </head>
<body>
...

Source:
http://www.w3schools.com/tags/tag_base.asp

Mayeenul Islam
  • 3,857
  • 5
  • 39
  • 89
-1

I had the same problem when using include menu so that i didn't had to change menu on each page.

So i hardcoded the url's:

<?php require_once('//domain.com/header.php'); ?>
d00rman
  • 310
  • 1
  • 5
  • 15