所以,现在我正在尝试生成动态URL以及它们的路径,而不使用.htaccess或使用模块。基本上,我正在尝试重写并输出在Apache服务的不同本地主机上无状态工作的URL路径。
我想有一个index.php通过像这样的查询字符串获取URI(显然不是代码):
-> index.php (echos dynamically generated URL hyperlinks)
-> index.php?q=/ (echos every URI in JSON)
-> index.php?q=/some_id (outputs table some_id info w/ links to reviews)
-> index.php?q=/some_id/reviews (outputs table column of reviews w/ link to review_ids)
-> index.php?q=/some_id/reviews/review_id (output related column info)
有人可以告诉我如何做到这一点吗?我想我将不得不使用$ _SERVER方法编写URL并在迭代表ID数组时爆炸...?
非常感谢任何帮助!
编辑:
这是我试图编写的代码:/
<?php
$db = $user = $pw = 'logininfo';
try {
$dbconn = new PDO('mysql:host=localhost;db='.$db, $user, $pw;
}
catch (Exception $e) {
echo "Error: ";
echo $e->getMessage();
}
?>
<!DOCTYPE HTML>
<head>
<title>Product Reviews</title>
</head>
<body>
<h1>Product List:</h1>
<h2>
<ul>
<?php
try {
$sql = "SELECT somename, some_id FROM products";
$stmt = $dbconn->query($sql);
if($stmt !== false) {
foreach($stmt as $row) { //output just name
echo "<li>";
echo htmlentities($row['somename'])."<br />";
if($stmt !== false) {
$url = "<a href=index.php?q=".
htmlentities($row['some_id'])."/>".
htmlentities($row['somename'])."'s Review</a>";
echo $url; //output URL
echo "</li>"."<br />";
}
else {
echo "Unnecessary";
}
}
if($_GET['url']) { //don't really know what to put here
header("Location: $url"); //can't use headers anyway
}
}
$stmt = null;
}
catch (PDOEXCEPTION $e) {
echo $e->getMessge();
}
?>
</ul>
</h2>
</body>
</html>
答案 0 :(得分:2)
您可以将网址写为:
http://example.org/index.php/reviews/id/ [where id can be your review id]
并使用$_SERVER['PATH_INFO']
中的index.php
获取index.php
之后的网址部分,然后展开文本并从中获取所需数据。
<?php
$query_string = explode('/', $_SERVER['PATH_INFO']);
switch(count($query_string)) {
case 2:
$some_id = (int) $query_string[1];
if ($some_id === 0) {
//(echos every URI in JSON)
}
else {
// (outputs table some_id info w/ links to reviews)
}
break;
case 3:
//(outputs table column of reviews w/ link to review_ids)
$some_id = (int) $query_string[1];
$table_name = $query_string[2];
break;
case 4:
//(output related column info)
$some_id = (int) $query_string[1];
$table_name = $query_string[2];
$review_id = (int) $query_string[3];
break;
default:
// Error case
}
?>
答案 1 :(得分:1)
试试这个尺寸
if (isset($_GET['q']) && !empty($_GET['q']))
{
$params = explode("/",$_GET['q']);
if (isset($params[3]) && !empty($params[3]))
echo "(output {$params[2]} {$params[3]} info)";
else if (isset($params[2]) && !empty($params[2]))
echo "(outputs table column of {$params[2]} w/ link to review_ids)";
else if (isset($params[1]) && !empty($params[1]))
echo "(outputs table {$params[1]} info w/ links to reviews)";
else
echo "(echos every URI in JSON) ";
}
else
echo "(echos dynamically generated URL hyperlinks)";