dongyoudi1342 2018-06-13 07:11
浏览 46

REST API中的会话原则

I need to control a session in a REST API. My REST API is implemented with slim and doctrine and within the routes.php file I have defined the POST request of the login:

    session_start();

    $app->post('/login', function ($request, $response) {
    $em = getEntityManager();
        $args = $request->getParsedBody() ?? json_decode($request->getBody(), true);
        $user = $em->getRepository(Usuario::class)->findOneByUsername($args['username']);
        if (null == $user) {
            echo "<script language='javascript'>alert('User not found'); window.location='App.php'</script>" ;

        } else {
            if ($user->getPassword() == $args['password']){
                $_SESSION['id'] = $user->getId();
                $_SESSION['username'] = $user->getUsername();
                if($user->getAdmin() && $user->getEnabled()){
                    echo "<script language='javascript'>window.location='Admin.php'</script>" ;
                } else if($user->getEnabled()){
                    echo "<script language='javascript'>window.location='Comparator.php'</script>" ;
                } else {
                    echo "<script language='javascript'>alert('Account 
inabilited'); window.location='App.php'</script>" ;
                }

            } else {
                echo "<script language='javascript'>alert('Incorrect Password'); window.location='App.php'</script>" ;
            }
        }
    });

I have 3 page .php. In page App.php I have an access form. I want to do is that when any user wants to enter the Admin or Comparator page, he can not enter without logging in previously. For this I have added the following code to the pages previously named:

<?php if (session_status() != "PHP_SESSION_ACTIVE" && session_status() != 2) { ?>
         <script>window.location='App.php'</script>
    <?php }?>

But when entering a valid username and password I get the following error:

Error

It's very weird because the field username in the class Usuario exists. Any solutions?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
    • ¥15 C# datagridview 单元格显示进度及值
    • ¥15 thinkphp6配合social login单点登录问题
    • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配