我有一个允许用户注册和创建个人资料的网站。我在标题中有一个基本的用户配置文件链接,允许用户在查看其他页面时链接回他们的配置文件页面。脚本工作正常。问题是现在我为两种不同类型的用户提供了单独的用户配置文件页面。用户 1 链接到配置文件.php用户 2 链接到配置文件 2.php。问题是我不确定如何使脚本查看用户类型并根据登录用户的状态显示配置文件.php或配置文件 2.php。
这是我的代码:
<?php
if(isset($_SESSION['valid'])&&$_SESSION['valid']==true){
// INSERT USER LOGGED IN MESSAGE BELOW AS HTML
?>
<h3><a href='profile.php'><?php echo $_SESSION['userName']; ?></a></h3>
<a href="login.php?action=logout">Logout</a>
<?php
// END OF IF LOGGED IN STMT
}
?>
我基本上希望脚本读取用户 1 是否登录然后链接到配置文件.php但如果用户 2 已登录,则链接到配置文件 2.php。如何使用我拥有的脚本完成此操作?
你做错了,你应该让profile.php
查看用户 ID 或用户引用(用户名等)。
你的逻辑是这样的:
- 用户名 -> 获取指向
profile.php?uid=USERNAME
的链接 - 配置文件页面根据
uid
值获取用户详细信息 - 个人资料页面对任何用户都是动态的
配置文件.php示例:
<?php
if(isset($_GET['uid'])){
$username = sanitize($_GET['uid']); // sanitize is not a function, just implying that data is insecure and requires to be santized through whatever method you choose.
$details = get_user_details($username); // this will check your database/file etc for details on user.
} else {
// user is not requested $_GET['uid'], nothing to show
echo "User not found!";
exit();
}
// output details in any format you desire for queried user
?>