简体   繁体   English

根据用户是否登录显示对象

[英]Displaying objects based on if a user is logged in or not

I'm learning about PHP sessions for user authentication on my website. 我正在网站上学习用于用户身份验证的PHP会话。 I know how to restrict the viewing of a complete page using sessions (simply check if the 'uid' session variable is set and if it is, show content, if not redirect to an error). 我知道如何使用会话来限制对整个页面的查看(只需检查是否设置了'uid'会话变量,如果已设置,则显示内容,如果没有重定向到错误,则显示内容)。

However I'm trying to figure out the best way to selectively show and hide different objects (div, text, images) based on if a user is logged in or not. 但是,我试图找出根据用户是否登录而有选择地显示和隐藏不同对象(div,文本,图像)的最佳方法。 Is it as simple as checking for the 'uid' session variable and displaying based on if it set or not? 它是否像检查“ uid”会话变量并根据是否设置就显示那样简单? Is there a more efficient way of doing this id there are a lot of conditional elements on a page? 有没有更有效的方法来执行此ID,页面上有很多条件元素?

You control sessions so you can choose what to check for. 您可以控制会话,因此可以选择要检查的内容。

$logged = isset($_SESSION['uid']);

then the html condition: 那么html条件:

<?php if($logged): ?>
<div>logged in content</div>
<?php endif; ?>

or without storing a temporary variable: 或者不存储临时变量:

<?php if(isset($_SESSION['uid'])): ?>
<div>logged in content</div>
<?php endif; ?>

Just use if statements within the script 只需在脚本中使用if语句

<?
    if(isset($_SESSION['uid'])){
?>
<div>
...
</div>
<?
    }
?>

As far as keeping things clean you really what to try to separate your html and php as much as possible. 至于保持事物清洁,您实际上是什么尝试尽可能地将html和php分开。 If there is a lot of data that depends on being logged in in order to be displayed you may prefer to separate that out and use include instead. 如果有很多数据需要登录才能显示,则您可能希望将其分开并使用include来代替。 This will help to keep things a little cleaner. 这将有助于保持环境清洁。

Kai has the idea, kudos for his answer using the cleaner if variant in the view. Kai有这个想法,在视图中使用清洁器if变体来回答他的回答。

As a minor improvement to his idea, I would either define a named constant: or create a user object to have access to the user's logged_in state application wide. 作为对他的想法的较小改进,我可以定义一个命名常量:或创建一个用户对象以访问整个用户的login_in状态应用程序。

Here is a quick retake of Kai's idea with a named constant: 这是Kai的想法与命名常量的快速重拍:

if(isset($_SESSION['uid'])){
  define("LOGGED_IN", 1);
}else{
  define("LOGGED_IN", 0);   
}

Then in your view / output: 然后在您的视图/输出中:

<?php if(LOGGED_IN): ?>
  <div>logged in content</div>
<?php endif; ?>

The only difference is that LOGGED_IN is accessible in functions, classes, everywhere. 唯一的区别是LOGGED_IN可以在函数,类和任何地方访问。

Last but not least, if you are familiar with OO style, creating a $user object like vascowhite seems to imply, is the way I would (and do) implement login status in my projects. 最后但并非最不重要的一点是,如果您熟悉OO风格,那么创建像vascowhite这样的$ user对象似乎暗示着,这是我(并且确实)在项目中实现登录状态的方式。 In his example, $this is the $user object I am refering to. 在他的例子中,$ this是我引用的$ user对象。

Happy coding, good-luck friend. 快乐的编码,好运的朋友。

I use an MVC framework (Zend Framework) and I provide each controler with a User object. 我使用MVC框架(Zend Framework),并为每个控制器提供User对象。 Once the user is logged in I set a flag to true, so showing restricted content to only logged in users is as simple as:-> 一旦用户登录,我将标志设置为true,因此向仅登录用户显示受限内容非常简单: - >

//content for everybody
<?php if($this->user->isLoggedIn()):?>
//top secret stuff for logged in users only
<p>Only logged in users see this</p>
<?php endif; ?>

Edit: Just to make clear; 编辑:只是为了弄清楚; $this is the view object in the MVC framework. $this是MVC框架中的视图对象。

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

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