0

I was having an issue with my login.php because I was not being able to show the successfully-connected page after you log in, in a Debian virtual machine. I didn't know why it was happening because trying the same code/DB with XAMPP in Windows it was working perfectly. This is the code:

<?php
$error='';
    if(isset($_POST['send'])){  
          $user=$_POST['user'];
          $password=$_POST['password'];
          $conexion = mysqli_connect("localhost", "root", "");
          $db = mysqli_select_db($conexion, "aragonnature");
          $consulta = mysqli_query($conexion, "SELECT * FROM usuarios WHERE user='$user' AND password='$password'");
          $filas = mysqli_num_rows($consulta);
              if($filas == 1){
                header("Location: administracion.php");
                } else {
                $error = "Usuario o contraseña incorrecto.";
            }
    }
?>

TL;DR A friend helped me to solve it, turns out that I'm using PHP 8 and MySQL 7.4, so after executing the following code the problem was solved:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'yourpassword';

However I can't find nowhere why by doing this the problem was solved, can any of you please explain me? I'm new with this and want to learn as much as possible.

Regards!

Jose
  • 1
  • https://www.php.net/manual/en/mysqli.requirements.php - `caching_sha2_password` should be available in PHP 7.2.4 onwards. – danblack Dec 16 '20 at 21:06
  • Oh I didnt know about that, I should have checked the official documentation. Thank you both guys! –  Dec 18 '20 at 02:04
  • 1
    Study [Changes in MySQL 8.0](https://dev.mysql.com/doc/refman/8.0/en/upgrading-from-previous-series.html). `caching_sha2_password` is not implemented in the connector which you use, it seems. PS. MySQL version 7 not exists. – Akina Dec 14 '20 at 18:00
  • btw it's Mysql 8 and php 7.4 and not the opposite – Lelio Faieta Dec 18 '20 at 14:47
  • Your code is open to SQL injection. [Immediately read this.](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – MAChitgarha Apr 25 '21 at 11:51
  • I had to do a small thing related with coding for my networking project, I dont really mind the code since they were not evaluating it, but thanks for caring about this :) – Jose Apr 26 '21 at 12:14

0 Answers0