我对这个问题的长度表示歉意,并且想提前说声谢谢。MySQL PHP CMS创建URL(需要编辑)
下面是有问题的代码片段。我有一个MySQL表,用于存储基于本教程的自定义PHP MySQL CMS程序的信息:cms tutorial。正如您在“列表页面”和“显示管理”功能中所看到的,该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; ?>'
我该怎么做呢?对不起,我对此很新。随我去学习! – KGDD
一个典型的实现是在创建记录时生成guid并存储guid列中的每条记录。您可以使用自己的算法进行guid生成或使用您的语言或数据库提供的算法(例如MySql中的uuid())。向guid添加一个唯一的索引。那么,无论你在哪里使用ID,都可以使用guid。你仍然需要传递它,但是你降低了某人猜测下一个GUID的概率。但为什么你想隐藏身份证号码?真正的解决方案可能是防止授权访问。 –
我不想阻止访问。我需要ID从表格中提取页面信息。所以我希望通过.htaccess隐藏URL中的id,并在.htaccess中指定一系列id编号,但还没有想出如何。所以我正在探索所有的选择。 – KGDD