0

Okay, first; I'm relatively new to php. I know the basics but nothing too fancy.

I'm developing a website where the header and the footer stay the same on which ever page you are. But the problem is this (obviously).

The index.php looks like this:

<?php include("header.php") ?>

// Body content

<?php include("footer.php") ?>

In the header.php there's also the < head> tag. When I change a page that is in an underlying folder, the css path ofcourse stays the same (so without the "../" ). And there's my problem, is this fixable or do I have to exclude the < head> tag from the header.php?

Thank's in advance.

3 Answers3

0

If the header page stays always in the same directory, I think that there is no problem with that. In the pages that are in the other folders you just have to point to where the header is and then the header will be in the right directory to call the css's.

Like:

<?php
include("header.php") 
?>

And in a subfolder:

<?php
include("./header.php") 
?>

Have you tried this?

Miguel
  • 126
  • 9
  • That's not the problem. Fetching the header works just fine like that. It's what's inside the header. So on the index the path for the css would be: src="assets/styles/css/app.css" On a file that's in another folder it logically should be: src="../assets/styles/css/app.css" And there's the problem – user3625849 Jul 01 '15 at 13:27
0

The easiest way to solve this is to use absolute paths like this:

/path/to/css/file.css

Shawn Steward
  • 6,773
  • 3
  • 24
  • 45
  • That means that every page should be in the same folder. But from a SEO perspective, this won't help. There's a folder structure like this: /index.php /folder1/page.php /folder1/folder2?another-page.php – user3625849 Jul 01 '15 at 13:30
  • Thanks, this eventually fixed my problem. – user3625849 Jul 02 '15 at 10:14
  • @user3625849 Actually, this absolute path technique makes it so you don't have to worry about the folder structure of your pages. The front slash (/) starts at the root of your site's URL and everything is the same from that point on. – Shawn Steward Jul 06 '15 at 19:15
0

I suggest to do a different approach.

Any request is handled by one file, so you index.php

in there you have

<?php include("header.php") ?>

// Body content

<?php include("footer.php") ?>

you can then create your directory structure and catch the script via an URL $_GET parameter

e.g. index.php?request=page1 you can then handle this in PHP saying

switch ($_GET['request']){
    case 'page1' : require './sub/page1.php';break;
....
    default: require 'real_index.php';break;
}

Then you can make your SEO-urls by using the .htaccess file. This is basically a rerouting of the url (mod_rewrite in Apache)

There you should have something like this:

RewriteEngine On
RewriteRule ^(.+)$ index.php?request=$1 [QSA,L]

So any request is then redirected, to the script you want, but on the server side you always stay on the same directory level like index.php is.

e.g http://localhost/page1 is then redirected to http://localhost/index.php?request=page1

For a bit more explanation maybe this video helps BasicMVC tutorial Its a bit fast for beginners, but very good explained.

CiTNOH
  • 186
  • 1
  • 8