[英]Making profile page unique by adding unique ID in URL. But how?
我正在使用PHP / MySQL创建社交网络脚本。 一切正常。 主页,配置文件,编辑配置文件等。但是“配置文件”页面具有URL- http://www.localhost/socialnetwork/profile.php 。
我有5个虚拟用户,使用用户名/密码访问个人资料时。 在浏览器上可以看到相同的URL。
但我想要类似-
网址-http://www.localhost/socialnetwork/profile.php?u = testuser 。
其中“ u”可以是数据库中的唯一用户ID。 如何执行呢? 这是我的代码-
<?php
include("includes/session.php");
include("includes/database.php");
include("includes/user.php");
include("includes/functions.php");
if(!$session->is_logged_in()) { redirect_to("login.php");}
?>
<?php
$user_id = $_SESSION['id'];
// - Here's What I have added - But nothing changes. The URL is same.
if(isset($_GET['id'])){
$uid = $_GET['id'];
}
$sql = "SELECT * FROM users WHERE id = '$user_id' LIMIT 1";
$result = $database->query($sql);
while($record = $database->fetch_array($result)){
$fname = $record['fname'];
$lname = $record['lname'];
$email = $record['email'];
$sex = $record['sex'];
$password = $record['password'];
$phone = $record['phone'];
$birthday = $record['birthday'];
$display = $record['dp'];
$bio = $record['bio'];
}
$_GET['u']
读出网址 在PROFILE.PHP之前
当用户“ foo”(虚拟用户名)登录时,他将被重定向到页面
http://www.localhost/socialnetwork/profile.php?u=foo
大! 但是我们该怎么做呢? 重定向是用PHP进行的,类似于:
header('location: /socialnetwork/profile.php?u=' . $theusername_just_logged);
header()函数基本上重定向到另一个页面。 调用该函数后,您将进入页面
http://www.localhost/socialnetwork/profile.php?u=foo
进入PROFILE.PHP
然后,在文件profile.php中,通过变量$ _GET检索用户名。 在这种情况下,$ _GET ['u']将包含用户名,即“ foo”。 这里有关$ _GET的更多信息
检索用户名后,您可以查询数据库以获取该用户的所有信息,并在此基础上构建页面!
这样,基于$ _GET ['u'],您将始终获得不同的页面! 如果用户名在数据库中不存在,请考虑创建正确的错误页面
希望能有所帮助
在您的while循环中添加
$id=$record['id'];
然后像这样在while循环中建立链接
echo '<p>
<a href="the url of your profile page.php?id='$id'">
'.$fname.'
</a></p>';
这将回显数据库中的所有名称,并为其添加唯一性。 在您的个人资料页面上,您可以使用来获取该特定用户的ID
echo $_GET[id'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.