防止会话存储中的双重输入

时间:2017-04-25 22:47:59

标签: php session

我正在尝试使用数组中的登录详细信息进行登录,所有这些都存储在此脚本中。

我添加了会话欠费,其中每个登录都将存储在session.txt中,我想要一个情况,我不会在session.txt中重复登录以防止双重登录,如果你已经登录,你无法再次登录。

此脚本中的第一个用户名被禁止登录两次,而其他人可以登录,我打算做的是避免在我的session.txt中双重输入。

这是我的代码:

<?php session_start(); /* Starts the session */

    /* Check Login form submitted */    
    if(isset($_POST['Submit'])){
        /* Define username and associated password array */
        $logins = array('Emmanuel' => '1234','John' => 'Faith123','Favour' => '12345','Abbey' => 'Godisgood');

        /* Check and assign submitted Username and Password to new variable */
        $Username = isset($_POST['Username']) ? $_POST['Username'] : '';
        $Password = isset($_POST['Password']) ? $_POST['Password'] : '';

        /* Check Username and Password existence in defined array */        
        if (isset($logins[$Username]) && $logins[$Username] == $Password){
      $handle = "sessions.txt";
      $data = file_get_contents($handle);
$line = explode("\n", $data);
foreach ($line as $user) 
{
if($Username==$user)
{
  echo"You can not vote twice";
}
else
{
            /* Success: Set session variables and redirect to Protected page  */
            $_SESSION['UserData']['Username']=$logins[$Username];
      $string = $Username . "\r\n";
      $handle = fopen("sessions.txt", 'a');
      fwrite($handle, $string);
      fclose($handle);
            header("location:../president");
            exit;
        } }else {
            /*Unsuccessful attempt: Set error message */
            $msg="<span style='color:red'>Invalid Login Details</span>";
        }
    }
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>LOGIN</title>
<link href="./css/style.css" rel="stylesheet">
</head>
<body>
<div id="Frame0">
  <h1>LOGIN</h1>
</div>
<br>
<form action="" method="post" name="Login_Form">
  <table width="400" border="0" align="center" cellpadding="5" cellspacing="1" class="Table">
    <?php if(isset($msg)){?>
    <tr>
      <td colspan="2" align="center" valign="top"><?php echo $msg;?></td>
    </tr>
    <?php } ?>
    <tr>
      <td colspan="2" align="left" valign="top"><h3>Login</h3></td>
    </tr>
    <tr>
      <td align="right" valign="top">Username</td>
      <td><input name="Username" type="text" class="Input"></td>
    </tr>
    <tr>
      <td align="right">Password</td>
      <td><input name="Password" type="password" class="Input"></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input name="Submit" type="submit" value="Login" class="Button3"></td>
    </tr>
  </table>
</form>
</body>
</html>

0 个答案:

没有答案