繁体   English   中英

PHP登录和会话从数据库获取数据

[英]PHP login and session get data from databse

这是我登录时的代码。 登录后,我想将userid作为主键来传递另一个页面。 我尝试了很多代码,但仍然无法正常工作。 请帮我

if(isset($_POST['login']))
    {
        $email=$_POST['email'];
        $password=md5($_POST['password']);
        $sql ="SELECT * FROM tbluser WHERE EmailAddress=:email and uPassword=:password";
        $query= $dbh -> prepare($sql);
        $query-> bindParam(':email', $email, PDO::PARAM_STR);
        $query-> bindParam(':password', $password, PDO::PARAM_STR);
        $query-> execute();
        $results=$query->fetchAll(PDO::FETCH_OBJ);
        if($query->rowCount() > 0)
        {
            $_SESSION['login']=$_POST['email'];
            $_SESSION['fname']=$results->FullName;
            $_SESSION['uid']=$results['UserID'];
            $currentpage=$_SERVER['REQUEST_URI'];
            echo "<script type='text/javascript'> document.location = '$currentpage'; </script>";
        } else{

            echo "<script>alert('Invalid Details');</script>";

        }

    }

以及如何在另一个页面中调用用户ID。

你需要session_start()

if($query->rowCount() > 0)
            {
                session_start();
                $_SESSION['login']=$_POST['email'];
                $_SESSION['fname']=$results->FullName;
                $_SESSION['uid']=$results['UserID'];
                $currentpage=$_SERVER['REQUEST_URI'];
                echo "<script type='text/javascript'> document.location = '$currentpage'; </script>";
            }

然后在另一页上使用session_start()启动会话,它将正常工作。

这就是您使用会话的方式,将其放在开头或您需要的每个php文件中

 <?php

        session_start();

        include("./Models/db_connect.php");
        include('./Controllers/Functions/PHP/messages.php');
        include('./Models/actual_date.php');
        $actual_date = get_date($db);

        switch(isset($_POST['login'])):  
            case 'Register':
            $email = htmlspecialchars(trim($_POST['em']), ENT_QUOTES, 'UTF-8');
            $password = htmlspecialchars(trim($_POST['pw']), ENT_QUOTES, 'UTF-8');

            // check if the combination fname/lname/email is already used
            include('./Models/log_check.php');
            unset($_SESSION['ID'],$_SESSION['role']);
            $_SESSION['ID'] = $row['ID'];
            $_SESSION['role'] = $row['role'];

            if(intval($row['ID']) > 0){
                include('./Models/status_update.php');
                $successmsg = "Connexion réussie! Redirection en cours";
                header('refresh:5;url=./index.php?page=Lobby');
                if($_SESSION['role'] === 'vet') {
                    include './Views/html_top_vets.php'; 
                } else {
                    include './Views/html_top_clients.php'; 
                }

                include('./Views/lobby.php');
            } else {
                $errormsg = "   
                <p>Vous n'avez pas de compte ou la combinaison est incorrecte!</p>";
                include('./Views/html_top_login.php');
                include('./Views/login.php');
            }
            break;
            default:
            include('./Views/html_top_login.php');
                include('./Views/login.php');
        endswitch;
    ?>

暂无
暂无

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

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