php - 我的 friend 系统为每个用户更改 session

标签 php html mysql

我正在创建一个 friend 系统,但它有一个名为 profile.php 的页面。该页面供系统的每个成员使用。看起来像

            <!DOCTYPE html>
<html>
<head>
<title>Profile</title>
<link rel='stylesheet' href='style.css' />
</head>
<body>
<?php include 'connect.php'; ?>

<?php include 'functions.php'; ?>

<?php include 'header.php';  ?>

<center><div class = 'user'>
<?php
if(isset($_GET['user']) && !empty($_GET['user'])) {
    $user = $_GET['user'];

} else {
    $user = $_SESSION['user_id'];
}

$my_id = $_SESSION['user_id'];
$username = getuser($user, 'username');

?>  

<h3><?php echo $username; ?></h3>

<?php
if($user != $my_id) {
    $check_frnd_query = mysql_query("SELECT id FROM frnds WHERE (user_one='$my_id' AND    user_two='$user') OR (user_one = '$user' AND user_two='$my_id')");
    if(mysql_num_rows($check_frnd_query) == 1) {
        echo "<a href = '#' class = 'box'>Already Friends</a>  |  <a href = 'actions.php?action=unfrnd&user=$user' class = 'box'>Unfriend $username</a>"; //This is where the profile page design will go.
    } else {
        $from_query = mysql_query("SELECT `id` FROM `frnd_req` WHERE `from`='$user' AND `to` = '$my_id'");
        $to_query = mysql_query("SELECT `id` FROM `frnd_req` WHERE `from`='$my_id' AND `to`='$user'");
        if(mysql_num_rows($from_query) == 1){
            echo "<a href = '#' class = 'box'>Ignore</a> | <a href = 'actions.php?action=accept&user=$user' class = 'box'>Accept</a>";
        } else if (mysql_num_rows($to_query) == 1) {
            echo "<a href = 'actions.php?action=cancel&user=$user' class = 'box'>Cancel Request</a>";
        } else {
            echo "<a href = 'actions.php?action=send&user=$user' class = 'box'>Send Friend Request</a>";
        }       
    }
    ?>
    </div></center>
    <div id = 'sidebar'>    
<ul>
    <?php  

        $my_id = $_SESSION['user_id'];
    $frnd_query = mysql_query("SELECT user_one, user_two FROM frnds WHERE user_one =     '$my_id' OR user_two = '$my_id'");
    while($run_frnd = mysql_fetch_array($frnd_query)) {
        $user_one = $run_frnd['user_one'];
        $user_two = $run_frnd['user_two'];
        if($user_one == $my_id) {
            $user = $user_two;
        } else {
            $user = $user_one;
        }
        $username = getuser($user, 'username');
        echo "<li><a href = 'my_ideas.php?user=$user' style=display:block;>Ideas</a>         </li>";

    ?>


    <li><a href = '#my_links'>Linked Ideas</a></li>
    <li><a href = 'settings.php'>Profile Settings</a></li>
</ul>
</div>
<?php
}   
} else  {

?>



<div id = 'sidebar'>
<ul>
    <li><a href = 'my_ideas.php'>My Ideas</a></li>
    <li><a href = '#my_links'>Linked Ideas</a></li>
    <li><a href = 'settings.php'>Profile Settings</a></li>
</ul>

</div>

<?php  } ?>     

<div id = 'user_content'>

<p>This is where the user_content should go.</p>

</div>  

</body>

</html>

// That is where it ends the site formatted it weird.  Also, the function get user is

function getuser($id, $field) {
    $query = mysql_query("SELECT $field FROM users WHERE id='$id'");
    $run = mysql_fetch_array($query);
    return $run[$field];
}

这样做的问题是,每次我进入一个不是我登录的用户的用户页面时,该网站都会将我注销并以其他用户的身份重新登录。我能做些什么来解决这个问题吗?

最佳答案

你需要session_start();。开始 session 。

关于php - 我的 friend 系统为每个用户更改 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21833731/

相关文章:

mysql - 创建具有多个支持文件的安装程序

php - 如何使用PHP打印此模式?

python - Django 中间件 - 如何编辑 Django 响应对象的 HTML?

iphone - 在 Safari iPhone 4.3 上通过 HTTPS 播放 HTML5 音频

javascript - Jquery无法选择表单内的输入元素,但在表单外时选择

php - 从具有多个 ID 的同一个 Mysql 列中获取数据

mysql - 在 INSERT 期间省略 "NOT NULL"值会将其默认为 0。为什么?

php - while 循环为空 php mysql

php - 您如何在无法重新启动的服务器(Apache)上使用 gettext?

php - 将值插入两个链接的表中 (1 :M)