【问题标题】:Dynamic pages / links in php how does it workphp中的动态页面/链接如何工作
【发布时间】:2013-02-20 17:27:17
【问题描述】:

我为用户做了一个小的登录系统,他们可以在account_setting页面登录并更改他们的用户信息。

但由于我对 php 很陌生,我想知道如何为每个用户提供自己的页面?一个公开的页面。

例如,用户“Steven”拥有user_id=17

如何为该用户创建一个页面,以便在那里显示他的信息。 类似website.com/user=17 ...他的信息。

如果页面可以作为模板,只是根据用户不同的信息/url。

我没有要求任何人为我写这个,一个好的教程的链接就可以了:) 但是,请不要发布关于该主题的 5 年前的帖子。

【问题讨论】:

  • 5年前的帖子有什么问题,只要他们回答你的问题?您的问题非常基本,10 年前人们用 PHP 做的事情要复杂得多!
  • 我应该更具体一点,我提到非安全页面。使用mysql等

标签: php url dynamic-pages


【解决方案1】:

您需要 userprofile.php?userid=17 并使用 $_GET['userid'] 根据该用户绘制信息。 userprofile.php 上的 HTML 应该是相同的,只有数据会根据用户 ID 而改变。如果未设置用户 ID,则显示错误消息或其他内容

【讨论】:

    【解决方案2】:

    一般来说:

    if (!empty($_GET['user']) && is_numeric($_GET['user'])){
      //Find him in database
      if (user_found($_GET['user'])){
        include "left_column.php" ;
        include "user_info.php" ;
      } else {
        echo "Page is not found" ; //or set header error 404
      }
    } else {
      include "news_column.php" ;
    }
    

    【讨论】:

      【解决方案3】:

      website.com/index.php?user=17

      <?php
      require_once 'db/connect.php';
      
      //Pull in 'user' from the query string.
      $user = isset($_GET['user']) ? trim($_GET['user']) : null;
      
      //Try to pull that user's info from the database.
      $stmt = $dbh->prepare("SELECT * FROM user WHERE user_id = :user_id");
      $stmt->bindParam(':user_id', $user);
      $stmt->execute();
      $user= $stmt->fetch(PDO::FETCH_ASSOC);
      
      if(!is_array($user)){
          //User not found. Throw 404 or redirect.
          header('HTTP/1.0 404 Not Found');
          exit;
      }
      ?>
      <!DOCTYPE html>
      <html>
          <head>
              <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
              <title><?php echo htmlentities($user['name'], ENT_QUOTES, "utf-8"); ?></title>
          </head>
          <body>
              <h1><?php echo htmlentities($user['name'], ENT_QUOTES, "utf-8"); ?></h1>
              <p>
                  <?php echo nl2br(htmlentities($user['bio'], ENT_QUOTES, "utf-8")); ?>
              </p>
          </body>
      </html>
      

      【讨论】:

        【解决方案4】:

        我将假设您将用户信息存储在数据库中。为了争论,我们会说它是一个 mysql 数据库。您需要做的是捕获用户 ID,然后从数据库中读取该列。

        如果您的 URL 是 website.com/user/view.php?id=17,您的用户变量将位于 $_GET['id']

        所以是这样的:

        $id = mysqli_real_escape_string($_GET['id']);
        $results = mysqli->query("select * from users where id = '$id'");
        $results = $results->fetch_assoc();
        

        ... 将为用户显示信息;然后你只需构建一个页面来显示它。

        【讨论】:

        • 不要使用已被弃用的 mysql_* 函数,不要教其他人使用该函数,而是使用 PDO 或 MySQLi
        • 公平;所以我会用 mysqli->query 替换 mysql_query,用 $results->fetch_assoc() 替换 mysql_fetch_assoc($results) - 我会用什么替换 mysql_real_escape_string?或者 mysqli 会自动执行此操作吗?我希望修改我对当前标准的回答。
        • 非常感谢!是的,我使用 mysql 数据库,使用 mysqli ofc 进行查询。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-23
        • 1970-01-01
        • 2014-09-26
        • 1970-01-01
        相关资源
        最近更新 更多