PHP&;MySQL存储用户ID登录后完成会话


PHP & MySQL store userid after login complete sessions

好吧,这是我的login.php页面的php代码文件:

<?php
    if (isset($_GET['action'])) {
        switch (strtolower($_GET['action'])) {
            case 'login':
                if (isset($_POST['username']) && isset($_POST['password'])) {
                    if (!validateUser($_POST['username'], $_POST['password'])) {
                        $_SESSION['error'] = "Bad username or password supplied.";
                        unset($_GET['action']);
                    }
                }else {
                    $_SESSION['error'] = "Username and Password are required to login.";
                    unset($_GET['action']);
                }           
            break;
            case 'logout':
                if (loggedIn()) {
                    logoutUser();
                    header ("Location: ".$domainNow);
                }else {
                    unset($_GET['action']);
                }
            break;
        }
    }
    if (loggedIn()) {
        header ("Location: ".$domainNow);
    }elseif (!isset($_GET['action'])) {
        $sUsername = "";
        if (isset($_POST['username'])) {
            $sUsername = $_POST['username'];
        }
        $sError = "";
        if (isset($_SESSION['error'])) {
            $sError = '<span id="error">' . $_SESSION['error'] . '</span><br />';
        }
        $sOutput .= '<h2>Login to NextGenStep</h2><br />
                ' . $sError . '
                <form name="login" method="post" action="/user-action/login">
                    Username: <input type="text" name="username" value="' . $sUsername . '" /><br />
                    Password: &nbsp;<input type="password" name="password" value="" /><br /><br />
                    <div style="padding-left:71px;"><input type="submit" name="submit" value="Login!" /></div>
                </form>
            <div style="padding-left:131px;"><h4>Create a new <a href="/register">account</a>?</h4></div>';
    }
    echo $sOutput;
?>

我是PHP和MySQL的新手,所以我想在用户登录后从数据库中获取用户id(列名为uid),但我也希望它将用户id存储在会话中,这样我就可以在其他页面上使用它。我已经session_start();在包含文件上声明,这样我就不必在每一页上手动执行。

此外,这是我的包含页面上的loggedIn函数:

function loggedIn() {
if (isset($_SESSION['loggedin']) && isset($_SESSION['username'])) {
    return true;
}

只需将UID保存在会话变量中,例如;

$_SESSION['uid'] = $uid;

在尝试使用$_SESSION变量之前,请记住使用session_start();,否则它将是未定义的。

顺便说一下,loggedIn()函数应该有一个假返回和一个真返回。我会用这个;

function loggedIn() {
    return ((isset($_SESSION['loggedin']) and isset($_SESSION['username'])) ? true : false); 
}

希望这能有所帮助。

要从数据库中检索您的UID,只要它已经写在那里而不是空的,请尝试(好吧,空的或不空的):

class FetchUID
    {
        public static function execute($_uid='')
            {
                if(!empty($_uid)) {
                        // I am pressuming you have a global database engine
                        // If not, retrieve your engine however at this point.
                        // For sake of ease, I am pretending it's a global....
                        global $con;
                        // I use PDO so this is how I would get it
                        // I am also making the statement broad just to make sure it is retrieving at all
                        $query  =   $con->prepare("select * from users where username ='$_uid'");
                        $query->execute();
                        if($query->rowCount() == 1)
                            $user_info  =   $query->fetch(PDO::FETCH_ASSOC);
                        else
                            $user_info['uid']   =   self::Write();
                    }
                return (isset($user_info['uid']) && !empty($user_info['uid']))? $user_info['uid']:'error';
            }
        protected static function Write()
            {
                // Code here that creates the custom uid and returns it again
            }
    }
// Check that the session->username is set + that the session->uid is NOT set
if(isset($_SESSION['username']) && !isset($_SESSION['uid'])) {
        // If set, fetch it via above class & methods
        $uid    =   FetchUID::execute($_SESSION['username']);
        // Write your $uid
        echo $uid;
        // Sessionize it
        $_SESSION['uid'] = $uid;
    }