【问题标题】:MySQL PHP CMS URL creation(need to edit)MySQL PHP CMS URL 创建(需要编辑)
【发布时间】:2011-10-20 17:16:09
【问题描述】:

对于这个问题的冗长,我深表歉意,并提前表示感谢。

以下是相关代码的 sn-p。我有一个 MySQL 表,它存储来自基于本教程的自定义构建的 PHP MySQL CMS 程序的信息:cms tutorial。正如您在 List Pages 和 Display Admin 函数中看到的那样,URL 是使用基本 url 和 ID 以及标题构建的。 ID 是 MySQL 表中的主键,它提取所有必要的信息以显示在页面上。我将如何重写下面的代码,使其仅在 URL 中而不是 ID 中显示标题,但仍然不使用 ID 作为在页面中显示信息的唯一标识符?

来自functions.php:

'// Display center bottom column
function centerbottomcolumn() {
if ($_GET['id']) {
    $pageID = (int) $_GET['id'];

    $result = mysql_query("SELECT centerbottomcolumn FROM pages WHERE id='$pageID'");
    $row = mysql_fetch_array($result);

    echo $row['centerbottomcolumn'];
} else {
    $result = mysql_query("SELECT value FROM settings WHERE name='homePage'");
    $row = mysql_fetch_array($result);

    $pageID = $row['value'];

    $result = mysql_query("SELECT centerbottomcolumn FROM pages WHERE title='$pageID'");
    $row = mysql_fetch_array($result);

    echo $row['centerbottomcolumn'];
}

}

// List the pages
function listPages() {
// List the home page first
$result = mysql_query("SELECT value FROM settings WHERE name='homePage'");
$row = mysql_fetch_array($result);

$homeID = $row['value'];

$result = mysql_query("SELECT title FROM pages WHERE id='$homeID'");
$row = mysql_fetch_array($result);

$homeTitle = $row['title'];

echo "<li><a href='" . BASE_URL . "/index.php'>$homeTitle</a></li>";

// List the rest of the pages
$result = mysql_query("SELECT id, title FROM pages");

while ($row = mysql_fetch_array($result)) {
    // Do not list the home page twice
    if ($row['title'] != $homeID) {
        $pageID = $row['title'];
        $pageTitle = $row['title'];

        echo "<li><a href='" . BASE_URL . "/?id=$id&title=$pageTitle'>$pageTitle</a></li>";
    }
}

}

// Display admin table
function displayAdmin() {
// Find the home page ID
$result = mysql_query("SELECT value FROM settings WHERE name='homePage'");
$row = mysql_fetch_array($result);

$homeID = $row['value'];

// Display a table
$result = mysql_query("SELECT id, title, date FROM pages");

echo '<table width="961">';
echo '<tr height="50">
    <th align="left">ID</th>
    <th align="left">Title of the Page</th>
    <th align="left">Date Created</th>
    <th align="left">Actions</th>
    </tr>';

while ($row = mysql_fetch_array($result)) {
    $id = $row['id'];
    $title = $row['title'];
    $date = date('M d, Y', $row['date']);

    echo "<tr>
        <td>$id</td>
        <td><a href='". BASE_URL . "/id=$id&title=$title'>$title</a>";
    if ($id == $homeID) {
        echo ' <strong>(Home Page)</strong>';
    }
    echo "</td>
        <td>$date</td>
        <td><a href='edit.php?id=$id'>Edit</a></td><td>
        <a href='confirm.php?id=$id'>Delete</a></td><td>
        <a href='sethome.php?id=$id'>Set as Home</a>";
}
echo '</table>';

}

// Get array with page IDs
function getArray() {
$result = mysql_query("SELECT id FROM pages");

$IDs = array();
$i = 0;
while ($row = mysql_fetch_array($result)) {
    $IDs[$i] = $row['id'];
    $i++;
}
return $IDs;

}

From index.php:

(above head tag)
require_once 'functions.php';

connect();

$IDs = getArray();

(from body)
<?php if (in_array($_GET['id'], $IDs) || !$_GET): ?>
<?php centerbottomcolumn(); ?>
<?php else: ?>

        <!-- Show a not found error -->
        <p>Not found</p>

    <?php endif; ?>'

【问题讨论】:

    标签: php mysql url content-management-system


    【解决方案1】:

    您希望您的网页根据给定 ID 显示信息,但又不想将 ID 传递给 URL 查询字符串上的网页?您可以将数据发布到目标页面而不是查询字符串。您可以发明一个新 ID 来代替真实 ID。但一般来说,如果没有输入,您不能让页面根据用户输入(在这种情况下单击带有 ID 的链接)来执行某些操作。如果您想隐藏 ID,因为它应该是秘密的,或者您不希望人们能够猜到它们,那么您可以将您的 ID 替换为某种映射到数据库中真实(秘密)ID 的随机 guid .

    【讨论】:

    • 我该怎么做呢?抱歉,我对此很陌生。边走边学!
    • 一个典型的实现是生成一个 guid,并在创建记录时将其存储在一个 guid 列中。您可以使用自己的算法生成 guid,也可以使用您的语言或数据库提供的算法(例如 MySql 中的 uuid())。向 guid 添加唯一索引。然后,无论您在哪里使用 ID,都可以使用 guid。你仍然需要传递它,但你会降低有人猜到下一个向导的可能性。但是为什么要隐藏身份证号码呢?真正的解决方案可能是通过授权阻止访问。
    • 我并不是要阻止访问。我需要 ID 从表中提取该页面信息。所以我希望通过 .htaccess 隐藏 url 中的 id 并在 .htaccess 中指定一系列 id 编号,但还没有弄清楚如何。所以我正在探索所有的选择。
    猜你喜欢
    • 2013-11-16
    • 1970-01-01
    • 2020-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-27
    相关资源
    最近更新 更多