Jump to content

Recommended Posts

Hello everyone, this is my first post!

 

I have recently decided to learn PHP. Overall things are going well for me and I have recently discovered the include() function which is amazing for headers and footers! However it is with this function that I have a question:

 

I have created my header.html and footer.html and put them in an includes directory in the root of my site. I then use include() to wrap the 2 pages around my unique page content. For /index.php it looks like this:

 

<?php
include ('includes/header.html');
?>

<p>UNIQUE PAGE CONTENT HERE</p>

<?php
include ('includes/footer.html');
?>

 

This is great, but i find that links that are in the header.html file do not update to remain relative to where the particular php file is stored (eg, the above code makes links in the header work for /index.php but they would break for /information/about.php)

 

To get around this I have created a '$page_path' variable that is unique to every page and shifts the directory focus back to the site root, from there I just add in the relevant path information. To link /information/about.php to itself the code would look like this:

 

In the /includes/header.html file;

 

<head>
  <link href="<?php echo $page_path; ?>includes/css/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
  <a href="<?php echo $page_path; ?>information/about.php"link</a>
</body>

 

and then in the /information/about.php file;

 

<?php
$page_path = '../';
include ($page_path . 'includes/header.html');
?>

 

This moves the path from the includes folder back to root and then to the information dir and selecting the about.php file.

 

This all works correctly and I am pleased with it but my question is is this the best way?? I'm pretty sure there is a more efficient solution but I just can't work it out. Sadly my understanding of PHP is not brilliant yet so I have a hard time explaining what I have done and looking for a more efficient process. In case my explanation is so poor that you cannot understand me, I have attached a zip of my web directory to help explain.

 

Any help/information/assurance/constructive criticism is welcome. Sorry about the length of this post!

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/87958-using-include-and-linking-to-files/
Share on other sites

I always find it a bit difficult as well to be quite honest, and it can be a tad tricky (especially if you're viewing files in different directories etc.)

 

One way you could do it is create a $domain variable or something, which would contain http://localhost or http://www.mydomain.com and then you could do your includes from there, like:

 

<?php

include($domain."/includes/header.html");

?>

setting up a configuration.php file or some other such name and having a universal site root works rather well

 

ex:

 

<?php
$home = 'http://www.mysite.com/';
?>

 

and then using

 

<?php
include 'configuration.php';
$link = $home . 'myfile.html'
?>

 

to link to http://www.mysite.com/myfile.html

 

you can play with it from there,

 

welcome to the world of PHP, opens up what you can do in building sites to a new level of business and other functions as opposed to making things that just look pretty  ;)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.