1

我正在为我所在城镇的本地企业创建目录。我允许每个企业在网站上创建个人资料,他们可以在其中上传联系信息、照片、他们在谷歌地图上的位置等。

我对 php 有很好的了解,但我不会接近行业标准。

我只是在寻找一些关于验证企业已在管理页面上登录的建议。我现在的方式是,当他们的用户名和密码得到验证后,我为他们创建了一个会话:

$_SESSION['session_businessid']

这基本上只是与他们的业务 ID 的会话,该会话从 mySQL 数据库中的业务表中获取。

然后在需要登录业务的每个页面上,我都包含一个名为 verify_logged_in.php 的 php 文件,其中包含以下代码:

<?php
session_start();

if ($_SESSION['session_businessid'])
{
    $BusinessID = $_SESSION['session_businessid'];
}
else
    header ("location: /admin/login.php");
?>

我只是想知道这种方法有多安全/不安全,是否有更好的方法?

4

1 回答 1

0

这不够安全,因为您将会话变量存储在默认的 php 会话中。您必须使用安全会话来保护被会话劫持、XSS 攻击等攻击或滥用的信息。您可以使用以下链接来指导您如何创建安全的 php 会话 - Create-a-Secure-Session-Managment -System-in-Php-and-Mysql

或者,如果您想要一个更简单但不太安全的会话,那么您可以使用以下代码:

会话.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.

anypage.php:

include 'sessions.php';
sec_session_start();
//rest of the code.

此外,您用于登录的方法将影响企业存储信息的安全性。

于 2013-11-10T12:04:40.310 回答