简体   繁体   English

获取当前登录用户的数据

[英]Get data of current user logged in

In my db I have a table, "article" with a column "authorid" and a table, "author" with a column "id". 在我的数据库中,我有一个表“ article”,其列为“ authorid”,一个表,“ author”,其列为“ id”。 The two tables are joined by these columns so when a user submits an article, the "article" table receives the article while displaying the id of the author in the "authorid" column. 这两个表通过这些列连接在一起,因此,当用户提交文章时,“文章”表会在接收到该文章的同时在“ authorid”列中显示作者的ID。 However, I can't get the "authorid" column to update with the current users id. 但是,我无法使用当前用户ID更新“ authorid”列。 Whenever a user submits an article, the "authorid" column returns 0 instead of displaying their id defined in "id" column of "author". 每当用户提交文章时,“ authorid”列将返回0,而不是显示在“ author”的“ id”列中定义的ID。

**PHP**
   if (isset($_GET['add']))
      if (!userIsLoggedIn())
        include $_SERVER['DOCUMENT_ROOT'] . '/includes/login.inc.html.php';
        exit();
    }
        include 'form.html.php';
        exit();

   if (isset($_GET['addform']))
    {
        include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';

        $text = mysqli_real_escape_string($link, $_POST['text']);
        $author = mysqli_real_escape_string($link, $_POST['author']);

        $sql = "INSERT INTO article SET
                articletext='$text',
                articledate=CURDATE(),
                authorid='$author'";
        if (!mysqli_query($link, $sql))
        {
            $error = 'Error adding submitted article: ' . mysqli_error($link);
            include 'error.html.php';
            exit();
        }

        header('Location: .');
        exit();
    }

I think I need to select user data and store in an array then store the array in $author variable but I'm unsure how to get the user data of the current user logged in. Here is my userIsLoggedIn function: 我想我需要选择用户数据并将其存储在一个数组中,然后将该数组存储在$ author变量中,但是我不确定如何获取当前登录用户的用户数据。这是我的userIsLoggedIn函数:

function userIsLoggedIn()
{
    if (isset($_POST['action']) and $_POST['action'] == 'login')
    {
        if (!isset($_POST['email']) or $_POST['email'] == '' or
            !isset($_POST['password']) or $_POST['password'] == '')
        {
            $GLOBALS['loginError'] = 'Please fill in both fields';
            return FALSE;
        }

        $password = md5($_POST['password'] . 'chainfire db');

        if (databaseContainsAuthor($_POST['email'], $password))
        {
            session_start();
            $_SESSION['loggedIn'] = TRUE;
            $_SESSION['email'] = $_POST['email'];
            $_SESSION['password'] = $password;
            return TRUE;
        }
        else
        {
            session_start();
            unset($_SESSION['loggedIn']);
            unset($_SESSION['email']);
            unset($_SESSION['password']);
            $GLOBALS['loginError'] =
                    'The specified email address or password was incorrect.';
            return FALSE;
        }
    }

    if (isset($_POST['action']) and $_POST['action'] == 'logout')
    {
        session_start();
        unset($_SESSION['loggedIn']);
        unset($_SESSION['email']);
        unset($_SESSION['password']);
        header('Location: ' . $_POST['goto']);
        exit();
    }

    session_start();
    if (isset($_SESSION['loggedIn']))
    {
        return databaseContainsAuthor($_SESSION['email'], $_SESSION['password']);
    }
}

The two tables are joined by "authorid" in "article" and "id" in "author", as you can see, I cant get "authorid" to update with "id" because, like i said above, I don't know how to get the user id of the current user logged in and store that id in $authors . 如您所见,这两个表由“文章”中的“ authorid”和“作者”中的“ id”连接在一起,如您所见,我无法获得“ authorid”来更新“ id”,因为如上所述,知道如何获取当前登录用户的用户ID并将该ID存储在$authors

**article table**
id  articletext     articledate     authorid
1   Test article    2011-08-05      0

**author table**
id  name    email           password
1   user    user@mail.com   d8a6582c02d188df9ad89a6affa412f7

Any help would be greatly appreciated, thanks! 任何帮助将不胜感激,谢谢!

For starters, your curly brackets are a bit odd. 对于初学者来说,大括号有点奇怪。 Try ... 尝试...

if (isset($_GET['add'])) {
    if (!userIsLoggedIn()) {
        include $_SERVER['DOCUMENT_ROOT'] . '/includes/login.inc.html.php';
        exit();
    }
    include 'form.html.php';
    exit();
}

... Not sure this will fix your problem, but as it stands, your code will exit; ...不确定这是否可以解决您的问题,但是就目前而言,您的代码将exit; before it ever hits if (isset($_GET['addform'])) 之前是否命中if (isset($_GET['addform']))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM