duanfeng7756 2017-06-20 21:55
浏览 15
已采纳

在哪里使用PHP存储信息 - 最佳实践

first of all sorry if my english will be a little bad!

I'm a newbie of PHP programming and i've some questions:

I'm developing a website using PHP. In this site i have a login: more of one php page is accessible only to logged user, and in all of this pages i check if the user is logged using $_SESSION["user"] variable, that, if it is instantied, contains the username. Is repeating this control each page a bad practice? What are alternatives?

Then, an user could be an admin or a tester and i get this information doing a query when i need (more of one time for the same session of the user). Should i use another $_SESSION variable to store these informations? and check this variable where i need? (In this moment i do a query to get (again) this information).

Is to use a SESSION variable secure?

I ask because i've same code in more pages, and this doesn't seem a good practice.

Regards, Luca

  • 写回答

1条回答 默认 最新

  • doulan9188 2017-06-20 22:23
    关注

    In my opinion, depending on the structure of your code, you could create a php file which will work as a controller and will have some basic configuration all of your other pages will inherit.

    $_SESSION is not a bad idea as long as you use it correctly. Since the user is logged in from the very start, you can keep in session the important information, like their username or their role permission in order to be able to call them immediately without making connections to the database constantly.

    But you have to be careful on the security of this issue too, as there is always the risk of someone affecting this data from the browser. So the session should have an expiration time limit and the data should be encrypted.

    These configurations could be set up from your main php class, and as a result inherit the methods of this class methods on every other php page you are using.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”