dongzang7182 2015-10-23 06:20
浏览 23
已采纳

检查用户是否已登录并且与配置文件管理中的名称相同

So I really don't know what I'm going to set the title on this one to, generally couldn' come up with anything good. Still though, if you are reading this I will do my best to explain it here.

I am trying to check if the currently logged in user equals to the one on the current profile management page, an example:

Currently logged in user: Bob
If bob is looking at his own profile page, he should see a "Edit" button, however, if he is looking at the profile page of "Peter", he should not. I've been trying a lot of different ways, the one I thought would work was doing a if session equals username variable, but with no result.

The issue I am looking at is, either it displays nothing at all, a blank page as far as that goes (except for the background and whatnot), OR Bob as used in the example has access to all edit buttons, which isn't right either.

Also, What I have right now if its to any use:

DB connection:

<?php
// error_reporting(E_ALL);
session_start();

$host = 'HOSTNAME';
$dbusername = 'DATABASENAME';
$dbpassword = 'PASSWORD';

$anslutning = mysqli_connect($host, $dbusername, $dbpassword) or die("<b>Could not connect to database server</b>");

$anslutning->select_db('DATABASE NAME') or die("<b>Could not connect to the specified database</b>");

?>

The profile management page this far:

if(isset($_GET['manage'])) {


                $manage = $_GET['manage'];


                $editAccount = $anslutning->prepare('SELECT userId, username, email, gender, age, profilePic FROM (tablename) WHERE userId=? LIMIT 1');
                $editAccount->bind_param("i", $manage);
                $editAccount->bind_result($userId, $username, $email, $gender, $age, $profilePic);
                $editAccount->store_result();
                $editAccount->execute();

                echo '<h2 class="accountm_title">Account management</h2>';

                if(isset($_SESSION['loggedIn'])) {

                echo '


                <i><a class="edit" href="index.php?editAccount='.$userId.'">Edit</a></i>
                <hr size="1" width="30%" class="manageHr">
                <div class="accountm">
                ';
            }
                while($row = $editAccount->fetch()) {

                    echo '


                    <form action="index.php" method="GET">
                    <p>Username: '.$username.' &nbsp; &nbsp; </p>
                    <p>Email: '.$email.' &nbsp; &nbsp; </p>
                    <p>Gender: '.$gender.'</p>
                    <p>Age: '.$age.' </p>
                    <p>Profile picture: <img src="'.$profilePic.'"></p>
                    </form>

                    ';



            }
        }

Then some code updating the database with the fields, which seems to work.

  • 写回答

1条回答 默认 最新

  • duanhan8757 2015-10-23 06:30
    关注

    After user logged in the system you will have a session to store userId, profile pages like this : http://example.com/editAccount=userId and you only show the Edit button when userId in session equal userId of profile page.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?