根据会话从数据库中获取数据

时间:2014-07-12 17:39:07

标签: php html

当您通过我的登录表单登录时,authentication.php将检查输入中的数据是否存在于数据库中。当匹配时,用户将被定向到他的角色的页面,因此假设用户是管理员,他将被定向到admin.php。当用户成功登录后,我想显示欢迎名字姓氏等消息。在我的数据库中,我有一个名为firstname的字段和一个名为lastname的字段。我希望有人可以帮助我,因为我似乎无法弄明白:(

authentication.php

<?php
    session_start();
    // Making a connection with the database.
    $mysqli=new MySQLi("localhost", "root", "root", "portfolio"); 
    $role=""; 

    // Declaring the username and password input.
    $username=filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING); 
    $password=filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRING); 

    // If role from members where username and password from inputs exicts in database bind parameters.
    // If given parameters not excists in database die
    if($query=$mysqli->prepare("SELECT `role` FROM members WHERE username=? AND password=?")) {
        $query->bind_param("ss", $username, $password);                                         
        $query->execute();                                                                
        $query->bind_result($role);
        $query->fetch();
    } else {                                                                                    
        echo "Errors in the Query. ".$mysqli->error;                                            
        die();
    }

    // If $role is filled make session for username to check if logged in and session role for redirect page.
    // If $role and $username is not filled invalid password, username combination.
    if($role!="") {                                                                           
        $_SESSION['ingelogt']=$username;                                                        
        $_SESSION['user_role']=$role;                                                 
        $location="$role.php";                                                                  
        header("location: $location");                                                          
    } else {                                                                                   
        echo "Invalid password, username combination";
        echo "<br/><a href='login.html'>Click to go back</a>";                                          
    }
?>

管理员将被定向到的页面名为admin.php

<?php
    session_start();
    // If session is not ingelogt lead back to index.php.
    if(!isset($_SESSION['ingelogt'])) {
        header("location: index.php"); 
    }

    // The role that has access to this page.
    $page_role="admin"; 
    $role=$_SESSION['user_role'];
    // If a user with a different role visits wrong page.
    if($role!=$page_role)
    {
        echo "You are not supposed to be here.";
        die();
    }

    // Start new DOMDocument and load html file.
    $dom = new DOMDocument();
    libxml_use_internal_errors(true);
    $dom->loadHTMLFile("admin.html");
    libxml_use_internal_errors(false);

    // If user is logged in add logg out icon in the menu.
    if($_SESSION['ingelogt']) {
        $oUl = $dom->getElementById('navUl');
            $oList = $dom->createElement('li');

                $oLink = $dom->createElement('a');
                $oLink->setAttribute('href','logout.php');

                    $oI = $dom->createElement('i');
                    $oI->setAttribute('class','icon-logout');

                    $oLink->appendChild($oI);

                $oList->appendChild($oLink);

            $oUl->appendChild($oList);
    }
    // Save DOMDocument with html document.
    echo $dom->saveHTML();
?>

1 个答案:

答案 0 :(得分:1)

如果我以任何方式误解你,请给我一个提示,我会删除这个答案。虽然我认为你想要做的是根据用户的名字和姓氏在页面的某个地方打印问候语。

基本上,一旦声明了$_SESSION - 元素,就可以在不同的页面访问它(类似于$_COOKIE,但不相同)。因此,最好的解决方案是使用从数据库收到的名字和姓氏初始化$_SESSION个变量,然后在所需的页面上打印这些变量(与您一起使用的方法相同) )。

首先,您需要获取数据库中的名称,这可以通过将 authentication.php 中的if - 语句更改为以下内容来完成:

if($query=$mysqli->prepare("SELECT `role`, `firstname`, `lastname` FROM members WHERE username=? AND password=?")) //assuming that your columns are called `firstname` and `lastname`

要获取这些内容,您还需要将行进一步更改为:

$query->bind_result($role, $first, $last);

在下一行使用fetch时,您的变量将被放入适当的绑定变量中。因此,在该声明之后,您可以执行以下操作(最好在$_SESSION['user_role']=$role;之后):

$_SESSION["firstname"] = $first;
$_SESSION["lastname"] = $last;

在此之后,你可以echo在任何你想要的名字和姓氏(这取决于你希望它放在哪里......)。例如,如果您希望它显示在 admin.php 的顶部,您只需将其放在$dom = new DOMDocument();之前:

echo "Hello " . $_SESSION["firstname"] . " " . $_SESSION["lastname"] . "!";

如果您对哪里放东西感到困惑,请尝试重新阅读指定的说明。我的大多数示例都只是要替换的东西(在这种情况下,您只需要找到相应的代码),如果不是这样,我会尝试重定向您。虽然知道这些事情很重要,但如果没有正确掌握代码,我建议您尝试理解。

相关问题