【问题标题】:checking urls against tables in the database根据数据库中的表检查 url
【发布时间】:2016-06-28 16:59:52
【问题描述】:

我目前正在通过正在工作的 IIS 使用 url 重写,并且我正在使用以下变量捕获 url

$compiled_url= $_GET["params"];

但是我需要用 MySQL 的 pages 表中的内容检查该 url 中的内容。表格如下所示

tbl_pages
page_id | page_slug | page_parent
--------+-----------+------------
   1    |   posts   | 0
   2    |   daily   | 2

http://www.domain.com/posts/daily

有哪些可能的方法来检查上述域并针对数据库传递参数并确保它们以该顺序存在,因此如果每天/帖子向后键入 url,它将失败为 404,因为它们没有反映这一点数据库中的方式

我已经开始使用这种方法,但作为一个例子,我的最终结果是我想成为一个班级或更整洁的选择

$compiled_url = explode("/",$compiled_url); 
$compiled_parent=0;
foreach($compiled_url as $url_decompiled)
{                   
    $url_results = mysqli_query($webapp_db,"SELECT * FROM tbl_pages WHERE page_slug='" . $url_decompiled . "' AND page_parent ='" . $compiled_parent . "'");
    if(mysqli_num_rows($url_results) > 0)
    {
    echo "page found <br>";
    $result = mysqli_fetch_array($url_results);                 
    $compiled_parent=$result['page_id'];
    }
    else
    {
    echo "page not found <br>"; 
    break;
    }

}

谁曾经做过这样的事情?不使用框架有哪些方法可用?

【问题讨论】:

  • 你需要在这里加入,将 pages 表加入到自身中。您需要在没有任何 PHP 的情况下使用原始 SQL 进行练习,直到获得有效的查询。
  • 说到PHP的实现,很容易被sql注入。
  • 我已经处理了 sql 注入方面的事情,但是在示例中没有。你能给我提供一个连接表的例子吗
  • 您将如何完成上述任务?

标签: php mysql url-rewriting


【解决方案1】:

你可以使用这样的东西(没有测试过),但你明白了它的要点......

<?php
$compiled_url = explode("/",$compiled_url);

// using alphabet characters for table aliases
$alphabet = explode("","abcdefghiklmnopqrstuvwxyz");

$sql_query = "SELECT * FROM tbl_pages ".$alphabet[0];

// loop to build all the JOINs 
for($i=0; $i<count($compiled_url); $i++)
{
    $sql_query .= " INNER JOIN tbl_pages ".$alphabet[$i+1]." ON  ".$alphabet[$i+1].".page_id = ".$alphabet[$i].".page_parent";
}


// loop to build all the WHERE filters
$where = array();
for($i=0; $i<count($compiled_url); $i++)
{
    $where[] = $alphabet[$i].".page_slug = '".$compiled_url[$i]."'";
}

$sql_query .= " WHERE ".implode(" AND ",$where);

// if results are found print "page found" else print "page not found"
if(mysqli_num_rows($url_results) > 0)
{
   echo "page found <br>";
   $result = mysqli_fetch_array($url_results);                 
   $compiled_parent=$result['page_id'];
}
else
{
   echo "page not found <br>"; 
   break;
}

【讨论】:

    【解决方案2】:

    您的代码存在问题,您必须在 for 循环之外而不是在其内部进行检查。而且我认为您在数据库中添加了一些错误的数据。

    所以考虑一下你的桌子是这样的

    tbl_pages
    page_id | page_slug | page_parent
    --------+-----------+------------
       1    |   posts   | 0
       2    |   daily   | 1 // <-- edited from 2 to 1
    

    那你就用这个脚本

    $compiled_url = explode("/",$compiled_url); 
    $parent = 0;
    $correct = true;
    foreach($compiled_url as $item){                   
        $results = mysqli_query($webapp_db,
            "SELECT * FROM tbl_pages WHERE page_slug='" . $item . "' AND page_parent ='" . $parent . "' LIMIT 1"
        );
        if($row = mysqli_fetch_array($result)){
            $parent = $row['id'];
        } else {
            $correct = false;
            break;
        }
    }
    
    if($correct){
       echo "page found <br>";
    } else {
       echo "page not found <br>"; 
    }
    

    根据需要编辑此代码,如果按原样使用,请注意 sql 注入

    【讨论】:

    • 这是完美的。你会使用什么方法来防止 $compiled url 的 sql 注入
    猜你喜欢
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 2022-01-16
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多