使用 PHP 显示错误消息
如果用户在未以管理员身份登录时尝试访问 /admin/
,我想显示错误。我不想在 URL 中传递内容,也不想执行 $_POST
来显示错误。我只想显示一条消息,当你刷新时它就消失了。
例如,转到以下 URL:http://getsatisfaction.com/getsatisfaction/topics/notifications_box/edit
它会让您返回主题并显示“抱歉,您被拒绝编辑此主题。”
当你刷新它就消失了。我也希望能够显示这样的错误。有谁知道他们是怎么做到的?
我见过其他网站也这样做(没有在 URL 末尾附加 ?error=1
)。
提前致谢。
I want to display an error if a user tries to go to /admin/
when they aren't logged in as an admin. I don't want to pass stuff in the URL and I do not want to do a $_POST
to display the error. I just want to display a message and when you refresh it is gone.
For example, go to this URL: http://getsatisfaction.com/getsatisfaction/topics/notifications_box/edit
It returns you back to the topic and says "I'm sorry, but you have been denied access to edit this topic."
When you refresh it is gone. I want to be able to display an error like that too. Does anyone know how they did that?
I've seen other sites do this as well (without appending an ?error=1
to the end of the URL).
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在会话中设置错误消息:
显示消息:
Set the error message in the session:
Display the message:
您需要检查会话并查看用户是否有权限。
检查会话特定于环境,例如,如果您使用 Joomla:
http://www.howtojoomla.net/how -tos/development/how-to-use-sessions-in-joomla
如果您使用 drupal:
http://drupal.org/node/360542
当然,php 中有一个用于会话的本机库:
http://php.net/manual/en/ref.session.php
希望有帮助!
You need to check the session and see if the user has permission.
Checking the session is specific to the environment, for example, if you use Joomla:
http://www.howtojoomla.net/how-tos/development/how-to-use-sessions-in-joomla
and if you use drupal:
http://drupal.org/node/360542
of course that there's a native library for sessions in php:
http://php.net/manual/en/ref.session.php
hope it helps!