Id like to know what the best way of laying out a simple website,
I used switches so that my website would contain the same layout and then just have a little content area that changes as you go to
myserver/index.php?page=home
or
myserver/index.php?page=settings
Here is some code of my switch:
index.php:
<?php
session_start();
if (!isset($_SESSION['Username'])) {
header("Location: login.php");
}
include("config.php");
include("userinfo.php");
if (isset($_GET['page'])) {
$Page = $_GET['page'];
} else {
} switch ($Page) {
case "logout": {
require("logout.php");
include("layout.php");
break;
}
case "home": {
$PageTitle = "Home";
$PageFileName = "home.php";
include("layout.php");
break;
}
case "music": {
$PageTitle = "Music";
$PageFileName = "music.php";
include("layout.php");
break;
}
I basicly want to know if the above is a good system for haveing different pages with the same layout but a different content section?
I could also use php includes and include bits and pieces as I need, like a header and side bar, but Id like to know what the best system is? or just any advice or anything.
Thanks,
Jason Russell
I personally use the following structure:
main directory: has all the files
includes folder: has two files
Then I create a page, let's call it index.php
<?php
include("includes/header.php");
echo "<h1>Page title</h1>";
echo "<p>Page text...</p>";
include("footer.php");
?>
I then write a script for each page and keep it all separate. Your method above seems extremely complicated and you will get very very confused after not very long!
Also, the best way to split the header/footer is to make the layout as you want it in one file. Then locate the beginning of the main DIV and the end of the main DIV and put the top part in the header, bottom part in the footer.
I tried to keep that as simple as possible. I hope that helps :)
Well, I can assure you that a huge switch/case is not the solution. Look into MVC.
Related
I am trying to include a specific css file depending on the URL provided by the browser.
My website has the following format:
Header information
php include for content
Footer information
I am doing this so when I change my header menu links, I don't have to change every page on the website.
The php include code is as follows:
<?php
$page = $_GET['page'];
$pages = array('main', 'contact');
if (!empty($page)) {
if(in_array($page,$pages)) {
$page .= '.php';
include($page);
}
else {
include('404.php');
}
}
else {
include('main.php');
}
?>
The URL is made up as follows
index.php?page= for example index.php?page=contact
As part of the php include code, when the page=???? element isn't within the array it loads page 404.php which is an error page.
As can be seen, the array currently contains two pages, main (the home page content) and contact (the contact form). If a user tries to load a page called test (index.php?page=test) my webpage will display the 404 page (however doesn't load index.php?page=404 - the URL in browser stays as index.php?page=test.
What I'm trying to achieve is to load 404.css file when page=test(or any other page name not included in the array) is loaded.
Does anyone know how i'd go about achieving this? I've tried writing an if statement in the header to load 404.css however I was using strstr that fetches the browser URL (so test, not 404), therefore the correct css file doesn't get loaded.
Any help is much appreciated (or a better way to achieve what i'm trying to do).
Thanks in advance
Iain
An easy way of doing this is to make use of the ob_* php functions like bellow:
PHP get all the information from within the ob_start and the ob_get_clean functions and concatenate them all together as a single variable $_page_content, that variable can be used anywhere you need.
<?php
$page = $_GET['page'];
$pages = array('main', 'contact');
if (!empty($page))
{
if(in_array($page, $pages))
{
ob_start('ob_gzhandler');
?>
<link rel="stylesheet" href="css/<?=$page?>.css" type="text/css">
<?
include($page . '.php');
$_page_content = ob_get_clean();
echo $_page_content;
}
else
{
include('404.php');
}
}
else
{
include('main.php');
}
?>
I am building a personal portfolio webpage as a school assignment for my study Information science at the university of Amsterdam. Right now I copy and paste my header to each new page I make, so I can make little changes which show people at which page they are. Copying and pasting everytime you make a new page is far from efficient. I want to include the header code with a php file. But how can I still make little changes per page then?
My page: http://annatol.nl/
I hope anybody can help me.
Thanks in advance,
Anna
In your main php script :
<?php
include_once('inc/header.php');
?>
In your inc/header.php script :
<?php
$page = $_SERVER['REQUEST_URI'];
switch($page) {
case "/projects.html":
//Do specific stuff for Projects page
break;
case "/otherpage.html":
//Do specific stuff for another page
break;
default:
//In other cases...
break;
}
?>
Or if you need inline changes, you can do something like this in your header.php script :
<?php
$page = $_SERVER['REQUEST_URI'];
?>
<a href="/projects.html" <?php if($page == "/projects.html") { echo "class=\"active\""; } ?>>My projects</a>
Something like this?
include 'header.php';
You can include all the files by using the include function
Example:
include "Path_To_File";
you can also use the require or require_once functions aswell
I'm attempting to create a dynamic website where certain site content loads in to index.php's body.
I currently have the site divided in to 3 sections: Header, Body, and Footer where each of these sections are dynamically separate from each other.
<?php
if(!file_exists('content/header.php'))
{
die('Sorry we are experiencing technical difficulties. Please come back later.');
}
else
include('content/header.php');
?>
<?php
include('content/body.php');
?>
<?php
include('content/footer.html');
?>
Now what I'm hoping to do is have certain page content load in to body.php if lets say I click on a hyperlink that says "register" so that a user my register them selves to the site, register.php will load its contents in to body.php.
I've tried searching around but I think I'm asking the wrong questions in Google search so I figured I'd explain my self here and hopefully someone would guide me in the right direction, Thank you for your time.
If you want it so the user doesn't get redirected look at AJAX.
Otherwise you could have the register redirect to index.php?page=register and your code be
<?php
$page = isset($_GET['page']) ? 'body' : someSanitizingFunction($_GET['page']);
include('content/'.$page.'.php');
?>
That's obviously a unsecure implementation but you'll get the idea from it.
You should also look at a template engine like Smarty, that may be what you're after.
To create simple layouts in PHP you can write pages like about.php, contact.php and register.php, then
you can put this code in your body.php
$whitelist = array("about", "register", "contact");
if(in_array($_GET['page'], $whitelist)) {
include($_GET['page'].".php");
} else {
include("error404.php");
}
Your url should be like index.php?page=register.
I also recommend to have a look in MVC frameworks with Smarty, Twig or TemplatePower. It is a good way to sort with layouts.
I have noticed that in a couple of sites they can run the whole site from just the index.php, I also noticed the same effect in phpmyadmin to some extent.
An example of a site that does this is Try Open School, it is a demo site, and you can log in with (admin as password and admin as username, or teacher, teacher, or student, student). If you notice all that changes about the URL is the Query String.
I looked through the site and all I can Image is a whole lot of codes on just one page, But I do not think this is the case. I tried mine and I did something like this..
<?php
if($_GET['ref'] == 'home')
{
require_once("home.php");
}
elseif($_GET['ref'] == 'profile')
{
require_once("profile.php");
}
?>
I used the URL Query String to make reference to the different pages and to make other logical decisions.
I did a whole lot of this on the page, and the page contained several hundreds of if else statements. I do not know if I am doing what they did on their site.
I love the concept and would like to implement that kind of stuff on my own site.
I would appreciate all help on how to go about this. Thanks
If $_GET['ref'] is the only parameter for selecting page. a php switch, would probably be a better solution.
If you want to get fewer lines of code in your index file, you could put the switch in a separete php file!
Like this (index.php)
<?php
echo "<div id=header>Someinput</div>
<div id=menu>Some menu input</div>
<div id=site>";
require("switch.php");
echo "</div>
<div id=footer>Some footer input</div>";
?>
Then (switch.php):
<?php
switch($_GET['ref']){
case "page1":
include("page1.php");
break 1;
case "page2":
include("page2.php");
break 1;
default:
include("404.php");
}
?>
I have a small situaton here. I'm building a custom CMS for one of my websites.
Below is the code for the main index page:
<?php
require("includes/config.php");
include("includes/header.php");
if(empty($_GET['page'])) {
include('pages/home.php');
} else {
if(!empty($_GET['page'])){
$app = mysqli_real_escape_string($db,$_GET['page']);
$content = mysqli_fetch_assoc(mysqli_query($db, "SELECT * FROM pages_content WHERE htmltitle = '$app'")) or die(mysqli_error($db));
$title = $content['title'];
$metakeywords = $content['htmlkeywords'];
$metadesc = $content['htmldesc'];
?>
<h1><?php echo $content['title']; ?></h1><hr /><br />
<div id="content"><?php echo $content['content']; ?></div>
<? } else { include('includes/error/404.php');} }
include('includes/footer.php'); ?>
The file, includes/header.php contains code to echo variables, such as, page title and meta stuff.
The issue is that when the include("includes/header.php"); is where it is, outside of the if conditions, it will not echo the varables, obviously, however, I can't put the include in the if condition otherwise, the home page, which does not require any url variables will show without these conditions.
What do I do?
You can't really write code like this for too long. It's ok to for start, but you will soon realize it's hard to maintain. The usual way is to split it into a few steps.
First check input and determine on which page are you
If you know you are on the homepage, include something like includes/templates/homepage.php
Otherwise try to load the page from the database
If it worked, include includes/templates/page.php
Otherwise include includes/templates/404.php
Each of the files in includes/templates will output the whole page, i.e. they all include the header, do something and include the footer. You can use for example Smarty templates instead of PHP files, which will make the approach obvious.
Once you have this, you can split the code even more. Instead of loading the page directly from index.php, include another file which defines a function like load_page($name) and returns the page details.
Then a few more changes and you realize you are using the MVC approach. :) The functions that load data from the database are your Models, the Smary templates are Views and the PHP files that put them together are Controllers.