duanlipeng4136 2013-10-22 17:58
浏览 390
已采纳

PHP登录:在登录和未登录用户之间显示不同内容的最佳方式

I'm learning PHP and I'm developing a quite simple website with authentication. As I don't think I'm good enough to make a secure authentication system (and anyway I don't have so much time), I searched and found this script http://php-login.net/ that seems to work perfectly. I'm using the "2-advanced" version of the script and in the index.php file there's something like this:

<?php
// load php-login components
require_once("php-login.php");

// create a login object. when this object is created, it will do all login/logout stuff automatically
// so this single line handles the entire login process.
$login = new Login();

// ... ask if we are logged in here:
if ($login->isUserLoggedIn() == true) {

    include("views/logged_in.php");

} else {

    include("views/not_logged_in.php");
}

So if the user is logged in it shows the contents of logged_in.php or not_logged_in.php if he's not (in the views directory there's an .htaccess that prevents the views to be accessed directly). Also other pages work this way (For example registration.php in the root directory includes the file views/registration.php in which there are the contents).

So here's my question: Is it more convenient to do it this way or (at least for the index) to make just one view and control single elements with something like this

if ($login->isUserLoggedIn() == true) { echo "you are logged in"
} else {echo "login form" }

for every element where it's needed? I think that for the client it would be basically the same but on the server-side I don't know and since I'm a beginner I wanted to ask. I'm sorry for my English but I hope you understood.

P.S.: I didn't understand if I can make "What's the best way" question on SO and I'sorry if I couldn't, next time I won't.

  • 写回答

1条回答 默认 最新

  • douzhu5900 2013-10-22 18:07
    关注

    My preferred method is to authenticate the user (however I wish, usually by hashed password in database) and then use sessions to track the login status. I can then set a variable like $_SESSION['loggedIn'] = true; and then test for that on subsequent calls to my script.

    E.g.

    if(isset($_SESSION['loggedIn'] && $_SESSION['loggedIn'] == true)) {
        //Redirect to welcome page
    } else {
        //Redirect to other page for users that are not logged in
    }
    

    If the user clicks a link to logout I can simply unset the flag.

    unset($_SESSION['loggedIn']);
    

    Bascially, depending on your needs, including security, there may be a few different ways you could go about doing this. Again though, for me, I authenticate the user with a hashed password stored in a database and then track the user's login status with a session variable.

    If what you have right now feels convenient and meets your needs, then you are good to go.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突