当构建一个仅对成员可访问的页面时,以下是正确的php:吗
<?php if($_SESSION['logged_in']): ?>
// all code for page here
<?php endif; ?>
我所有的html/php都位于这两行之间吗?
还有其他更好的方法吗?
我应该注意哪些安全问题?
我的内容不是特别敏感,但将来可能会敏感。
严格来说,写易读的代码,为什么不呢:
<?php
if (!$_SESSION['logged_in']) {
header("Location: login.php");
exit;
}
?>
<!-- // all code for page here -->
或者类似的。
更好的方法可能是:
<?php if(!$_SESSION['logged_in']) { header('Location:loginpage.php'); exit; } ?>
然后,您的页面可以正常继续,未登录的用户将获得登录页面。
编辑:根据标题文档添加了所需的exit
调用。
如果您愿意,您当然可以将所有HTML/PHP代码放在条件之间。请注意,如果您这样做,未经身份验证的用户在访问页面时将看不到任何东西。您可能希望将设计元素放在条件之外,或者至少在endif之前使用else:;
<?php
a = checksession(args); //your own function for checking session
if(!a)
header("Location: 401.shtml"); //This can contain advisory regarding login
?>
这将在您的页面顶部,其余部分由您决定。