【问题标题】:Created my one-file-blog (php/mysql) - Feedback and Injections?创建了我的单文件博客 (php/mysql) - 反馈和注入?
【发布时间】:2014-03-24 18:47:41
【问题描述】:

这是我第一次尝试编写一个非常简单的(单文件)博客引擎,使用 PHP 和 MySQL 构建。我想让一切变得简单,不想包含数百个文件、类等,因为我只想发布一些文本,就是这样。我不需要插件、更改模板、API 或类似的东西。该脚本现在可以正常工作并且运行良好,但我是一个真正的新手,并且刚刚开始使用 php/mysql。 :)

所以我想要一些反馈,我做错了什么,什么可能太复杂或者是否有注射或类似的可能性?欢迎任何帮助和反馈(对于我糟糕的英语感到抱歉!)。

我已经包含了一些 cmets,这样更容易理解我的想法:

<?php
///////////////////////////////////////////////////// BASE

// Whats the name of the blog and how many recent articles should shown on the front
$blogname = 'The basic blogname';
$anzahl = '3';

// Alright, let's connect to the database 
include_once 'include/connect.php';

// I use this to generate german date (e.g.: March --> März)
setlocale (LC_ALL, 'de_DE@euro.utf8', 'de_DE.utf8', 'de.utf8', 'ge.utf8');

///////////////////////////////////////////////////// START >>> IF

// As we using htaccess with modrewrite, we want to know, what page-name the user requested
if (isset($_GET['slug'])) {

// I'm not sure, if it makes sense (mysqli_/mysql_?) to avoid injections? Any help is welcome!
$blog = mysql_escape_string($_GET['slug']);

// Alright, now we check the database and ask if the sitename exist and if the status is "online" (published/draft)
$result = mysqli_query($con,"SELECT * FROM entries WHERE slug='$blog' AND status = 'ONLINE'");

// We call the result and check, if there is a article in the database
$num_results = mysqli_num_rows($result); 
if ($num_results > 0){ 

// We now also include the header-file, because there we also have the $title-variable for the site / browsertab
include 'header.php';
include_once 'markdown.php';

// Create variables from the database-fields, also convert the content with markdown
while($row = mysqli_fetch_array($result)){
$title = $row['title'];
$content = $row['content'];
$my_html = Markdown($content);
$date = $row['date'];
    $date = strftime('%d. %B %G', strtotime($date));

// and final: show the article on the website
echo '<h2>' . $title . '</h2>';
echo '<div id="date">' . $date . '</div>';
echo '<div id="content">' . $my_html . '</div>';
echo '<div id="link"><a href="/simple/"' . $slug . '">Back to front-page</a></div>';

// we also inlucde the footer, so that we have a complete page - header/content/footer
include 'footer.php';
}

///////////////////////////////////////////////////// ELSE  >>>

// but if there is NO entry in the database with this pagename...
} else {

// again we need the header
include 'header.php';

// then we say:
echo '<h2>Error</h2>';
echo '<div id="content">There is no article with this name!</div>';
echo '<div id="link"><a href="/simple/"' . $slug . '">Back to front</a></div>';

// and include the footer
include 'footer.php';
}

///////////////////////////////////////////////////// ELSE >>>

// But if the user just open the blog and don't request a name, we want to show him the last articles (3 - see top)...
} else {

// So again we call the database and request the last published entries and sort them, limited by the amount of given entries
$result = mysqli_query($con,"SELECT * FROM entries WHERE status = 'ONLINE' ORDER BY id DESC LIMIT $anzahl");

// Again include header and markdown
include 'header.php';
include_once "markdown.php";

// We generate variables from the datebase during the loop, also convert the excerpt with markdown
while($row = mysqli_fetch_array($result)){ 
$title = $row['title'];
$slug = $row['slug'];
$excerpt = $row['excerpt'];
$my_html = Markdown($excerpt);
$date = $row['date'];
 $date = strftime('%d. %B %G', strtotime($date));

// And publish them on the website
echo '<h2><a href="/simple/' . $slug . '">' . $title . '</a></h2>';
echo '<div id="date">' . $date . '</div>';
echo '<div id="content">' . $my_html . '</div>';
echo '<div id="link"><a href="/simple/' . $slug . '">Read more...</a></div>';

}
// Last time, we include the footer again.
include 'footer.php';
}

///////////////////////////////////////////////////// <<< FINISH
?>

谢谢 - 是的,我愿意学习! :))

【问题讨论】:

  • 您为什么要为您的数据库使用mysql_escape_string 以及mysqli 扩展名?只坚持其中之一。除此之外,我强烈建议在查询中使用prepared statements 而不是直接串联。大大降低了SQL注入的几率。
  • 您可能希望在首页中包含分页,或为博客提供存档。
  • 我想做一个手写存档...我不是每天都写,所以只需将条目(标题和链接)插入(复制)到文章中,名称为“档案”。 :)

标签: php mysql sql-injection blogs feedback


【解决方案1】:

通过使用 SQL 抽象库和模板可以让你的代码更整洁

$sql = "SELECT * FROM entries WHERE slug=?s AND status = 'ONLINE'";
$row = $db->getRow($sql, $_GET['slug']);
if ($row) { 
    $title   = $row['title'];
    $content = Markdown($row['content']);
    $date    = strftime('%d. %B %G', strtotime($row['date']));
    $tpl = 'single.tpl.php';
    include 'main.tpl.php'
} else {
    include '404.php';
}

对于列表

$sql  = "SELECT * FROM entries WHERE status = 'ONLINE' ORDER BY id DESC LIMIT ?i";
$data = $db->getAll($sql, $anzahl);
$tpl = 'list.tpl.php';
include 'main.tpl.php'

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-18
  • 1970-01-01
相关资源
最近更新 更多