您的位置:首页 > 编程语言

模块化web程序代码

2015-03-28 17:51 239 查看
此部分是个人学习时一些资料的整理以及代码实现。



includes文件夹下包括:



modules文件夹下包括:



index.php作为索引文件

各个文件代码如下:

index.php:

<span style="font-size:18px;"><?PHP
#index.php
require('./includes/config.inc.php');
if(isset($_GET['p'])){
$p = $_GET['p'];
}elseif(isset($_POST['p'])){
$p = $_POST['p'];
}else{
$p = null;
}
//determine what page to display:
switch($p){
case 'about':
$page = 'about_inc.php';
$page_title = 'About This Site';
break;
case 'contact':
$page = 'contact.inc.php';
$page_title = 'Contact us';
break;
case 'search':
$page = 'search.inc.php';
$page_title = 'Search Result';
break;
default:
$page = 'main.inc.php';
$page_title = 'Site Home Page';
break;
}
//make sure the file exits;
if(!file_exists('./modules/'.$page)){
$page = "main.inc.php";
$page_title = 'Site Home Page';
}
//Include the header file
include('./includes/header.html');
include('./modules/'.$page);
include('./includes/footer.html');
?></span>


config.inc.php:

<span style="font-size:18px;"><?PHP
#config.inc.php
//Errors are emailed here
$conact_email = 'veaglefly@163.com';
//whether we are working on a local server
$host = substr($_SERVER['HTTP_HOST'],0,5);
if(in_array($host,array('local','127.0','192.1'))){
$local = true;
}else{
$local = false;
}
if($local){
$debug = true;
define('BASE_URI','/path/to/html/folder/');
define('BASE_URL','HTTP://lcoalhost/directory/');
define('DB','path/to/mysql.inc.php');
}else{
define('BASE_URI','/path/to/live/html/folder/');
define('BASE_URL','www.***.com');
define('DB','path/to/live/mysql.inc.php');
}

/**********Error Management*****************/
//Create the error handler
function my_error_handler($e_number,$e_message,$e_file,$e_line,$e_vars){
global $debug,$contact_email;
$message = "An error occurred in script '$e_file' on line $e_line:$e_message";
//Append $e_vars to the $message:
$message .=print_r($e_vars,1);
if($debug){
echo '<div class = "error">'.$message.'</div>';
debug_print_backtrace();
}else{
error_log($message,1,$conact_email);
if(($e_number!=E_NOTICE)&&($e_number <2048)){
echo '<div class = "error">A system error occurred.</div>';
}
}
}
//user_error
set_error_handler('my_error_handler');
?></span>


header.html

<?PHP
#header.html
//This page begins the HTML header for the site
//check for a $page_title value
if(!isset($page_title)) $page_title = 'Default Page Title';

?>

<!DOCTYPE HTML>
<html>

<head>
<title><?PHP echo $page_title;?></title>
<link rel="stylesheet" type="text/css" href="style/style.css" title="style" />
</head>

<body>
<div id="main">
<div id="header">
<div id="logo">
<div id="logo_text">
<!-- class="logo_colour", allows you to change the colour of the text -->
<h1><a href="index.html">colour<span class="logo_colour">blue</span></a></h1>
<h2>Simple. Contemporary. Website Template.</h2>
</div>
</div>
<div id="menubar">
<ul id="menu">
<!-- put class="selected" in the li tag for the selected page - to highlight which page you're on -->
<li><a href="index.php">Home</a></li>
<li><a href="index.php?p=about">About</a></li>
<li><a href="index.php?p=this">This</a></li>
<li><a href="index.php?p=that">That</a></li>
<li><a href="index.php?p=contact">Contact Us</a></li>
</ul>
</div>
</div>
<div id="site_content">
<div class="sidebar">
<!-- insert your sidebar items here -->
<h3>Latest News</h3>
<h4>New Website Launched</h4>
<h5>January 1st, 2010</h5>
<p>2010 sees the redesign of our website. Take a look around and let us know what you think.<br /><a href="#">Read more</a></p>
<p></p>
<h4>New Website Launched</h4>
<h5>January 1st, 2010</h5>
<p>2010 sees the redesign of our website. Take a look around and let us know what you think.<br /><a href="#">Read more</a></p>
<h3>Useful Links</h3>
<ul>
<li><a href="#">link 1</a></li>
<li><a href="#">link 2</a></li>

</ul>
<h3>Search</h3>
<form method="get" action="index.php" id="search_form">
<p>
<input type = "hidden" name = "p" value = "search">
<input class="search" type="text" name="terms" value="Search....." />
<input name="search" type="image" style="border: 0; margin: 0 0 -9px 5px;" src="style/search.png" alt="Search" title="Search" />
</p>
</form>
</div>
<div id="content">


footer.html
<!--footer.html-->
</div>
</div>
<div id="content_footer"></div>
<div id="footer">
Copyright © colour_blue | <a href="http://validator.w3.org/check?uri=referer">HTML5</a> | <a href="http://jigsaw.w3.org/css-validator/check/referer">CSS</a> | <a href="http://www.html5webtemplates.co.uk">design from HTML5webtemplates.co.uk</a>
</div>
</div>
</body>
</html>


main.inc.php

<?PHP
if(!defined('BASE_URL')){
require('.../includes/config.inc.php');
$url = BASE_URL.'index.php';
header("Localtion:$url");
exit;
}
?>
<!-- insert the page content here -->
<h1>Welcome to the colour_blue template</h1>
<p>This standards compliant, simple, fixed width website template is released as an 'open source' design (under a <a href="http://creativecommons.org/licenses/by/3.0">Creative Commons Attribution 3.0 Licence</a>), which means that you are free to download and use it for anything you want (including modifying and amending it). All I ask is that you leave the 'design from HTML5webtemplates.co.uk' link in the footer of the template, but other than that...</p>
<p>This template is written entirely in <strong>HTML5</strong> and <strong>CSS</strong>, and can be validated using the links in the footer.</p>
<p>You can view more free HTML5 web templates <a href="http://www.html5webtemplates.co.uk">here</a>.</p>
<p>This template is a fully functional 5 page website, with an <a href="examples.html">examples</a> page that gives examples of all the styles available with this design.</p>
<h2>Browser Compatibility</h2>
<p>This template has been tested in the following browsers:</p>
<ul>
<li>Internet Explorer 8</li>
<li>Internet Explorer 7</li>
<li>FireFox 3.5</li>
<li>Google Chrome 6</li>
<li>Safari 4</li>
</ul>


search.inc.php
<?PHP
#search.inc.php
if(!defined('BASE_URL')){
require('.../includes/config.inc.php');
$url = BASE_URL.'index.php?=search';
if(isset($_GET['terms'])){
$url .= '&terms='.urlencode($_GET['terms']);
}
header("Localtion:$url");
exit;
}
echo "<h1>Search Result</h1>";
if(isset($_GET['terms'])&&($_GET['terms']!='Search...')){
for($i = 1;$i <=10;$i++){
echo <<< EOT
<h4><a href = "#">Search Result </h4>
<p><This is a description</p>
EOT;
}
}else{
echo '<p class = "error">Please use the search this site.</p>';
}
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐