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
I have a change_username.php which makes it possible for users to change their username. If they changed their username a page like "Your new username ist xxx" or "xxx is already used" will be displayed.
I don't want to use an echo and put all the HTML stuff in there.
My first idea would be to create another 2 PHP's which then get the username from the change_username.php and display a proper result with HTML.
Are there any alternatives / better ways to do that? Creating files for every result sucks as much as putting the HTML data in an echo.
If I understood your question correctly you want to include a page with in your PHP: try using this as reference http://www.w3schools.com/php/php_includes.asp
You can include the page using
include 'filename';
or
<?php include 'menu.php'; ?>
I hope this helps you
I would do it a little bit differently, I think the problem is with managing the code where large page is getting messy. What I do in these cases I just use functions with parameters. So I create a lib.php file, where I store function with HTML blocks of code being echoed. Then when I need I call these functions in the necessary files, just requiring lib.php at the top of my 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 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 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
On my site I have overall header and footer that are being created by functions header() and footer(), accessible in overall.php file. All sites are based on the same template so it looks like following:
<?
include ("/overall.php");
header();
echo "<div>";
// content of site
echo "</div>";
footer();
?>
I am thinking about another solution - maybe make a function createsite($content):
<?
function createsite($content)
{
header();
echo "<div>".$content."</div>";
footer();
}
?>
so that it would be simplier to create new page (there will be more than hundred of them). What are your ideas? I have been thinking about something like default include of overall.php on all sites. I hugely prefer clear PHP over frameworks like symfony or zend.
You will probably end up doing the same thing all of the frameworks are also doing, which is having a single point of entry for all your pages which will include all the neccesary functions (such as those in overall.php) and then redirecting the user to whichever actual page they want to see by including that as well.
Keep in mind that what you're currently building is (or rather, will be) a very basic framework in its own right.
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.
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
The way I see it, if a file has been included once then any object, function or member in it is defined.
require_once checks if the file is included and if so, doesn't include it again. But when would the event ever arise that someone would go 'this file has already been included so I can use the class inside it, but I'd better include it again because this script needs it'?
Am I missing something?
I use include_once / require_once for classes files, and include / require for html code (let's say a form for example). You shouldn't redeclare the class, but you could insert html code multiple times in your code.
In ye olde days before proper classes, I would sometimes use require inside a for or while loop when doing large imports. Along the same lines, template engines use it for repeated includes of the same file that should fail if the file is missing.