更新时间:2022-05-24 22:08:17
您的问题有点宽泛.所以我会尽我所能回答.
You question is a bit broad. So I'll answer as mush as I can understand.
要访问/profile/user
,您必须使用URL重写.您必须启用Apache的mod_rewrite
(如果它是您的网络服务器).然后,您必须在.htaccess
文件中定义规则.
To access to /profile/user
, you have to use URL-rewriting. You have to enable the mod_rewrite
of Apache (if it's your webserver). Then you have to define rules in a .htaccess
file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^profile/(\w+)$ profile.php?user=$1
</IfModule>
因此,浏览到http://host/profile/namehere
将使用profile.php
,而$_GET['user']
将用"namehere"
定义.
So, browsing to http://host/profile/namehere
will use profile.php
and $_GET['user']
will be defined with "namehere"
.
在profile/namehere
页面中显示信息(profile.php
未经测试):
Display informations in profile/namehere
page (profile.php
untested):
<?php
if (!isset($_GET['user']))
{
// Error: Fall in error 404, or display error, or ...
header("HTTP/1.1 404 Not Found");
exit(0);
}
$username = $_GET['user'];
// connect database using PDO
$pdo = new PDO('mysql:dbname=DBNAME;host=localhost' , 'DBUSER', 'DBPASS') ;
$sth = $pdo->prepare("select * from profile_table where username=:username") ;
if (!$sth) { /* Error */ die ; }
$sth->execute([':username' => $username]) ;
$result = $sth->fetch(PDO::FETCH_OBJ) ;
if (!$result) { /* Error */ die ; }
// display informations using template
header("Content-Type: text/html; charset=utf-8") ;
include("template.php") ;
exit();
模板示例:
Template example :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title><?php echo $result->name ?></title>
</head>
<body>
<p><?php echo $result->info1 ?></p>
<p><?php echo $result->info2 ?></p>
<p><?php echo $result->info3 ?></p>
</body>
</html>