0

I´m trying to create a simple login script for my Website (with PHP & Mysql). Created the original script with plain php & mysql commands and everything worked just fine. Now i wanted to exchange the old mysql commands with mysqli commands. Somehow i´m now getting the error "Trying to get property of non-object *** on line 11" when I test my script. Could somebody explain exactly to me what causes that problemn and how to solve it (because I dont really understand the error here)?

Login Script:

<?php
session_start();
?>
<?php
    include_once "db_connect.php";
    $username = $_POST["username"];
    $password = md5($_POST["password"]);
    $abfrage = "SELECT username, password FROM login WHERE username LIKE '$username' LIMIT 1";
    $ergebnis = mysqli_query($verbindung,$abfrage);
    $row = mysqli_fetch_assoc($ergebnis);
    if ($row->password === $password) {         <--- Line 11
    $_SESSION["username"] = $username;
    if ($username != "admin") {
            echo "Login erfolgreich. <br> <a href=\"../secure/geheim.php\">Geschützter Bereich</a>";
    }
    else {
            echo "Login erfolgreich. <br> <a href=\"../secure/admin.php\">Geschützter Bereich</a>";
    }
    }
    else {
    echo "Benutzername und/oder Passwort sind falsch.";

    }
?>
AVI
  • 5,516
  • 5
  • 29
  • 38
RedPanda
  • 399
  • 1
  • 4
  • 10

4 Answers4

1
<?php
session_start();

include_once "db_connect.php";
// either use require_once + bail-out code in db_connect.php
// or check the connection resource/object here. 
if ( !$verbindung || $verbindung->connect_errno ) {
    die('sorry, db error. try again later');
}
$password = md5($_POST["password"]); // md5, unsalted ...not secure anymore. see http://docs.php.net/password_hash

// see http://php.net/security.database.sql-injection
$abfrage = sprintf( // password is a reserved word in mysql -> backticks around the field id
    "SELECT `username`, `password` FROM login WHERE username LIKE '%s' LIMIT 1",
    mysqli_real_escape_string($verbindung, $_POST["username"]) 
);
$ergebnis = mysqli_query($verbindung,$abfrage);
// mysqli_query may fail at any time -> error handling required
if ( !$ergebnis ) {
    echo 'db query failed'; // $verbindung->error should contain more information
}
else if ( !($row = mysqli_fetch_assoc($ergebnis)) ) {
    echo 'no result'; // you probably shouldn't make a distinction between "no such record" and "wrong password" - just for illustration
}
else if ($row['password'] === $password) { // fetch_assoc returns an array, not an object
    $_SESSION["username"] = $username;
}
VolkerK
  • 95,432
  • 20
  • 163
  • 226
  • Thank you very much for those helpful suggestions. Gonna keep that in mind while reworking my whole script. – RedPanda Dec 02 '15 at 14:40
1

$row is an associative array, because you have used $row = mysqli_fetch_assoc($ergebnis); but you are treating $row as an object i.e. $row->password

So try:

if ($row['password'] === $password)
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
mbrother
  • 26
  • 3
0

use that like this

$row = mysqli_fetch_assoc($ergebnis);
if ($row['password'] === $password) {  
Arun Krish
  • 2,153
  • 1
  • 10
  • 15
0

Try with $row["password"]==$password

If it still shows the same thing, then var_dump $row and see if it returns a result.

trincot
  • 317,000
  • 35
  • 244
  • 286
Amir Saadallah
  • 668
  • 1
  • 8
  • 19