How can I speed up my PHP based website?
I just built a small PHP based site from scratch, yay for me! But given the size of it, it's running a little slower than I expected.
I have the files/folders organized like this:
- articles [folder]
- css [folder]
- html [folder]
- images [folder]
- js [folder]
- photography [folder]
- resources [folder]
- articles.php [file]
- bio.php [file]
- contact.php [file]
- content.php [file]
- favicon.ico
- footer.php [file]
- header.php [file]
- index.php [file]
- photography.php [file]
And most of my PHP files are coded something like this:
<?php
$thisPage="Writing";
include("header.php");
$page = $_GET['article'];
$file = "articles/".$page.".html";
if(file_exists($file)) {
include($file);
} else {
print "404 Error. Page does not exist";
}
function IsSafeInclude($x) {
if(strpos($x, "/../") !== false || strpos($x, "../") === 0 || strpos($x, "/..") == (strlen($x) - 3) || $x == '..')
return false;
else
return true;
}
//include("html/articles-left.html");
?>
<div id="article-nav-container">
<ul id="article-nav-pg">
<li><a href="articles.php?article=article_name1">1</a></li>
<li><a href="articles.php?article=article_name2">2</a></li>
<li><a href="articles.php?article=article_name3">3</a></li>
<li><a href="articles.php?article=article_name4">4</a></li>
<li><a href="articles.php?article=article_name5">5</a></li>
<li><a href="articles.php?article=article_name6">6</a></li>
</ul>
<script type=开发者_开发问答"text/javascript">
$(document).ready(function() {
var loc = window.location.href; // The URL of the page we're looking at
$('#article-nav-pg a').each(function() {
if (loc.indexOf(this.href) !== -1) { // If the URL contains the href of the anchor
$(this).addClass('selected'); // Mark it as selected
}
});
});
</script>
</div><!-- end articles nav -->
<p id="left-description"><img src="images/side-descrip-stories.jpg" width="20" height="90" alt="Story Description" /></p>
<?php
include("footer.php");
?>
Some files also have html codes directly inside. I would appreciate any advice on how to improve the speed on my small PHP based site.
Thanks
First of all you need to measure:
- how slow is "slow"?
- is slow consistent?
- does the site become very very slow when many users visit it?
Then find bottlenecks, design changes, implement changes and measure again. At some point you ought to specify how much do you want to improve things and after that stop.
Most of the times the bottleneck is the database. Database queries optimization is a subject for a whole book, but if you use MySQL have a look at the slow query log. To quantify the site's performance characteristics have a look at JMeter.
Edit: I just found out (from your source) that you are not using MySQL. So you need to measure and provide us with some numbers. How much is the page render time to start with?
On a completely unrelated note though, be careful for the direct file inclusion security issue you've got there.
精彩评论