我对我已经发布的东西有疑问,但我想我会再次问这个问题,因为我现在有更多的代码。
我用于教程的原始代码
function checkLoggedIn($page)
{
$loginDiv = '';
$action = '';
if (isset($_POST['action']))
{
$action = stripslashes ($_POST['action']);
}
session_start ();
// Check if we're already logged in, and check session information against cookies
// credentials to protect against session hijacking
if (isset ($_COOKIE['project-name']['userID']) &&
crypt($_SERVER['REMOTE_ADDR'] . $_SERVER['HTTP_USER_AGENT'],
$_COOKIE['project-name']['secondDigest']) ==
$_COOKIE['project-name']['secondDigest'] &&
(!isset ($_COOKIE['project-name']['username']) ||
(isset ($_COOKIE['project-name']['username']) &&
Users::checkCredentials($_COOKIE['project-name']['username'],
$_COOKIE['project-name']['digest']))))
{
// Regenerate the ID to prevent session fixation
session_regenerate_id ();
// Restore the session variables, if they don't exist
if (!isset ($_SESSION['project-name']['userID']))
{
$_SESSION['project-name']['userID'] = $_COOKIE['project-name']['userID'];
}
// Only redirect us if we're not already on a secured page and are not
// receiving a logout request
if (!isSecuredPage ($page) &&
$action != 'logout')
{
header ('Location: ./');
exit;
}
}
else
{
// If we're not already the login page, redirect us to the login page
if ($page != Page::LOGIN)
{
header ('Location: login.php');
exit;
}
}
// If we're not already logged in, check if we're trying to login or logout
if ($page == Page::LOGIN && $action != '')
{
switch ($action)
{
case 'login':
{
$userData = Users::checkCredentials (stripslashes ($_POST['login-username']),
stripslashes ($_POST['password']));
if ($userData[0] != 0)
{
$_SESSION['project-name']['userID'] = $userData[0];
$_SESSION['project-name']['ip'] = $_SERVER['REMOTE_ADDR'];
$_SESSION['project-name']['userAgent'] = $_SERVER['HTTP_USER_AGENT'];
if (isset ($_POST['remember']))
{
// We set a cookie if the user wants to remain logged in after the
// browser is closed
// This will leave the user logged in for 168 hours, or one week
setcookie('project-name[userID]', $userData[0], time () + (3600 * 168));
setcookie('project-name[username]',
$userData[1], time () + (3600 * 168));
setcookie('project-name[digest]', $userData[2], time () + (3600 * 168));
setcookie('project-name[secondDigest]',
DatabaseHelpers::blowfishCrypt($_SERVER['REMOTE_ADDR'] .
$_SERVER['HTTP_USER_AGENT'], 10), time () + (3600 * 168));
}
else
{
setcookie('project-name[userID]', $userData[0], false);
setcookie('project-name[username]', '', false);
setcookie('project-name[digest]', '', false);
setcookie('project-name[secondDigest]',
DatabaseHelpers::blowfishCrypt($_SERVER['REMOTE_ADDR'] .
$_SERVER['HTTP_USER_AGENT'], 10), time () + (3600 * 168));
}
header ('Location: ./');
exit;
}
else
{
$loginDiv = '<div id="login-box" class="error">The username or password ' .
'you entered is incorrect.</div>';
}
break;
}
// Destroy the session if we received a logout or don't know the action received
case 'logout':
default:
{
// Destroy all session and cookie variables
$_SESSION = array ();
setcookie('project-name[userID]', '', time () - (3600 * 168));
setcookie('project-name[username]', '', time () - (3600 * 168));
setcookie('project-name[digest]', '', time () - (3600 * 168));
setcookie('project-name[secondDigest]', '', time () - (3600 * 168));
// Destory the session
session_destroy ();
$loginDiv = '<div id="login-box" class="info">Thank you. Come again!</div>';
break;
}
}
}
return $loginDiv;
}
我的代码:
<?php
function encrypt($input)
{
$hash = password_hash($input, PASSWORD_DEFAULT);
return $hash;
}
function checkUserCreds($username, $password)
{
$id = 0;
$hash = '';
$db = new PDO('$dbDNS', '$dbuser', '$dbpass');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //Set error mode
try
{
$st = $db->prepare("SELECT id, login, email, pass FROM users WHERE login =:username");
$st->bindParam(':username', $username, PDO::PARAM_STR);
$success = $st->execute();
if($success)
{
$userData = $st->fetch();
$hash = $userData['pass'];
if (password_verify($password, $hash) == $hash)
{
$id = $userData['id'];
}
}
}
catch (PDOException $e)
{
$id = 0;
$hash = '';
}
$db = null;
return array ($id, $username, $hash);
}
function checkLoggedIn($page)
{
$loginMess='';
$action='';
if (isset($_POST['action']))
{
$action = stripslashes($_POST['action']);
}
session_start();
//Check if already logged in and check session information against cookies
if (isset($_COOKIE['sukd']['id']) && encrypt($_SERVER['REMOTE_ADDR'] . $_SERVER['HTTP_USER_AGENT']) == $_COOKIE['sukd']['hashv2'] && (!isset ($_COOKIE['sukd']['username']) || (isset ($_COOKIE['sukd']['username']) && checkUserCreds($_COOKIE['sukd']['username'], $_COOKIE['sukd']['hash']))))
{
echo "isset cookies: ON, GOOD <br>";
// Regenerate the ID to prevent session fixation
//session_regenerate_id ();
}
else
{
// If we are not on the login page, redirect.
if ($page != 'login')
{
header ('Location login.php');
exit;
}
}
if ($page = 'login' && $action != '')
{
switch($action)
{
case 'login':
{
$userData = checkUserCreds(stripslashes($_POST['username']), stripslashes($_POST['password']));
if ($userData[0] != 0)
{
$_SESSION['sukd']['id']=$userData[0];
$_SESSION['sukd']['ip']=$_SERVER['REMOTE_ADDR'];
$_SESSION['sukd']['userAgent']=$_SERVER['HTTP_USER_AGENT'];
if(isset($_POST['remember']))
{
//remember for 7 days
setcookie('sukd[id]', $userData[0], time () + (3600 * 168));
setcookie('sukd[username]', $userData[1], time() + (3600 * 168));
setcookie('sukd[hash]', $userData[2], time() + (3600 * 168));
setcookie('sukd[hashv2]', encrypt($_SERVER['REMOTE_ADDR'] . $_SERVER['HTTP_USER_AGENT']), time () + (3600 * 168));
}
else
{
setcookie('sukd[id]', $userData[0], false);
setcookie('sukd[username]', '', false);
setcookie('sukd[hash]', '', false);
setcookie('sukd[hashv2]', encrypt($_SERVER['REMOTE_ADDR'] . $_SERVER['HTTP_USER_AGENT']), time () + (3600 * 168));
}
header ('Location: ./');
exit;
}
else
{
$loginMess = "The username or password you entered is incorrect <br>";
}
break;
}
case 'logout':
default:
{
$_SESSION = array();
setcookie('sukd[id]', '', time () + (3600 * 168));
setcookie('sukd[username]', '', time() + (3600 * 168));
setcookie('sukd[hash]', '', time() + (3600 * 168));
setcookie('sukd[hashv2]', '', time () + (3600 * 168));
session_destroy();
$loginMess = "echo 'Successfully logged out <br>'";
break;
}
}
}
return $loginMess;
}
?>
例如,它被调用checkLogged(login)
并在出现问题时输出登录消息。此外,它使用带有操作的隐藏字段来设置案例切换的值、登录或注销。目前,它可以正常登录,添加 cookie 等。
但是,问题是,当用户已经登录时,它应该检查代码。
if (isset($_COOKIE['sukd']['id']) && encrypt($_SERVER['REMOTE_ADDR'] etc..
我无法真正理解原始代码,所以我什至不知道从哪里开始。cookie 数组有点奇怪,它似乎基于两个不同的版本,具体取决于您是 setcookie 还是调用 cookie。
如果有人在不使用顶级方法的情况下有更安全的方法,我很高兴有人能进一步启发我。
我的代码的原件。
digest = hash
decondDigest = hashv2