dongxiaoguang9108 2014-07-25 02:34
浏览 15
已采纳

在使用之前擦除$ _SESSION是否合适?

I am still new in session
Is it good to wipe $_SESSION before using it?

example:

$_SESSION         = array();
$_SESSION['id']   = 1;
$_SESSION['name'] = 'Someone';

I am asking opinion from you guys.
Because I don't have many experience in session.

In my awkward logic,
Maybe I will forgot to logout from admin session
and login to member session

So maybe some $_SESSION value from admin will still in $_SESSION array


Additional:
1. I was admin user and not logout yet from admin page.
2. Now I go from admin page to member login page

What should I do here?
Kick admin to the admin page because he is not member?

  • 写回答

1条回答 默认 最新

  • doutao6330 2014-07-25 02:41
    关注

    Nope. In fact, its really bad and your example code will render your sessions useless.

    When you call session_start() you are either given an empty $_SESSION or you get back the data you saved to $_SESSION on a previous page load. For more information on sessions check out the PHP docs:

    http://php.net/manual/en/book.session.php

    http://php.net/manual/en/function.session-start.php


    About logging in and out: Your logout process has to destroy whatever session data identifies the user (probably their ID). Typically this is done by using unset, i.e. unset($_SESSION['user_id']).

    I can't imagine any other way to log out a user, maybe if you provided more information I could give you a better answer about this.


    Regarding your addition it looks like your authentication system could use some work. You shouldn't be able to get to a login page when you are already logged in (even as admin, since its just another user with higher privileges, right?). If you manually type in the login url after you're logged in, then it should redirect you to the homepage.

    Here's Fantastic write-up on this topic, I shoulda done some research! Thanks @HamZa

    The definitive guide to form-based website authentication

    And here's my super basic pseudo code auth process:

    Does current page require authentication
        Yes:
            Is the user logged in?
                Yes:
                    Does the user have the correct privilages to view the page?
                        Yes:
                            AUTHENTICATED! Show page
                        No:
                            Print a message that says something like, "You're in the wrong place amigo"
                No:
                    Redirect to login
        No:
            Show the page
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效