0

I cant load informations from my mysql database. I want to load data from my database and compare it with a php variable, but it does not work. Whenever I load the page, I see a white page. My Code

Can anybody help me?

  <?php
define('DB_SERVER', '');
define('DB_USERNAME', '');
define('DB_PASSWORD', '');
define('DB_DATABASE', '');

$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);

if($_SERVER["REQUEST_METHOD"] == "POST") {

    $myusername = mysqli_real_escape_string($db,$_POST['login']);
    $mypassword = mysqli_real_escape_string($db,hash('ripemd160', $_POST['pass']));
    $sql = "SELECT username, password, active FROM User WHERE username = '$myusername' and password = '$mypassword'";
    $result = mysqli_query($db, $sql);
    while($row = mysql_fetch_assoc($result)) 
    {
        $username = $row['username'];
        $password = $row['password'];
        $active = $row['active'];

        if($username == $myusername){
            if($active == 1)
            {
                session_start();
                $_SESSION["login"] = $myusername;
                echo $_SESSION["login"];

                header("Location: http://www.example.de"); 
            }
            else
            {
            echo "please verify your email";    
            }
        }
    }   
}
else 
{
    header("Location: http://www.fapsite.de/Main/Home/Views/Login/WrongPassword.php");
}

?>
Koen Hollander
  • 1,687
  • 6
  • 27
  • 42
  • 1
    Don't post a screenshot of code!!! – AbraCadaver Oct 06 '16 at 17:36
  • 1
    [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)*** Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! [Don't believe it?](http://stackoverflow.com/q/38297105/1011527) – Jay Blanchard Oct 06 '16 at 17:45
  • Please use PHP's [built-in functions](http://jayblanchard.net/proper_password_hashing_with_PHP.html) to handle password security. If you're using a PHP version less than 5.5 you can use the `password_hash()` [compatibility pack](https://github.com/ircmaxell/password_compat). Make sure you ***[don't escape passwords](http://stackoverflow.com/q/36628418/1011527)*** or use any other cleansing mechanism on them before hashing. Doing so *changes* the password and causes unnecessary additional coding. – Jay Blanchard Oct 06 '16 at 17:45

2 Answers2

2

When you attempt to get fetched username, you access $row['Username'], while the key should be username, note the case of the first letter. This is why evaluation $username == $myusername is not evaluated to true.

Same appliles to Password vs password and Active vs active.

Georgy Ivanov
  • 1,573
  • 1
  • 17
  • 24
0

This, at least, is a fatal error in your code:

You need to use mysqli function when fetch while not mysql_fetch_assoc

Rick James
  • 135,179
  • 13
  • 127
  • 222
harrrrrrry
  • 13,643
  • 2
  • 23
  • 28