Jump to content

Recommended Posts

What is the best way to set up PHP pages?

 

Option 1:

= many pages like this:

<?php

//all required info
require_once('pagefiles/essentialStuff.php');

//more important stuff
require_once('pagefiles/importantStuff.php');

?>

//all html / head tags etc

//bespoke info for each page

 

Option 2:

= Using one page for everything (index.php) and using index.php?page=adduser / index.php?page=showusers  etc...

 

 

Or another way??

 

 

I suppose loads of page1.php, page2.php, page3.php isn't the best way??

Link to comment
https://forums.phpfreaks.com/topic/207063-best-way-to-set-up-php-pages/
Share on other sites

<?php
include "global.php"; // would be your file that includes functions, database connectivity, etc..

include "header.php"; // would be your file that is the chunk of HTML code that your page would start off with
$page = $_GET['page'];

if($page){
if(file_exists("pages/".$page.".php")){
	include "pages/".$page.".php";
}else {
	include "pages/404.php";
}
}else {
include "pages/home.php";
}

include "footer.php"; // would be your file that closes the open chunk of HTML from header.php
?>

 

Or if you don't want to use the file_exists method

 

<?php
include "global.php"; // would be your file that includes functions, database connectivity, etc..

include "header.php"; // would be your file that is the chunk of HTML code that your page would start off with
$page = $_GET['page'];
$pages = array('home','about','contact');

if($page){
if(in_array($page,$pages)){
	include "pages/".$page.".php";
}else {
	include "pages/404.php";
}
}else {
include "pages/home.php";
}

include "footer.php"; // would be your file that closes the open chunk of HTML from header.php
?>

How bad is it to use loads of different php pages that are accessed in the url? Of course they would all have relevant includes (global.php and header.php as mentioned above).

 

Facebook seem to have lots of different php pages, and I am sure many others. Are they doing it wrong?

Larger apps are typically written in an object oriented style, with each class being stored in its own file and included dynamically when needed.  If that doesn't make any sense, don't worry about it.  Put another way - large apps are written in a way that makes them flexible to change and easier to maintain than what you're used to seeing/writing yourself.  Lots of files is a byproduct of this programming style.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.