php中的动态页面/链接它是如何工作的
我为用户制作了一个小型登录系统,他们可以在account_setting页面上登录并更改他们的用户信息。php中的动态页面/链接它是如何工作的
但是因为我很新的PHP我不知道我怎么能给每个用户自己的网页?一个公开的页面。
Ex,User“Steven”has user_id=17
。
如何为该用户创建一个页面,以便他的信息显示在那里。 类似于website.com/user=17
...他的信息。
而且,如果页面可以充当模板,只是根据用户的不同信息/ url。
我不问任何人为我写这篇文章,链接到一个好的教程将工作得很好:) 但是,请不要在该主题5年前的帖子。
需要userprofile.php?用户id = 17和使用$ _GET [ '用户ID']得出基于该用户的信息。在userprofile.php中HTML应该是相同的,只有数据会根据用户ID而改变。如果没有设置用户ID,然后显示错误信息或东西
一般说:
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" ;
}
我会假设你在数据库中存储用户信息。为了争论,我们会说它是一个mysql数据库。你需要做的是捕获用户标识,然后从数据库中只读取该列。
如果您的网址是website.com/user/view.php?id=17,你的用户变量将在$ _GET [ '身份证']
因此,像这样:
$id = mysqli_real_escape_string($_GET['id']);
$results = mysqli->query("select * from users where id = '$id'");
$results = $results->fetch_assoc();
...会为用户提供信息;那么你只需构建一个页面来显示它。
不要使用mysql_ *函数他们已被弃用,并没有教别人使用该功能,而是使用PDO或MySQLi – GGio 2013-02-20 17:32:36
公平;所以我会用mysqli-> query和mysql_fetch_assoc($ results)替换mysql_query和$ results-> fetch_assoc() - 我会用什么替换mysql_real_escape_string?或者mysqli自动执行此操作?我正在修改现行标准的答案。 – DiMono 2013-02-20 17:52:18
非常感谢!是的,即时通讯使用MySQL数据库,用mysqli ofc查询。 – 2013-02-20 21:53:18
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>
非常感谢! – 2013-02-21 03:15:17
这有什么错5岁的帖子,只要他们回答你的问题?你的问题是非常基本的,10年前人们用PHP来做更复杂的事情! – 2013-02-20 17:31:59
我应该更具体一些,我认为非安全页面。使用mysql等 – 2013-02-21 03:17:25