0

我正在创建一个社交网络网站,每个用户都有自己的个人资料,但是当我登录时出现问题,个人资料页面没有出现。我使用了 cookie 和会话,我对这个问题做了很多研究,但没有任何成功,所以我认为问题出在 cookie 上。我不知道如何解决它;如果有人可以帮助我,我将不胜感激。

配置文件.php

<?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(); ?>

如果您需要其他相关代码,请告诉我。谢谢你。

4

1 回答 1

0

您显示的代码有很多问题。对于初学者,不要使用mysql_函数。 来自PHP 手册

此扩展自 PHP 5.5.0 起已弃用,不推荐用于编写新代码,因为它将在未来被删除。相反,应该使用mysqliPDO_MySQL扩展。

其次,您的header重定向嵌入在您的 HTML 中,这是一种不好的做法,您只被ob_start(). 尽管如此,你有一个条件可以重定向到'profile.php'或'index.php',幸运的是你被重定向到'index.php',否则你将拥有一个永远自我重定向的页面。

我看不到您是否/在哪里设置了变量$session_id,但从可以看到的情况来看,它是null并且永远不会== $profile_id,所以$owner永远都是false

有了这个,你while在获取一行时有一个循环......删除它,不需要它。

现在了解代码中的一些逻辑。如果您必须成为个人资料所有者才能查看它,请在查询后立即检查,如果不是所有者,header("Location: index.php"); die;并且没有else,则后面的任何内容都表示它是查看页面的个人资料所有者。

此外,如果您打算使用会话变量,则需要确保session_start();位于页面顶部。你在ob_start();那里,但最后你打电话给flush(). 阅读ob_start()并为您启动的缓冲区调用正确的刷新函数。

于 2013-02-24T01:57:11.317 回答