简体   繁体   English

PHP-获取当前用户ID以存储在会话中?

[英]Php - get current user id to store in a session?

I'm building a small form and I'm having trouble figuring out how to link user data to what is submitted to the database. 我正在构建一个小型表单,但在弄清楚如何将用户数据链接到提交给数据库的内容时遇到了麻烦。 For instance, I can process user data into my db but I need to link the data they submit to their user id. 例如,我可以将用户数据处理到我的数据库中,但是我需要将他们提交的数据链接到其用户ID。 Out of 6 tables, I have an "author" and "article" table both with "id" field sets and would like to link each author to the articles they submit by getting their id once logged in and storing it in a session, but am unsure as how to actually select and store their id. 在6个表格中,我有一个“作者”和“文章”表格,都带有“ id”字段集,并希望通过登录一次ID并将其存储在会话中来将每个作者链接到他们提交的文章。不确定如何实际选择和存储其ID。 Here is the portion of my controller where I'd like to add a session for user id: 这是我控制器的一部分,我想为用户ID添加会话:

<?php
include_once $_SERVER['DOCUMENT_ROOT'] .
        '/includes/magicquotes.inc.php';

require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/access.inc.php';

if (!userIsLoggedIn())
{
    include '../login.html.php';
    exit();
}
if (isset($_GET['add']))
{
    $pagetitle = 'New Article';
    $action = 'addform';
    $text = '';
    $authorid = '';
    $id = '';
    $button = 'Add article';

    include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
...........

Any thoughts? 有什么想法吗?

EDIT- 编辑-

Author table with stored id: 具有存储ID的作者表:

id  name    email           password
1   John    John@mail.com   d8a6582c02d188df9ad89a6affa412f7

In the login function, retrieve the author id from the db and store in a new session variable. 在登录功能中,从数据库中检索作者ID并将其存储在新的会话变量中。 http://www.php.net/manual/en/session.examples.basic.php http://www.php.net/manual/zh/session.examples.basic.php

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

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