获取当前用户登录的数据

时间:2011-08-06 11:03:27

标签: php mysql

在我的数据库中,我有一个表,“article”带有一列“authorid”和一个表,“author”带有一列“id”。这两个表由这些列连接,因此当用户提交文章时,“article”表接收文章,同时在“authorid”列中显示作者的id。但是,我无法使用当前用户ID更新“authorid”列。每当用户提交文章时,“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();
    }

我想我需要选择用户数据并存储在数组中,然后将数组存储在$ 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']);
    }
}

这两个表由“author”中的“authorid”和“作者”中的“id”连接,正如您所看到的,我无法通过“id”更新“authorid”,因为,就像我上面说的那样,不知道如何获取当前登录用户的用户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

非常感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

对于初学者来说,你的花括号有点奇怪。试试......

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

...不确定这会解决您的问题,但就目前情况而言,您的代码会exit;点击之前if (isset($_GET['addform']))