简体   繁体   English

会员专区

[英]Members only area

When building a page that is to be accessible to members only, is the following the correct php:在构建仅供成员访问的页面时,以下是正确的 php:

<?php if($_SESSION['logged_in']): ?>

// all code for page here

<?php endif; ?>

Does all of my html / php sit between these two lines?我的所有 html / php 是否都位于这两行之间?

Are there any other ways of doing this that are better?还有其他更好的方法吗?

What security issues should I be aware of?我应该注意哪些安全问题?

My content is not particularly sensitive but may be in the future.我的内容不是特别敏感,但将来可能会。

Speaking strictly in terms of writing legible code, why not:在编写清晰的代码方面严格地说,为什么不:

<?php
if (!$_SESSION['logged_in']) {
   header("Location: login.php");
   exit;
}
?>

<!-- // all code for page here -->

Or similar.或类似的。

A better way might be:更好的方法可能是:

<?php if(!$_SESSION['logged_in']) { header('Location:loginpage.php'); exit; } ?>

Then your page can continue as normal, and a user who isn't logged in will get the login page instead.然后您的页面可以正常继续,未登录的用户将获得登录页面。

Edit: added the required exit call as per the header documentation.编辑:根据 header 文档添加了所需的exit调用。

You certainly can put all your HTML / PHP code in between the conditional if you like.如果您愿意,您当然可以将所有 HTML / PHP 代码放在条件之间。 Just be aware that if you do, unauthenticated users won't see anything when they hit the page.请注意,如果您这样做,未经身份验证的用户在点击页面时将看不到任何内容 You may want to put design elements outside the conditional, or at least use an else: before the endif;您可能希望将设计元素放在条件之外,或者至少在 endif 之前使用 else:

<?php
 a = checksession(args); //your own function for checking session
 if(!a)
   header("Location: 401.shtml");  //This can contain advisory regarding login
?>    

This will be at the top of your page, rest you can decide.这将在您的页面顶部,您可以决定 rest。

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

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