登录后不显示用户个人资料数据 [英] User profile data does not appear after logging in

查看:196
本文介绍了登录后不显示用户个人资料数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



profile.php



 <?php 
ob_start();
require_once('for members / scripts / global.php');

if($ logged == 1){
echo(你需要登录查看个人资料);
exit();
}
if(isset($ _ GET ['id'])){
$ id = $ _ GET ['id'];
$ id = preg_replace(#[^ 0-9]#,,$ id);

} else {
$ id = $ _ SESSION ['id'];
}
//收集成员信息
$ query = mysql_query(SELECT * FROM members WHERE id ='$ id'LIMIT 1)或者(无法收集用户信息) ;
$ count_mem = mysql_num_rows($ query);
if($ count_mem == 0){
echo(用户不退出);
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 // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional。 dtd>
< html xmlns =http://www.w3.org/1999/xhtml>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8/>
< title><?php print($ fname); ?> <?php print($ lname); ?>的个人资料< / title>
< link href =style / stylesheet.csstype =text / css/>
< / head>

< body>
< div class =container center>
< h1><?php print($ username); ?>< / h1>
<?php
if($ owner == true){
header(Location:profile.php);
?>
<! -
< a href =#>编辑个人资料< / a>< br /
< a href =#>帐户设置< / a>< br />
- >
<?php
} else {
header(Location:index.php);
?>
<! -
< a href =#>私人讯息< / a>< br /
< a href =#>添加为朋友< / a>< br />
- >
<?php
}
?>
< / div>
< / body>
< / html>
<?php flush(); ?>

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

解决方案

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


此扩展程序从PHP 5.5.0开始弃用,并且不建议写入新代码,因为它将来会被删除。而是 mysqli PDO_MySQL 扩展名。


其次,你的重定向嵌入在你的HTML,这是不好的做法,你只有保存 ob_start ()。有了这个,虽然,你有一个条件,将重定向到profile.php或index.php,幸运你被重定向到index.php,否则你会有一个永远的自我重定向页面。 / p>

我看不到你/你在哪里设置变量 $ session_id ,但从可以看出,它 null 并且永远不会 == $ profile_id ,因此 $ owner 将总是 false



code>循环,当获取一行...删除它,不需要它。



现在对于你的代码中的一些逻辑。如果您必须是个人资料所有者才能查看,请在查询后立即检查,如果不是所有者,请头(Location:index.php); ,, c>,其后的任何内容意味着是配置文件所有者查看页面。



此外,您需要确保 session_start(); 位于页面顶部使用会话变量。你有 ob_start(); 在那里,但最后你调用 flush()。阅读 ob_start(),并调用适当的flush函数你开始的缓冲区。


I am creating a social network website where each user has his own profile, but there is a problem when I log in, the profile page does not appear. I used cookies and sessions I did lot of research about the problem but without any success, so I think that the problem is in the cookies. I do not know how to fix it; if anyone can help me, I will appreciate that.

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

If you need other related code, let me know. Thank you.

解决方案

There are quite a few things wrong with the code that you have displayed. For starters, Do not use mysql_ functions. From the PHP manual

This extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used.

Secondly, your header redirects are imbedded in your HTML, which is bad practice and you've only been saved by ob_start(). With that though, you have a conditional that will either redirect to 'profile.php' or 'index.php', be lucky you get redirected to 'index.php', otherwise you'd have a forever self-redirecting page.

I can't see if/where you ever set the variable $session_id, but from what can be seen, it's null and will never == $profile_id, so $owner will always be false.

With that, you have a while loop while fetching one row...remove it, no need for it.

Now for some of the logic in your code. If you have to be the profile owner in order to view it, check that immediately after your query, and if not the owner, header("Location: index.php"); die; and don't have an else, anything following it means that it's the profile owner viewing the page.

Also, you need to make sure session_start(); is at the top of the page if you plan on using the session variables. You have ob_start(); up there, but at the end you call flush(). Read up on ob_start() and call the proper flush function for the buffer you started.

这篇关于登录后不显示用户个人资料数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆