1

请帮助我一次仅验证一个会话,请参阅以下脚本,该脚本当前允许相同的用户名登录任意数量的会话。

我不确定何时何地验证会话,请帮助我仅添加可以验证用户名会话的那几行。

<?php // accesscontrol.php
include_once 'common.php';
include_once 'db.php';

session_start();

$uid = isset($_POST['uid']) ? $_POST['uid'] : $_SESSION['uid'];
$pwd = isset($_POST['pwd']) ? $_POST['pwd'] : $_SESSION['pwd'];

if(!isset($uid)) {
  ?>
  <!DOCTYPE html PUBLIC "-//W3C/DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
<title>Login</title>
<meta http-equiv="Content-Type"
  content="text/html; charset=iso-8859-1" />
<head>
<style type="text/css">
<!--
.style1 {
    font-size: 16px;
font-family: Verdana, Arial, Helvetica, sans-serif;
}
.style3 {
    font-size: 12px;
    font-family: Verdana, Arial, Helvetica, sans-serif;
}

body {
background-color: #D7F0FF;
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}

-->
</style>

  </head>
<body>
  <h1 class="style1"> <br><br>Amogh Site - Login Required </h1>
  <span class="style3"><br>
  You <strong>must login to access this area </strong>of the site. <br>
  <br>
  If you are not a registered user, please contact your Admin
     to sign up for instant access!</span>
  <p><form method="post" action="<?=$_SERVER['PHP_SELF']?>">

<span class="style3">User ID:&nbsp;&nbsp;&nbsp;&nbsp;    
<input type="text" name="uid" size="12" />
<br>
<br />
Password:</span>    
<input type="password" name="pwd" SIZE="12" />
<br>
<br />
<input type="submit" value="Login" />
  </form></p>

</body>
  </html>
  <?php
  exit;
}

$_SESSION['uid'] = $uid;
$_SESSION['pwd'] = $pwd;

dbConnect("hitek_svga3");
$sql = "SELECT * FROM user WHERE
    userid = '$uid' AND password = '$pwd'";
$result = mysql_query($sql);
if (!$result) {
error('A database error occurred while checking your '.
    'login details.\\nIf this error persists, please '.
    'contact you@example.com.');
}

if (mysql_num_rows($result) == 0) {
  unset($_SESSION['uid']);
  unset($_SESSION['pwd']);
  ?>

  <!DOCTYPE html PUBLIC "-//W3C/DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title> Access Denied </title>
    <meta http-equiv="Content-Type"
      content="text/html; charset=iso-8859-1" />
    <style type="text/css">
<!--
.style1 {
    font-size: 16px;
    font-family: Verdana, Arial, Helvetica, sans-serif;
}
.style3 {
font-size: 12px;
font-family: Verdana, Arial, Helvetica, sans-serif;
}
-->
</style>  

  </head>
  <body>
  <br/>
  <br/>

  <h1 class="style1"> Access Denied </h1>
  <p class="style3">Your user ID or password is incorrect, or you are not a
 registered user on this site. To try logging in again, click
 <a href="<?=$_SERVER['PHP_SELF']?>">here</a>. To access, please contact our Admin     !</a>.</p>
  </body>
  </html>
  <?php
  exit;
}

$username = mysql_result($result,0,'fullname');
$_SESSION['user'] = mysql_result($result,0,'userid');
$_SESSION['email'] = mysql_result($result,0,'email');
$_SESSION['notes'] = mysql_result($result,0,'notes');

?>
4

2 回答 2

0

首先,为什么要将密码存储在会话变量中?

其次,如果 POST vars 'uid' 和 'pwd' 不存在,您的代码假定会话变量 'uid' 和 'pwd' 将存在,因此您需要在允许脚本之前确保其中一个或存在接着说。这必须在 session_start() 函数之后完成:

<?php // accesscontrol.php
include_once 'common.php';
include_once 'db.php';

session_start();

if(
      (!isset($_SESSION['uid']) || !isset($_SESSION['pwd'])) &&
      (!isset($_POST['uid']) || !isset($_POST['pwd']))
{
     //Redirect or throw exception or whatever
}

$uid = isset($_POST['uid']) ? $_POST['uid'] : $_SESSION['uid'];
$pwd = isset($_POST['pwd']) ? $_POST['pwd'] : $_SESSION['pwd'];
于 2012-12-02T11:15:57.673 回答
0

你不应该:

  • 在数据库中保留明文密码
  • 使用mysql扩展
  • 2012年无框架开发

问题的关键是:PHP 如何判断请求来自哪个用户?上次我检查它使用了作为 GET 参数发送的令牌PHPSESSID或 HTTP 请求标头部分中的 cookie(我认为它被称为)。

显然,为了保证没有人窃取会话,必须通过安全通道交换身份令牌,即一旦用户登录,您必须生成会话 ID 并禁用端口 80 上的普通 HTTP 套接字。必须保留需要登录用户的脚本在仅允许端口 443 上的 HTTPS 的单独主机中。

会话 ID 将从登录脚本中分配,并将保存在user表的列中。顺便说一句,常规应用程序使用单独的表将会话与用户相关联,但是由于您需要每个用户一个客户端,因此表中的一列user就足够了。

因此,当请求带有会话令牌时,如果令牌仍然有效,您的授权逻辑将检查用户表。它应该使用令牌对用户进行身份验证,因此如果请求不包含令牌或在数据库中找不到令牌,则发出 a403 FORBIDDEN并在标头中建议登录 URL Location- 您也可以编写带有<a>在代理不自动遵循重定向的情况下链接。

登录脚本承诺使用令牌更新列,具体取决于您要执行的操作:使旧会话无效或阻止创建新会话,直到用户明确注销另一个客户端(后者在这种情况下会导致麻烦)用户无法访问他之前登录的旧机器,可能是因为关闭了移动设备或因为它位于不同的建筑物中)

于 2012-12-02T11:22:22.273 回答