Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I've got a question about php and pages :
Is-it worth to split the code into many other files ?
So at the end, the index.php would be composed of :
include 'footer.php';
include 'header.php';
include 'home.php';
include 'load.php';
[...]
Or should I put all the code in an unique page ?
Other question, if I include a php page, could all the vars created in the included page be used in the main page, or should-I use $_SESSION ?
Thank you all for your help !
As for your main question it’s worth. In fact, it will be a pain for you to manage your web page if you don’t follow this method in the long run. I am not a php guy but I can tell you this because this is a universal truth. The idea here is to write once and use anywhere necessary. You don’t want to write the same thing again and again, do you? That’s why we produce function when programming. We define a function and use it as many times we want. Now consider you have suddenly found that you need some modification to do , as you have created a function you can make the change in just one place and it will be reflected everywhere without touching a single line of code outside the function’s code. In actual project change is a constant. That’s why we find many design pattern in the software industry like MVC.
In a web project, mostly (not always) the header, navigation, footer are same across all the pages. Therefore, you should consider making different file for these stuff and you should always put your content in your index.php file which are unique for index page.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
Is it bad practice to make all of my web pages .PHP pages? My main reason for doing this would be to prevent myself from writing repeating code for things such as my nav bar and footer and having these in separate files so that I can 'include' them in a way similar to below.
<?php include('nav.php'); ?>
I was made aware that you can also use JavaScript to do this however my thoughts are that relying on JavaScript for something as crucial as a nav bar would be a bad idea.
This is more of an opinion question than anything. I would not consider this to be bad practice as long as the following conditions are met:
If someone browses to https://example.com/nav.php or any similar "include only" nothing bad can happen as a side-effect. By bad I mean that if the .php expects to be included from wider context and ends up wiping the database for example. Easy way to prevent this is to declare a constant in your main "page" files and check that in the includes.
You find this approach easy to manage. In a larger page you might end up with a lot of files that are included from different places and expect certain global variables to be present. In these cases structuring your directories correctly and documenting the code is essential.
Happy coding!
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
On php, i want to put all classes and functions on a single page. This page will be called throughout my project. is it recommendable or a bad practice?
It is bad practice. It is called a big ball of mud anti pattern. Try to keep everything modularized. It will be more maintainable and readable. You will thank yourself later.
Sure, it's possible. These files are often called "helpers". You could compile all the functions and classes that you need throughout your website in one file (e.g. functions.php). However, you should not forget to properly split your code apart into different files, to prevent creating a big mess. You can then use the functions/classes like so:
require 'functions.php';
$class = new MyClass(); // Use a class from the functions.php
myFunction(); // Use a function from the functions.php
But! Note that this file can now also be accessed via a URL. For example: http://example.com/functions.php. This can, in some cases, cause unwanted behavior. You can restrict the "direct access" to this file using .htaccess rules or simple place this code on top of the helper file:
if(count(get_included_files()) == 1) die();
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
With my current workflow, when I need to add a new CSS sheet or a new script to a 10 pages website, I need to do this 10 times. Same story if I need to rename one item in my navigation menu, or correct a typo in the footer.
That made me realize that I need to start learning PHP to handle that via includes, echo, etc.
Therefore my question is the following: is there a best practice to do this? (I guess this is relatively easy to implement a < ?php include 'header.php'; ?> and a < ?php include 'footer'; ?> but can become quickly messy when it comes to dealing with unique page title and descriptions, etc.
What are your suggestions?
Many thanks
If your building not object style PHP website using require_once seems just the proper way to achieve your goal. It's better than include because require spread all errors and _once only include it once (Even if there is an inclusion of the same file deeper in the including tree) :)
Elsewhere Assetic is library that handle exactly this :)
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I have a web page which made up of three pages[Header, Content, Footer]. I have something like as follows in my code
<?php
if (session_status() == PHP_SESSION_NONE){
session_start();
}
if(!isset($_SESSION['username'])){
include('header.php');
include('body.php');
include('footer.html');
}else {
header('Location: users.php');
}
?>
Is there anything wrong with this approach? Are I am doing the right thing? I am not facing any problems since I am a newbie So I am not much aware of the issues. I just want to know good/bad practice to achive this?
I have this kind of set up because header & footer is common for all my pages. So I just modify the centre portion.
For what you're trying to achieve, this approach is perfectly fine.
Although as your files and directories will increase in number, sometimes it'll get frustrating to remember the location of those files and using include accordingly. i.e. include("../../static_pages/body.php").
So you should put $_SERVER['DOCUMENT_ROOT'] at the beginning and use it accordingly to use the same URLs in whole websites.
i.e. include($_SERVER['DOCUMENT_ROOT']."static_pages/body.php")
Oh, mind the difference between require and include. require will stop execution when file is not found whereas include will just throw a warning.
Both have their importance. :D
I think there's nothing wrong with your approach. However, I am not a professional or something but if it works it should be alright because I think it is a secure way of displaying your page.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I know CSS and HTML to a reasonable degree, but I've never built a multi-page website before.
I have a header area of my site coded, and want it to display it across multiple webpages. How can I do this?
Save it as header.php/header.html whichever one you need. Then, from every file you code from then on, you can <?php include('header.php');?>. The files you include in HAVE to be .php or it wont work.
Same applies to jsp if for any reason php didnt work or you fancied a change of pace
Create a file called header.jsp then use the tag below when you want to include it on a page.
<jsp:include page="{header.jsp}"/>
This is maybe of no use to you but who knows it may help someone. Some people find this easier and some find this harder it depends on your programming background.
Use a web programming language (PHP / ASP.Net / Whatever you like) to include the navigation bar in all the pages. There are template languages like Twig that can do that for you in a smarter way and provide you with additional template features, if you might need them.