Ich bin neu in PHP und ich versuche, ein einfaches Login-System zu entwickeln, wo es eine Erfolgsmeldung auf eine sichere Seite und leitet um, wenn Details falsch sind, es eine Fehlermeldung und lädt das Login-Formular neu.Wie kann ich ein PHP-Anmeldeskript erhalten, um Benutzer korrekt einzuloggen?
Ich habe versucht, für eine Weile jetzt und kann es nicht herausfinden, obwohl ich einige Funktionen in Bezug auf die richtige Seite habe.
Meine Datenbank auf PhpMyAdmin ist korrekt konfiguriert. Außerdem wird jede Hilfe zu Sitzungen sehr geschätzt.
PHP CODE:
<?php
$servername = "localhost";
$username = "root";
$password = "cornwall";
$con=mysqli_connect('localhost','root','cornwall','ibill');
// This code creates a connection to the MySQL database in PHPMyAdmin named 'ibill':
$username = $_POST['username'];
$password = $_POST['password'];
//These are the different PHP variables that store my posted data.
$login="SELECT * FROM users WHERE username='$username' AND password='$password'";
$result=mysqli_query($con, $login);
$count=mysqli_num_rows($result);
//This is the query that will be sent to the MySQL server.
if($count==1)
{
header('Location: http://localhost/projects/ibill_v3/html/main.html#home');
exit();
}
//This checks the 'user_details' database for correct user registration details and if successful, directs to home page.
else {
header('Location: http://localhost/projects/ibill_v3/html/loginform.html');
echo "Wrong details";
exit();
}
//If login details are incorrect
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
?>
HMTL CODE
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1; minimum-scale=1;">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<link href="/projects/ibill_v3/css/mainstyles.css" rel="StyleSheet"/>
<link href="/projects/ibill_v3/css/loginform.css" rel="StyleSheet"/>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script src="script.js"></script>
<script type='text/javascript' src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<script type='text/javascript'>
$(document).on('pageinit', function(){
$('.loginform').validate({ // initialize the plugin
// rules & options
});
});
</script>
</head>
<body>
<!--********************************LOGIN FORM PAGE**********************************************-->
<!--****************************************************************************************-->
<!--********************************HEADER**********************************************-->
<div data-role="page" id="loginform">
<div data-role="header" data-id="foo1" data-position="fixed">
<h1>Register</h1>
</div>
<!--********************************HEADER**********************************************-->
<!--********************************MAIN**********************************************-->
<div data-role="main" class="ui-content">
<img class="mainlogo" src="/projects/ibill_v3/img/ibill logo.png" alt="iBill Logo" width="250" height="190">
<h2>Sign in</h2>
<section class="loginform">
<form data-ajax="false" method="POST" action="loginform.php" >
<ul>
<li>
<label for="username">Username</label>
<input type="text" name="username" id="username" class="required" minlength="5" placeholder="enter username (min-5 characters)">
</li>
<li>
<label for="password">Password</label>
<input type="password" name="password" placeholder="enter password" minlength="6">
</li>
<div id="loginformbutton">
<button class='active' type='submit' value='submit'>Sign in</button>
</div>
<p>Don't have an account? Sign up!</p>
<div id="registerbutton">
<a href="/projects/ibill_v3/html/register.html" data-role="button">Register</a>
</div>
</ul>
</form>
</section>
</div>
<!--********************************MAIN**********************************************-->
<!--********************************FOOTER**********************************************-->
<div data-role="footer">
<footer class="footer">
<p>awilliams©</p>
</footer>
</div>
</div>
<!--********************************END OF LOGIN FORM PAGE**********************************************-->
<!--****************************************************************************************-->
</body>
** WARNUNG **: Bei der Verwendung von 'mysqli' Sie sollte parametrisierte Abfragen und ['bind_param'] (http://php.net/manual/en/mysqli-stmt.bind-param.php) verwenden, um Benutzerdaten zu Ihrer Abfrage hinzuzufügen. ** Verwenden Sie NICHT ** String-Interpolation oder Verkettung, um dies zu erreichen, weil Sie einen schwerwiegenden [SQL injection bug] erstellt haben (http://bobby-tables.com/). ** NIEMALS ** $ _POST' oder '$ _GET' Daten direkt in eine Abfrage einfügen. – tadman
** WARNUNG **: Es ist nicht einfach, Ihre eigene Zugriffskontrollschicht zu schreiben, und es gibt viele Möglichkeiten, sie ernsthaft falsch zu verstehen. Bitte schreiben Sie nicht Ihr eigenes Authentifizierungssystem, wenn Sie ein modernes [Entwicklungsframework] (http://codegeekz.com/best-php-frameworks-for-developers/) wie [Laravel] (http://laravel.com/) verfügt über ein robustes [Authentifizierungssystem] (https://laravel.com/docs/5.2/authentication). Am absolutesten folgen Sie [empfohlene Best Practices für die Sicherheit] (http://www.phptherightway.com/#security) und speichern Sie Passwörter niemals als Nur-Text. – tadman
@PedroLobito Dieses Tutorial hat Klartext-Passwörter und verwendet keine vorbereiteten Anweisungen. Es ist ein hervorragendes Beispiel dafür, was nicht zu tun ist. Dies ist ein gelöstes Problem in jedem Framework. Verwenden Sie einfach, was auch immer sie zur Verfügung stellen. Keine Notwendigkeit, das Rad neu zu erfinden und es schlecht zu machen. – tadman