2016-06-06 20 views
-1

Das Problem, mit dem ich konfrontiert bin, ist, dass meine Preg_Match Validierung überhaupt nicht funktioniert. Alles, was ich tun muss, ist klicken Sie auf die Login-Schaltfläche und es meldet sich an. Ich brauche es zu sein, wenn der Benutzer nichts leer lässt oder einen Fehler macht, die Validierung den Fehler aufnimmt und die entsprechende Fehlermeldung anzeigt.PHP Login-Seite mit Preg_Match Validierung

Hier ist mein Code

Index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> 
<html> 
<head> 
<title>Login</title> 
</head> 
<body> 
<h1>Login</h1> 
<?php 
if (!isset($_POST['submit'])){ 
?> 
<!-- The HTML login form --> 
<form action="<?=$_SERVER['PHP_SELF']?>" method="post"> 
    Email:<br> 
    <input type="text" name="email" /><br /> 
    Password:<br> 
    <input type="password" name="password" /><br /><br/> 
    <input type="submit" name="submit" value="Login" /> 
    <a href="Register.php"/>Sign Up</a> 
</form> 
<?php 
} else { 
include("DBConn.php"); 
mysqli_connect("localhost","root","","test"); 

$email = $_POST['email']; 
$password = $_POST['password']; 

if(!$email) 
{ 
    echo('Enter an email'); 
} 
elseif(!preg_match('/^[a-z0-9][email protected][a-z\.]+$/i', $email)) 
{ 
    echo('Enter a valid email'); 
} 
if(preg_match('/^[0-9a-f]{50}$/', $password)) { 
    echo 'The password does not meet the requirements!'; 
} 

$sql = "SELECT * FROM tbl_user WHERE Email LIKE '{$email}' AND Password LIKE  '{$password}' LIMIT 1"; 
$result = mysqli_query($DBConnect,$sql); 
if (!$result == 1) { 
    echo "<p>Invalid email/password combination</p>"; 
} else { 
    echo "<p> Users Email is: $email</p>"; 
    echo "<p> Users Password is: $password </p>"; 
    echo "<p> Users Encrypted Password is: '".md5($password)."'</p>"; 
    echo"<p>Logged in successfully</p>"; 
?> 
    <a href="Item.php"/>Click here to go to Store.</a> 
<?php 
} 
} 
?>  
</body> 
</html> 

DBConn.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>Connect to DB</title> 
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> 
</head> 
<body> 
<?php 
$DBName = "Test"; 
$DBConnect = mysqli_connect ("127.0.0.1","root","","test"); 

if($DBConnect === FALSE) 
     echo "<p> Connection Failed /<p>\n"; 

    else { 
     echo "<p> Successfully connected to the " . "\"$DBName\" database .  </p>\n"; 
    } 
?> 
</body> 
</html> 

CreateTable.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>Create Table</title> 
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> 
</head> 
<body> 
<?php 
include("DBConn.php"); 
$TableName = "tbl_User"; 
$SQLString = "SHOW TABLES LIKE '$TableName'"; 
$QueryResult = mysqli_query($DBConnect,$SQLString); 
$FileName = 'userData.csv'; 

if($QueryResult = mysqli_query($DBConnect,$SQLString)) 
{ 
$numRows = mysqli_num_rows($QueryResult); 
} 
if($numRows == 0) 
echo "<p> This table does not exist and will be created now. "; 
else 
{ 
$SQLString = "DROP TABLE tbl_User"; 
$QueryResult = mysqli_query($DBConnect,$SQLString); 
if($QueryResult === FALSE) 
    echo "<p> Table already exsits</p>"; 
else 
    echo "<p> Successfully deleted the table.</p>"; 
} 
if($numRows == 0) 
{ 
$SQLString = "CREATE TABLE tbl_User (ID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY,FirstName VARCHAR(50),LastName VARCHAR(50),Email VARCHAR(50),Password VARCHAR(50))"; 

$QueryResult = mysqli_query($DBConnect , $SQLString); 
    if($QueryResult === FALSE) 
     echo "<p> Unable to create the tbl_User table. </p>" . "<p> Error Code" . 
    mysqli_errno($DBConnect) . ":" . mysqli_error($DBConnect) . "</p>"; 

    else 
     echo "<p> Successfully created the tbl_User table.</p>"; 
} 
if(file_exists($FileName)) 
{ 
echo "\nFile Exits"; 
$twoDarray = array(); 
if (($handle = fopen("userData.csv", "r")) !== FALSE) { 
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 
    $twoDarray[] = $data; 
} 
echo "<pre>\n"; 
print_r(array_filter($twoDarray[$data])); 
echo "</pre>"; 

foreach($twoDarray as $rows) 
{ 
    $SQLString = "INSERT INTO tbl_User(FirstName,LastName,Email,Password) VALUES ('$rows[0]','$rows[1]','$rows[2]','$rows[3]')"; 
    $QueryResult = mysqli_query($DBConnect,$SQLString); 
} 
fclose($handle); 
} 
else { 
echo "\nFile does not exsits, Wrong path"; 
} 
} 


?> 
+2

Das Passwort genau 50 Hex-Zahlen sein muss? Wie kommst du auf sowas? – mario

+0

In der Datenbank, die wir erstellt haben, war es eine Länge von 50 in phpMyAdmin @ mario – DevAnon

+1

Die Zahl, die Sie in 'PHPMyAdmin' sehen, ist eine maximale Länge, keine minimale oder genaue Länge. – Peter

Antwort

2

Erstens sieht es so aus, als ob Sie Klartext-Passwörter speichern und das ist unethisch und sehr unsicher.

Zweitens müssen Sie in mit Prepared Statements (MySQLi oder PDO) suchen

Drittens die Ausführung des SQL zu stoppen, wenn ein Fehler aufgetreten ifs auftritt dies ändern. Auf diese Weise, wenn ein Fehler Echo tritt der Fehler und stoppen Sie die SQL-Ausführung:

if(!$email) 
{ 
    $err_msg = 'Enter an email'; 
} 
elseif(!preg_match('/^[a-z0-9][email protected][a-z\.]+$/i', $email)) 
{ 
    $err_msg = 'Enter a valid email'; 
} 
if(preg_match('/^[0-9a-f]{50}$/', $password)) { 
    $err_msg .= ' The password does not meet the requirements!'; 
} 

if(!empty($err_msg)) { 
    echo $err_msg; 
} 
else { 
    $sql = "SELECT * FROM tbl_user WHERE Email LIKE '{$email}' AND Password LIKE  '{$password}' LIMIT 1"; 
    $result = mysqli_query($DBConnect,$sql); 
    if (!$result == 1) { 
     echo "<p>Invalid email/password combination</p>"; 
    } else { 
     echo "<p> Users Email is: $email</p>"; 
     echo "<p> Users Password is: $password </p>"; 
     echo "<p> Users Encrypted Password is: '".md5($password)."'</p>"; 
     echo"<p>Logged in successfully</p>"; 
} 
?> 

Viertens: Du bist gerade, dass es genau 50 von [0-9a-f] in Ihrem pregmatch von /^[0-9a-f]{50}$/ tun Sie dies /^[0-9a-f]{1,50}$/ ändern müssen {1,50} bedeutet zwischen 1 und 50. Dies bedeutet auch, dass ein Benutzer auch keine Großbuchstaben in seinem Passwort haben kann.

Vielleicht suchen Sie nach etwas in der Art von (?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}, das überprüft, dass es mindestens 1 Nummer, 1 Kleinbuchstabe, 1 Großbuchstabe und mindestens 6 Zeichen lang ist.

bearbeiten für Prepared Statements

Wenn MySQLi mit Prepared Statements Sie Folgendes tun müssen:

$username = $_POST['username']; 
$password = $_POST['password']; 

if($stmt = $DBConnect->prepare("SELECT * FROM table WHERE username = ? AND password = ? LIMIT 1")) { 
    $stmt->bind_param('ss', $username, $password); 
    if (!$stmt->execute()) { 
     die('execute() failed: ' . htmlspecialchars($stmt->error)); 
    } 
    $stmt->store_result(); 
    if ($stmt->num_rows == 1) { 
     echo 'success'; 
    } 
    else { 
     echo 'failure'; 
    } 
} 
else { 
    die('prepare() failed: ' . htmlspecialchars($DBConnect->error)); 
} 
$stmt->close(); 

Allerdings, wenn Sie mit Passwörtern beschäftigen sollten Sie wirklich mit ihnen phps Hashing werden Hashing (php) und Speichern eines Salzes, um auch abzurufen.

+0

Die Validierung funktioniert hier, aber wenn ich versuche, klicken Sie auf Anmelden es sagt Ungültige E-Mail/Passwort-Kombination. – DevAnon

+0

Ihre ähnlichen Bedingungen haben keine Platzhalter, die die Abfrage wahrscheinlich behindern. Aber bitte schauen Sie sich die vorbereiteten Anweisungen so schnell wie möglich für die Sicherheit Ihrer Datenbank an. – Matt

+0

Ich habe die vorbereiteten Aussagen so bald wie möglich zur Hauptfrage hinzugefügt, wenn das hilft. – DevAnon

0

Einige einfachere Validierungen sowohl für die E-Mail und Passwort:

if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) { 
    // Email is valid 
} 

if (ctype_xdigit($password) && strlen($password) == 50) { 
    // Password is valid 
}