如果会话是在php中设置的,如何查看其他用户的配置文件


How to view other users profile if session is set in php

我正试图创建一种社交网络,但我的会话出现了问题。所以我无法访问其他用户的个人资料。这是我的loginwebsite1.php 代码

<?php
ob_start();
session_start();
$connection = mysqli_connect('localhost', 'root', '123456789', 'register');
if (isset($_POST['email1'])) {
    $email = mysqli_real_escape_string($connection, htmlentities($_POST['email1']));
}
if (isset($_POST['password1'])) {
    $password = mysqli_real_escape_string($connection, htmlentities($_POST['password1']));
}
if (!empty($email) && !empty($password)) {
    $query  = "select id from register where email='$email' and password='$password'";
    $result = mysqli_query($connection, $query);
    $row    = mysqli_fetch_array($result);
    if ($row > 0) {
        $_SESSION['id'] = $row;
        $_GET['id']     = $row;
        header('location: new.php');
    } else {
        echo "sorry but the email-id or password is wrong";
    }
} else {
    echo "please enter your email-id or password or there";
}
?> 

我的会话代码如下:

<?php
ob_start();
session_start();
if (isset($_SESSION['id']) && !empty($_SESSION['id'])) {
    $id = $_SESSION['id'];
    foreach ($id as $fn)
        $connection = mysqli_connect('localhost', 'id', 'password', 'register');
    $query     = "select firstname,lastname from register where id='$fn'";
    $result    = mysqli_query($connection, $query);
    $row       = mysqli_fetch_array($result);
    $firstname = $row['firstname'];
    $lastname  = $row['lastname'];
} else {
    header('location: loginwebsite1.php');
}
?> 

但当我尝试像profile.php?id=9一样输入时,它仍然会打开登录用户的配置文件。

$id=$_SESSION['id'];

我相信这条线就是你的问题所在。在这里,当您应该从$_get变量中获取配置文件页面时,您可以从$_SESSION变量中获取要加载的配置文件页面的ID号。

它应该是:

$id=$_GET['id'];

您还应该检查$_GET['id']是否也设置为这样的

$connection=mysqli_connect('localhost','id','password','register');
if(isset($_SESSION['id']) && !empty($_SESSION['id']) || !empty($_GET['id']))
{
    $id=!empty($_GET['id'])? $_GET['id']: $_SESSION['id'];
    foreach($id as $fn){            
        $query="select firstname,lastname from register where id='$fn'";
        $result=mysqli_query($connection,$query);
        $row=mysqli_fetch_array($result); 
        $firstname=$row['firstname'];
        $lastname=$row['lastname'];
    }
}else{
    header('location: loginwebsite1.php');
}

还将$connection变量设置为只连接到数据库一次。