【发布时间】:2016-12-02 08:23:54
【问题描述】:
我希望能够在 url 中显示用户 ID,例如:profile.php?user=1, 2 etc.
我一直在到处搜索,但我无法让它工作。
profile.php 代码:
<?php
ob_start();
session_start();
require_once 'dbconnect.php';
// if session is not set this will redirect to login page
if( !isset($_SESSION['user']) ) {
header("Location: index.php");
exit;
}
// select loggedin users detail
$res=mysql_query("SELECT * FROM users WHERE userId=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);
$id = $_GET['userId'];
?>
PHP 代码:
<?php
ob_start();
session_start();
require_once 'dbconnect.php';
$id = (isset($_GET['id'])) ? $_GET['id'] : $_SESSION['id'];
$query = "SELECT * FROM users WHERE userId = $id";
// if session is not set this will redirect to login page
if( !isset($_SESSION['user']) ) {
header("Location:profile.php?id=" . $_SESSION['user']);
exit;
}
// select loggedin users detail
$res=mysql_query("SELECT * FROM users WHERE userId=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);
$id = $_GET['userId'];
?>
<?php echo $userRow['userId'];?>
我在那里使用最后一个命令链接到我自己的个人资料,但我想知道如何查看其他用户的个人资料。
【问题讨论】:
-
$_GET 将读取参数,而不是把它放在那里。您必须手动将其放在那里,这意味着当您在 html 代码中执行 href 时,您应该执行
href="domain.com/profile.php?user=$variable"之类的操作。对于$variable,您应该从 SQL 中读取值。 -
$id = $_GET['userId'];用于获取 GET 参数而不是设置它
-
你想在 Location: index.php 中添加 $id 就像 Location: index.php?uid=$id;
-
我有一个问题,为了安全起见,显示 URL 是最佳做法还是好的?
标签: php session profile userid