dongshao8471 2012-05-04 16:50
浏览 34

编写没有mod_rewrite的动态URL路径

So, right now I'm trying to generate dynamic URLs as well as their paths without using .htaccess or the use of modules. Basically, I'm trying to rewrite and output URL paths that work statelessly on different localhosts served by Apache.

I would like to have an index.php that gets URIs through a query string like this (obviously not code):

-> 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)

Could someone walk me through how I'd go about doing this? I figure I'm going to have to write the URL using $_SERVER methods and explode while iterating through an array of table IDs..?

Any help is greatly appreciated!


EDIT:
Here's the code I was trying to write :/

  <?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>
  • 写回答

2条回答 默认 最新

  • doudang8824 2012-05-04 18:37
    关注

    You can write URLs as :

    http://example.org/index.php/reviews/id/ [where id can be your review id]
    

    and use $_SERVER['PATH_INFO'] in index.php to get part of URL which is after index.php, then explode the text and get desired data out of it.

    <?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
        }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?