English 中文(简体)
1. 显示用户有标记或没有标记的物体
原标题:Displaying objects based on if a user is logged in or not

我在我的网站上学习关于用户认证的PHP课程。 我知道如何限制使用会议对完整页的浏览(如果确定所建会议变量,如果是,显示内容,即使不改用错误)。

但 我试图拿出最佳方法,有选择地展示和隐藏不同的物体(div、文本、图像),如果用户不作标记的话。 这是否简单,视所建会议的情况而定,并显示其是否定点? 是否有更有效的方法这样做?

最佳回答

You control sessions so you can choose what to check for.

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

then the html condition:

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

或未储存临时变量:

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

Just use if statements within the script

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

要使事情保持下去,你将尽最大可能把你的html和php分开。 如果存在大量数据,需要加以记录才能显示,那么你可能倾向于将数据分离出来使用<>条码>。 这将有助于使事情更加清洁。

Kai有这样的想法,即使用更清洁的<条码>(如果变量)来回答。

作为对其想法的微小改进,我要么将。 界定一个名称的不变值: 或创建用户标语,以便进入国家应用范围上的用户标记。

Here is a quick retake of Kai s idea with a named constant:

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

之后,您认为/产出:

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

唯一的区别是,政府专家小组 各种职能、班级都可以进入国家。

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. In his example, $this is the $user object I am refering to.

欢乐的好友。

我使用了MVC框架(修正框架),我向每个控制者提供了用户物体。 一旦用户在我打上真实的旗帜,那么只向用户贴上限定内容就象:-和”一样简单。

//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.





相关问题
why the session in iis automatically log out?

I used iis6, and when i called a function Directory.delete(), the all the session relate this website will be logged out. and i debugged the website, i found no exception. any one have ideas on this ? ...

Check session from a view in CodeIgniter

What is the best way to check session from a view in CodeIgniter, it shows no way in their user guide, otherwise I will have to make two views on everything, which is kinda weird...still a newbie to ...

Can I get the size of a Session object in bytes in c#?

Is it possible to get the size(in bytes) of a Session object after storing something such as a datatable inside it? I want to get the size of a particular Session object, such as Session["table1"], ...

提供严格分类的出席会议物体

提供严格分类的与会机会的最佳方式是什么? 我正计划转而选择矩阵,这正在促使汇编者抱怨我的幻觉方案拟订方法......

PHP Session is not destroying after user logout

I m trying to create an authentication mechanism for my PHP Application and I m having difficulty destroying the session. I ve tried unsetting the authentication token which was previously set within ...

热门标签