[英]User profile data does not appear after logging in
我正在創建一個社交網絡網站,每個用戶都有自己的個人資料,但是登錄時出現問題,個人資料頁面沒有出現。 我使用過cookie和會話,對此問題進行了大量研究,但沒有成功,因此我認為問題出在cookie中。 我不知道如何解決; 如果有人可以幫助我,我將不勝感激。
<?php
ob_start();
require_once('for members/scripts/global.php');
if($logged == 1){
echo("you need to be loged in to view profiles");
exit();
}
if(isset($_GET['id'])){
$id=$_GET['id'];
$id= preg_replace("#[^0-9]#","",$id);
}else{
$id=$_SESSION['id'];
}
//collect member information
$query = mysql_query("SELECT * FROM members WHERE id='$id'LIMIT 1") or die("could not collect user information ");
$count_mem = mysql_num_rows($query);
if($count_mem == 0){
echo("the user does not exit");
exit();
}
while($row = mysql_fetch_array($query)){
$username = $row['username'];
$fname = $row['firstname'];
$lname = $row['lastname'];
$profile_id= $row['id'];
if($session_id == $profile_id){
$owner = true;
}else{
$owner = false;
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php print("$fname"); ?> <?php print("$lname"); ?>'s profile</title>
<link href="style/stylesheet.css" type="text/css"/>
</head>
<body>
<div class="container center">
<h1><?php print("$username"); ?></h1>
<?php
if($owner == true ){
header("Location: profile.php");
?>
<!--
<a href="#">edit profile</a><br />
<a href="#">account settings</a><br />
-->
<?php
}else{
header("Location: index.php");
?>
<!--
<a href="#">private message</a><br />
<a href="#">add as friend</a><br />
-->
<?php
}
?>
</div>
</body>
</html>
<?php flush(); ?>
如果您需要其他相關代碼,請告訴我。 謝謝。
您顯示的代碼有很多錯誤。 對於初學者, 請勿使用mysql_
函數。 從PHP手冊
自PHP 5.5.0起不推薦使用該擴展,不建議編寫新代碼,因為將來會刪除該擴展。 相反,應使用mysqli或PDO_MySQL擴展名。
其次,您的header
重定向被嵌入到HTML中,這是一種不好的做法,並且只由ob_start()
保存。 有了這個,您就有條件將重定向到“ profile.php”或“ index.php”,但很幸運,您將重定向到“ index.php”,否則您將擁有一個永久的自我重定向頁面。
我看不到是否/在哪里設置了變量$session_id
,但是從可見的角度來看,它為null
且永遠不會== $profile_id
,因此$owner
始終為false
。
這樣,您就可以while
獲取一行時有一個while
循環...刪除它,不需要它。
現在了解代碼中的一些邏輯。 如果您必須是個人資料所有者才能查看它,請在查詢后立即檢查;如果不是所有者,請檢查header("Location: index.php"); die;
header("Location: index.php"); die;
並且沒有else
,其后的任何內容表示該個人資料所有者正在查看該頁面。
另外,您需要確保session_start();
如果您計划使用會話變量,則位於頁面頂部。 你有ob_start();
在那里,但最后您調用flush()
。 閱讀ob_start()並為您啟動的緩沖區調用適當的刷新函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.