我正在为我镇上的本地企业创建一个目录。我允许每个企业在网站上创建个人资料,他们可以在其中上传联系信息、照片、他们在 Google map 上的位置等。
我对 php 有很好的了解,但我不会接近行业标准。
我只是在寻找一些关于验证企业是否已在管理页面上登录的建议。我现在的方式是,当他们的用户名和密码得到验证后,我为他们创建一个 session :
$_SESSION['session_businessid']
这基本上只是一个与他们的业务 ID 的 session ,从 mySQL 数据库的业务表中获取。
然后在每个需要登录业务的页面上,我包含一个名为 verify_logged_in.php 的 php 文件,其中包含以下代码:
<?php
session_start();
if ($_SESSION['session_businessid'])
{
$BusinessID = $_SESSION['session_businessid'];
}
else
header ("location: /admin/login.php");
?>
我只是想知道这种方法的安全/不安全程度,是否有更好的方法?
最佳答案
这不够安全,因为您将 session 变量存储在默认的 php session 中。您必须使用安全 session 以保护信息被黑客攻击或被 session 劫持、XSS 攻击等滥用。您可以使用以下链接来指导您如何创建安全的 php session - Create-a-Secure-Session-Managment-System-in-Php-and-Mysql .
或者,如果您想要一个更简单但不太安全的 session ,则可以使用以下代码:
session .php:
function sec_session_start() {
$session_name = 'sec_session_id'; // Set a custom session name
$secure = false; // Set to true if using https.
$httponly = true; // This stops javascript being able to access the session id.
ini_set('session.use_only_cookies', 1); // Forces sessions to only use cookies.
$cookieParams = session_get_cookie_params(); // Gets current cookies params.
session_set_cookie_params($cookieParams["lifetime"], $cookieParams["path"], $cookieParams["domain"], $secure, $httponly);
session_name($session_name); // Sets the session name to the one set above.
session_start(); // Start the php session
session_regenerate_id(); // regenerated the session, delete the old one.
任何页面.php:
include 'sessions.php';
sec_session_start();
//rest of the code.
此外,您用于登录的方法将对企业存储信息的安全性产生影响。
关于php - 我验证用户已登录的方法有多安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19889373/