0

Note: I am German. So some irrelevant text is in german like e.g. some php echo messages.

Probably it is only a beginner mistake. But I searched all night long and did not find an answer to my problem. I hope you can help me.

I want to create a php login script with XXAMP and its mysql-database. I created a database via phpmyadmin.

[Here could be an image of this database] - But: "You need at least 10 reputation to post images." Sry.

Then I worked on the login.php script. It looks so:

<?php
session_start();
?>

<?php
$verbindung = mysql_connect("127.0.0.1", "myname" , "mypassword")
or die("Verbindung zur Datenbank konnte nicht hergestellt werden");
mysql_select_db("homepage") or die ("Datenbank konnte nicht ausgewählt werden");

$username = $_POST["username"];
$password = md5($_POST["password"]);

$abfrage = "SELECT username, password FROM access WHERE username LIKE '$username' LIMIT 1";
$ergebnis = mysql_query($abfrage);
$row = mysql_fetch_object($ergebnis);

// $count = mysql_num_rows($ergebnis);

if($row->password == $password) {
    $_SESSION["username"] = $username;
    echo "Login erfolgreich. <br> <a href=\"home.php\">Weiter</a>";
    }
else {
    echo "Benutzername und/oder Passwort waren falsch. <a href=\"index.htm\">Login</a>";
    }
?>

I already tested the connection to the database in a separate php file. It works.

But when I try to login on my index.htm:

<html>
<head>
         <title>Login</title>
</head>
<body>

         <form action="login.php" method="post">
                 <input type="text" size="24" maxlength="50" name="username"<br>
                 <input type="password" size="24" maxlength="50" name="password"<br>
                 <input type="submit" value="Login">
         </form>

</body>
</html>

I get the answer on login.php:

password == $password) { $_SESSION["username"] = $username; echo "Login erfolgreich. Weiter"; } else { echo "Benutzername und/oder Passwort waren falsch. Login"; } ?>

I tried so much things, but nothing helped. Maybe you have an idea why it does not work?
I would be very grateful.

2
  • 1
    It looks like your code is breaking on the > of if($row->. my guess is that php is not running, as it possibly assumes it is html code, and that is the closing tag of <?php. Commented Jul 30, 2015 at 2:59
  • You better get started learning techniques with PDO .. mysql_connct is out of fashion. By the time you get the hang of it, mysql_connect will be on some gravestone... ;-).. Look up some youtubes. They are a good start. Commented Jul 30, 2015 at 5:39

5 Answers 5

1

try this index.html

<html>
<head>
         <title>Login</title>
</head>
<body>

         <form action="login.php" method="post">
                 <input type="text" size="24" maxlength="50" name="username"<br>
                 <input type="password" size="24" maxlength="50" name="password"<br>
                 <input type="submit" value="Login">
         </form>

</body>
</html>

and login.php

<?php
session_start();

$verbindung = mysql_connect("127.0.0.1", "myname" , "mypassword")
or die("Verbindung zur Datenbank konnte nicht hergestellt werden");
mysql_select_db("project") or die ("Datenbank konnte nicht ausgewählt werden");

$username = $_POST["username"];
$password = md5($_POST["password"]);

$abfrage = "SELECT username, password FROM user WHERE username LIKE '".$username."' LIMIT 1";
$ergebnis = mysql_query($abfrage);
if (!$ergebnis)
{
 die("mySQL error: ". mysql_error());  

 }
$row = mysql_fetch_object($ergebnis);

// $count = mysql_num_rows($ergebnis);

if($row->password == $password) {
    $_SESSION["username"] = $username;
    echo "Login erfolgreich. <br> <a href=\"home.php\">Weiter</a>";
    }
else {
    echo "Benutzername und/oder Passwort waren falsch. <a href=\"index.htm\">Login</a>";
    }

?>

Note::The mysql extension is deprecated.if you are doing new project then use mysqli

Sign up to request clarification or add additional context in comments.

Comments

1

Are you sure it's saved as a .PHP file?

Comments

0

Would you try code below:

<?php
session_start();?>

<?php
$verbindung = mysql_connect("127.0.0.1", "myname" , "mypassword")
or die("Verbindung zur Datenbank konnte nicht hergestellt werden");
mysql_select_db("homepage") or die ("Datenbank konnte nicht ausgewählt werden");

$username = $_POST["username"];
$password = md5($_POST["password"]);

$abfrage = "SELECT username, password FROM access WHERE username='$username' LIMIT 1";
$ergebnis = mysql_query($abfrage) or die("error");
$row = mysql_fetch_array($ergebnis);

// $count = mysql_num_rows($ergebnis);

if($row['password'] == $password) {
$_SESSION["username"] = $username;
echo "Login erfolgreich. <br> <a href=\"home.php\">Weiter</a>";
}
else {
echo "Benutzername und/oder Passwort waren falsch. <a href=\"index.htm\">Login</a>";
}
?>

Please let me if this works or not.

1 Comment

Then I get following answer on login.php: Weiter"; } else { echo "Benutzername und/oder Passwort waren falsch. Login"; } ?> Maybe I can test with some echo which information is not send/processed correctly.
0

here is a complete login authentication script:

<?php
session_start();

//Include db connection
include 'db_connect.php';

//Get inputs and encode password to md5() remove that if no md5 enc.
$Username = mysql_real_escape_string($_POST['username']);
$Password = md5(mysql_real_escape_string($_POST['password']));


//Username check query
$Check_if_Username = mysql_query("SELECT * FROM Users WHERE Username = '$Username' AND Password = '$Password'");

//Email check query
$Check_if_Mail = mysql_query("SELECT * FROM Users WHERE Email = '$Username' AND Password = '$Password'");


//Check if Username returns any result
if(mysql_num_rows($Check_if_Username)==1){

    //Data was found, populate variables:
    $data = mysql_fetch_array($Check_if_Username);
    $_SESSION['Username'] = $data['Username'];
    $_SESSION['LoggedIn'] = TRUE;
}

//Otherwise, check for email match
else if(mysql_num_rows($Check_if_Mail)==1){

    //Data was found, populate variables:
    $data = mysqli_fetch_array($Check_if_Mail);
    $_SESSION['Username'] = $data['Username'];
    $_SESSION['LoggedIn'] = TRUE;

}

//No data was found:
else{
    mysql_close();
    header("Location: /login.php?error=invalid data");
    die;
}



//If the user was logged in and the data was found, close mysql connection
mysql_close();

//Then redirect
header("Location: /account.php");

Comments

0

So I find out what was going wrong. I am embarrassed about my dumb fault.
It was not a question about programming, but the directory of opening the php files in my browser. I opened it from htdocs in xampp, but it must be done with localhost/... .
Nevertheless I am very thankful about your fast answers! I think, I should read some explaining stuff about all these programms and modules first.

See you later. Sometime I will answer some of your questions. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.