Making a web page from many php/html pages [closed] - php

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.

Related

Best way to manage multiple php includes? [closed]

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 :)

Make PHP include only appear on certain URLs? [closed]

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 8 years ago.
Improve this question
I'm not the best at coding PHP, HTML and CSS are more where my expertise lie.
I would like to have a PHP file included into the homepage of my site, but only the homepage and not on any other page within the site, as the element in question is based in a header.php file and would appear on every other page otherwise. Is this possible to do and if so how could it be done?
If anyone could help that would be great!
Use the server superglobal, and request URI to check for the home page.
<?php
if($_SERVER['REQUEST_URI'] == '/' or $_SERVER['REQUEST_URI'] == '/index.php')
include('somefile.php');
?>
EDIT:
Got ninja'd, keep in mind that your home page could be requested a couple ways if you are using index.php. Also: Fairly sure that there is always an opening / on URIs, as it is just the verbatim query string for the server.
As you didn't post any code. So, I assume one of the condition here.
You can try something like this:
<?php
if($_SERVER['REQUEST_URI']=="index.php" || $_SERVER['REQUEST_URI']==""){
// include your file..
include('somefile.php');
}
?>
P.S.: I assume that index.php is your default home page.
You should search more on $_SERVER['REQUEST_URI']
You will need to adjust the variables, but this should do the trick.
if (basename($_SERVER['PHP_SELF']) == "yourHomePage.php") {
include_once("yourinclude.php");
}
You may need to echo the basename of the page to get it exactly right. I cannot remember if it will have a / on the front.
echo(basename($_SERVER['PHP_SELF']));

Default php include on all pages [closed]

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.

Is-it worth to split a page into many pages? [closed]

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.

How to separate this PHP and HTML source [closed]

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.

Categories