suggestions for php file and code structure

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Rippie
Forum Commoner
Posts: 76
Joined: Sun Jan 10, 2010 11:32 am
Location: Nottingham

suggestions for php file and code structure

Post by Rippie »

Hi all,

Have to ask you all this, how do you all do in regards to file structure and your php code ? I am working on a intranet for my work and so far i have following file structure:

/includes
/images
/login
/module 1
/module 2
/styles
index.php
file.php
file.php

So that is kinda my file structure. not many files in the root, trying to move them all away and into folders as someone said that is best coding practice. the issue i have with this layout is that my navigation, header & footer file is in the includes folder and depending on what module im in i have to change the url to where they are all located and i was hoping i am doing this wrong and that someone have a better solution.

The way i include my files is with using include or require either by doing /path/to/header.php or even started to use http://domain/path/to/page.php

I am currently looking into learning OOP, but from what i understand it would not solve my problem in figuring out how to include everything etc.

Thank you in advance for any help.

Rippie
User avatar
hypedupdawg
Forum Commoner
Posts: 74
Joined: Sat Apr 10, 2010 5:21 am

Re: suggestions for php file and code structure

Post by hypedupdawg »

Yes - the best way to do this is using absolute links to include your files. That way, the link always starts from the root directory. You need to be carefull though - css files will attach using just a forward slash at the start like this:

Code: Select all

<link href="/includes/example.css" rel="stylesheet" type="text/css" />
But if you are going to use the php include function, you need to do this:

Code: Select all

<?php include($_SERVER['DOCUMENT_ROOT'] . "/includes/file.php"); ?>
By using these methods, you always start from the top directory, wherever that may be. So the links will work whether you are testing on a localhost, or on your site, or have moved to a new site. If you want more info, I found this site really helpful.
Rippie
Forum Commoner
Posts: 76
Joined: Sun Jan 10, 2010 11:32 am
Location: Nottingham

Re: suggestions for php file and code structure

Post by Rippie »

Thank you very much for your reply. much appreciated.

May i ask how you normally do your file structure ? like if you have a main page but then maybe you have an admin module and a different module. would you put them into different folders or put them all in one just make unique filenames ? example admin_file1.php module_file.php ?

Rippie
User avatar
hypedupdawg
Forum Commoner
Posts: 74
Joined: Sat Apr 10, 2010 5:21 am

Re: suggestions for php file and code structure

Post by hypedupdawg »

I think I would put all the normal files in the main directory (index, login, etc.), all the images in another, and then the admin files in a separate one like this:

/includes
/images
/admin
adminview.php
login.php
index.php
gallery.php
videos.php

- You should put only admin specific files in the admin folder.
- You should keep the otheres loose or in a folder named "all" and not "nonAdmin", as they may need to be accessed by the admins as well.

Also, a folder named "nonAdmin" shows there is a folder called "admin" out ther to break into! For further info, I reccommend you read this topic here.
Rippie
Forum Commoner
Posts: 76
Joined: Sun Jan 10, 2010 11:32 am
Location: Nottingham

Re: suggestions for php file and code structure

Post by Rippie »

hypedupdawg wrote:Yes - the best way to do this is using absolute links to include your files. That way, the link always starts from the root directory. You need to be carefull though - css files will attach using just a forward slash at the start like this:

Code: Select all

<link href="/includes/example.css" rel="stylesheet" type="text/css" />
But if you are going to use the php include function, you need to do this:

Code: Select all

<?php include($_SERVER['DOCUMENT_ROOT'] . "/includes/file.php"); ?>
By using these methods, you always start from the top directory, wherever that may be. So the links will work whether you are testing on a localhost, or on your site, or have moved to a new site. If you want more info, I found this site really helpful.
Hello again. You can still use $_SERVER['DOCUMENT_ROOT']/ for CSS link yes ?
User avatar
hypedupdawg
Forum Commoner
Posts: 74
Joined: Sat Apr 10, 2010 5:21 am

Re: suggestions for php file and code structure

Post by hypedupdawg »

Yes, of course you can. However, it may look better to use the <link> tag in your pages, as including the css sheet at the top of each page will make it look really messy - however, I leave it up to you!

EDIT: I should make it clear that under no circumstances should you do something like this:

Code: Select all

<link href="<?php print ($_SERVER['DOCUMENT_ROOT'] ; ?>/includes/example.css" rel="stylesheet" type="text/css" />
This will print your document root to the source code, potentially revealing your username and which host you are using - a massive security breach.
Post Reply