login with username or email address in php

前端 未结 8 2311
面向向阳花
面向向阳花 2021-02-01 10:56

I am trying to create a login with username or email

My code is:

$username=$_REQUEST[\'login\'];
$email=$_REQUEST[\'login\'];
$password=$_REQUEST[\'passw         


        
8条回答
  •  面向向阳花
    2021-02-01 11:42

    Hi, for me works something like this:

    if ( !isset($_POST['emailuser'], $_POST['userPass']) ) {
        // Could not get the data that should have been sent.
        die ('Please fill both the username and password field!');
    }
    $emailuser = ($_POST['emailuser']);
    $emailuser = trim($emailuser);
    
        if ($stmt = $con->prepare('SELECT userEmail or userName, userPass FROM users WHERE userEmail = ? or userName = ?')) {
       // Bind parameters (s = string, i = int, b = blob, etc), in our case the username is a string so we use "s"
       $stmt->bind_param('ss', $emailuser, $emailuser);
       $stmt->execute();
       // Store the result so we can check if the account exists in the database.
       $stmt->store_result();
    
       if ($stmt->num_rows > 0) {
           $stmt->bind_result($userName, $userPass);
           $stmt->fetch();
           // Account exists, now we verify the password.
           // Note: remember to use password_hash in your registration file to store the hashed passwords.
           if (password_verify($_POST['userPass'], $userPass)) {
               // Verification success! User has loggedin!
               // Create sessions so we know the user is logged in, they basically act like cookies but remember the data on the server.
               session_regenerate_id();
               $_SESSION['loggedin'] = true;
               $_SESSION['name'] = $emailuser;
               $_SESSION['emailuser'] = $userName;
               header('location: /menu.php');
           } else {
               echo 'Incorrect password!';
           }
       } else {
           echo 'Incorrect username!';
       }
       $stmt->close();    } ?>
    

提交回复
热议问题