简体   繁体   English

如何加快基于PHP的网站的速度?

[英]How can I speed up my PHP based website?

I just built a small PHP based site from scratch, yay for me! 我只是从头开始构建了一个基于PHP的小型站点,对我来说! 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] CSS [文件夹]
  • html [folder] html [文件夹]
  • images [folder] 图片[资料夹]
  • js [folder] js [文件夹]
  • photography [folder] 摄影[文件夹]
  • resources [folder] 资源[文件夹]
  • articles.php [file] article.php [文件]
  • bio.php [file] bio.php [文件]
  • contact.php [file] contact.php [文件]
  • content.php [file] content.php [文件]
  • favicon.ico favicon.ico
  • footer.php [file] footer.php [文件]
  • header.php [file] header.php [文件]
  • index.php [file] index.php [文件]
  • photography.php [file] photography.php [文件]

And most of my PHP files are coded something like this: 而且我的大多数PHP文件都编码如下:

<?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. 有些文件也直接在内部包含html代码。 I would appreciate any advice on how to improve the speed on my small PHP based site. 对于在我的小型PHP网站上如何提高速度的任何建议,我将不胜感激。

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 . 数据库查询优化是整本书的主题,但是如果您使用MySQL,请查看慢速查询日志 To quantify the site's performance characteristics have a look at JMeter . 要量化站点的性能特征,请查看JMeter

Edit : I just found out (from your source) that you are not using MySQL. 编辑 :我刚刚发现(从您的来源)您没有使用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. 不过,在完全不相关的注释上,请小心您所遇到的直接文件包含安全性问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM