如何成功登录?

nuypyhwy  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(406)

索引.php

<!DOCTYPE HTML>  
     <html>
   <head>
    <style>
  .error {
 color:red;
   }

  .button {
  background-color: #4CAF50;
   border: none;
    color: white;
 padding: 10px 25px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
     }
  .div1 {
background-color: #f2f2f2;
margin-top: -19px;
margin-bottom: -25px;
margin-left: -19px; 
  }

 .copy {
 border-radius: 4px;
 padding: 6px 20px;
  border-style: ridge;
   }

  .copy1{
 border-radius: 4px;
 padding: 6px 28px;
 border-style: ridge;
   }

    .copy2{
  border-radius: 4px;
  padding: 4px 2px;

  }
   </style>
  </head>
   <body>  

  <?php
   // define variables and set to empty values
   include_once 'connect.php';
  $nameErr = $emailErr = $usernameErr = $passwordErr = $DateOfBirthErr = 
    $departmentErr = $ageErr = "";
    $name = $email = $username = $password = $DateOfBirth = $department = 
   $age = "";

    if (isset($_POST['submit'])) {
   if (empty($_POST["name"])) {
     $nameErr = "Name is required";
    } else {
     $name = test_input($_POST["name"]);
   // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
   $nameErr = "Only letters and white space allowed"; 
     }
       }

    if (empty($_POST["email"])) {
     $emailErr = "Email is required";
       } else {
      $email = test_input($_POST["email"]);
        // check if e-mail address is well-formed
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $emailErr = "Invalid email format"; 
       }
    }

    if (empty($_POST["username"])) {
     $usernameErr = "Username is required";
      } else {
     $username = test_input($_POST["username"]);
    // check if name only contains letters and whitespace
   if (!preg_match("/^[a-zA-Z ]*$/",$username)) {
   $usernameErr = "Only letters and white space allowed"; 
       }
       }

     if (empty($_POST["password"])) {
     $passwordErr = "Password is required";
    } else {
     $password = test_input($_POST["password"]);
    $hashed_password = password_hash($password, PASSWORD_DEFAULT);
  // check weather password is alphanumeric
    if(!preg_match('/^(?=.*\d)(?=.*[A-Za-z])[0-9A-Za-z!@#$%]{6,}$/', 
  $password))
    {
$passwordErr = "Password must be alphanumeric and atleast 6 characters 
   long!";
   }
  }

    if (empty($_POST["Date_of_birth"])) {
    $DateOfBirthErr = "Date Of Birth is required";
   } else {
  $DateOfBirth = test_input($_POST["Date_of_birth"]);
    }

   if (empty($_POST["department"])) {
    $departmentErr = "Department is required";
   } else {
   $department = test_input($_POST["department"]);
     }

   if (empty($_POST["age"])) {
        $ageErr = "Age is required";
      } else {
        $age = test_input($_POST["age"]);
  }

   if($nameErr == "" && $emailErr == "" && $usernameErr == "" && 
  $passwordErr == "")
    { 
  $check="SELECT * FROM users WHERE username = '$_POST[username]'";
     $rs = mysqli_query($mysqli,$check);
  $da = mysqli_fetch_array($rs, MYSQLI_NUM);
 if($da[0] > 0) {
 echo "Username Already in Exists<br/>";
 }

   else
{
      $sql = "INSERT INTO users(`id`,`username`, `password`, `email` , 
    `name` , `Date_of_birth` , `department` ,`age`)
         VALUES ('','".$username."', '".$hashed_password."', '".$email."' , 
 '".$name."' , '".$DateOfBirth."' , '".$department."' , '".$age."')";

    if (mysqli_query($mysqli, $sql)) {
    echo "Registered successfully";
    } else {
    echo "Error: " . $sql . "<br>" . mysqli_error($mysqli);
    } 
    mysqli_close($mysqli);
   }
  }

  }

   function test_input($data) {
   $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
     return $data;
   }
   ?>

  <div style="padding-left: 250px" class="div1">
  <h2 style="color:#009999">Registration Form :</h2>
  <p><span class="error">All fields are required </span></p>
  <form method="post" action="">  
    <span style="color:#0099ff">Name: </span>
  <input type="text" name="name" class= "copy" style="margin-left: 52px">
 <span class="error"> <?php echo $nameErr;?></span>
 <br><br>
 <span style="color:#0099ff"> E-mail: </span>
 <input type="text" name="email" class= "copy" style="margin-left: 48px">
 <span class="error"><?php echo $emailErr;?></span>
 <br><br>
  <span style="color:#0099ff"> Username: </span>
 <input type="text" name="username"  class= "copy" style="margin-left:26px">
   <span class="error"> <?php echo $usernameErr;?></span>
   <br><br>
  <span style="color:#0099ff"> Password: </span>
    <input type="password" name="password"  class= "copy" style="margin- 
    left:30px">
  <span class="error"> <?php echo $passwordErr;?></span>
  <br><br>
   <span style="color:#0099ff"> Date Of Birth : </span>
      <input type="date" class= "copy1" name="Date_of_birth">
      <span class="error"> <?php echo $DateOfBirthErr;?></span>
    <br><br>
 <span style="color:#0099ff">  Age : </span>
    <input type="number" name="age"  class= "copy" style="margin-left:62px">
    <span class="error"> <?php echo $ageErr;?></span>
      <br><br>
    <span style="color:#0099ff">  Department : </span>
 <select name="department"  class= "copy2" style="margin-left:14px">
   <option value="EE">Electrical & Electronics</option>
  <option value="EC">Electronics & Communication</option>
  <option value="ME">Mechanical</option>
  <option value="CS">Computer Science</option>
  <option value="CV">Civil</option>
    <option value="IS">Information Science</option>
   </select>
 <span class="error"> <?php echo $departmentErr;?></span>
    <br><br>
 <input type="submit" class="button" name="submit" value="Register">  
 <p style="color:black">Already Registered? <a href="login.php">Login</a>. 
 </p>
    </form>
     </div>
    </body>
    </html>

登录.php

<?php
   include_once 'connect.php';
 session_start();

 if($_SERVER["REQUEST_METHOD"] == "POST") {
   // username and password sent from form 

   $myusername = mysqli_real_escape_string($mysqli,$_POST['username']);
   $mypassword = mysqli_real_escape_string($mysqli,$_POST['password']); 

   $sql = "SELECT * FROM users WHERE username = '$myusername' and password = 
  '$mypassword'";
   $result = mysqli_query($mysqli,$sql);
  $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
  $active = $row['active'];

    $count = mysqli_num_rows($result);

  // If result matched $myusername and $mypassword, table row must be 1 row

    if($count == 1) {
     session_register("myusername");
     $_SESSION['login_user'] = $myusername;

     header("location: welcome.php");
  }else {
     $error = "Your Login Name or Password is invalid";
     }
      }
   ?>
  <html>

  <head>
  <title>Login Page</title>

     <style type = "text/css">
      body {
        font-family:Arial, Helvetica, sans-serif;
        font-size:14px;
     }
     label {
        font-weight:bold;
        width:100px;
        font-size:14px;
     }
     .box {
        border:#666666 solid 1px;
     }
       </style>

   </head>

      <body bgcolor = "#FFFFFF">

  <div align = "center">
     <div style = "width:300px; border: solid 1px #333333; " align = "left">
        <div style = "background-color:#333333; color:#FFFFFF; 
      padding:3px;"><b>Login</b></div>

         <div style = "margin:30px">

           <form action = "" method = "post">
              <label>UserName  :</label><input type = "text" name = 
      "username" class = "box"/><br /><br />
              <label>Password  :</label><input type = "password" name = 
     "password" class = "box" /><br/><br />
              <input type = "submit" value = " Submit "/><br />
           </form>

           <div style = "font-size:11px; color:#cc0000; margin-top:10px"><? 
    php echo $error; ?></div>

        </div>

        </div>

         </div>

       </body>
  </html>

欢迎使用.php

<?php
   include_once 'session.php';

   ?>
  <html>

 <head>
  <title>Welcome </title>
  </head>

 <body>
  <h1>Welcome <?php echo $login_session; ?></h1> 
  <h2><a href = "logout.php">Sign Out</a></h2>
  </body>

   </html>

注销.php

<?php
     session_start();

     if(session_destroy()) {
     header("Location: login.php");
      }
     ?>

会话.php

<?php
   include_once 'connect.php';
    session_start();

  $user_check = $_SESSION['login_user'];

      $ses_sql = mysqli_query($mysqli,"select username from users where username = 
   '$user_check' ");

     $row = mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);

     $login_session = $row['username'];

    if(!isset($_SESSION['login_user'])){
  header("location:login.php");
   }
   ?>

连接.php

<?php

 $databaseHost = 'localhost';
   $databaseName = 'amith';
$databaseUsername = 'root';
 $databasePassword = '';

   $mysqli = mysqli_connect($databaseHost, $databaseUsername, 
    $databasePassword, $databaseName);

  ?>

我正在创建一个简单的php注册表单和一个登录表单。我已经完成了注册表,效果很好,但是现在在完成注册表之后,当他们尝试登录到他们的页面时,应该会成功登录,但是我无法得到想要的结果,即使很难,我有相同的用户名和相同的密码在数据库中注册,但当我试图用这些用户名和密码登录我无法登录,请任何人可以指导我应该做什么改变,以获得我的结果以上代码

ddrv8njm

ddrv8njm1#

您正在用插入密码 password_hash() 在注册过程中。然后您需要使用验证密码 password_verify() . 像这样用来检查密码是否匹配

if(password_verify($password, $hashed_password)) {
    // If the password inputs matched the hashed password in the database
    // Do something, you know... log them in.
}

您的登录码

<?php
   include_once 'connect.php';
 session_start();

 if($_SERVER["REQUEST_METHOD"] == "POST") {
   // username and password sent from form 

   $myusername = mysqli_real_escape_string($mysqli,$_POST['username']);
   $mypassword = mysqli_real_escape_string($mysqli,$_POST['password']); 

   $sql = "SELECT * FROM users WHERE username = '$myusername' ";
   $result = mysqli_query($mysqli,$sql);
  $row = mysqli_fetch_array($result);
  $hashed_password=$row['password'];
  if(password_verify($mypassword, $hashed_password)) {

     $_SESSION['login_user'] = $myusername;

     header("location: welcome.php");
     exit();
}    
  else 
  {
     $error = "Your Login Name or Password is invalid";
    }
}
   ?>

使用准备好的语句。因为您的代码容易受到sql注入攻击。

相关问题