我有一个小型社交网站,有个人资料页面和搜索页面。这一切都很好,在菜单栏上,它显示用户用户名,并通过profile.php?id=5
链接到他们的配置文件(例如)。当我通过search.php
搜索内容时,一切都很好,但当我在搜索后重新加载页面时,用户名突然显示为"p",链接转到profile.php?id=p
有人知道发生了什么事吗?如果您需要更多信息,请告诉我,并提前表示感谢。代码:
<?php
//capture search term and remove spaces at its both ends if the is any
$searchTerm = trim($_GET['search']);
//check whether the name parsed is empty
if($searchTerm == "")
{
echo "Enter name you are searching for.";
exit();
}
//database connection info
$host = ""; //server
$db = ""; //database name
$user = ""; //dabases user name
$pwd = ""; //password
//connecting to server and creating link to database
$link = mysqli_connect($host, $user, $pwd, $db);
//MYSQL search statement
$query = "SELECT * FROM users WHERE username SOUNDS LIKE '%$searchTerm%' or fname SOUNDS LIKE '%$searchTerm%' or lname SOUNDS LIKE '%$searchTerm%'";
$results = mysqli_query($link, $query);
echo "<div class='searched'>Results for ";
echo $searchTerm;
echo "</div>";
/* check whethere there were matching records in the table
by counting the number of results returned */
if(mysqli_num_rows($results) >= 1)
{
$output = "";
while($row = mysqli_fetch_array($results))
{
$output .="<div class='user'><a href='profile?id=$row[id]'>";
$output .= "<img class='search_pp' src='" . $row['picture'] . "'/><br>";
$output .= "<div class='search_username'> " . $row['username'] . "</div>";
$output .= "<div class='search_full'>Full name: " . $row['fname'] . " " . $row['lname'] . "</div>";
$output .= "<div class='search_sex'>" . $row['sex'] . "</div></div></a>";
}
echo $output;
}
else
echo "No records of " . $searchTerm;
?>
这是用户名更改为"p"的地方
<?php echo htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8'); ?>
这就是链接中的id变为"p"的地方
<a href="profile?id=<?php echo htmlentities($_SESSION['user']['id'], ENT_QUOTES, 'UTF-8'); ?>">
然后整个站点的用户用户名和id都更改为'p'
1-您在哪里设置$_SESSION['user']
?
2-你能在刷新前后做一个var_dump($_SESSION['user']);
来看看发生了什么吗?