Files from 27-03-26: Password Hashing and checks for password stregnth
This commit is contained in:
@@ -7,6 +7,10 @@ Wichtige Informationen
|
||||
- Wird eventuell nie aktuell sein.
|
||||
- Stundenplan - [Link](https://wvss-mannheim.webuntis.com/WebUntis/?school=wvss-mannheim#/basic/timetablePublic/class?entityId=2583)
|
||||
|
||||
## 27-03-26 - PHP (Unterrichts Projekt)
|
||||
- Registrierung weiter gemacht
|
||||
- Passwort checks
|
||||
- Passwort hashing
|
||||
|
||||
## 25-03-26 - PHP (Unterrichts Projekt)
|
||||
- Registrierung gemacht
|
||||
|
||||
47
Zweites Jahr/unterrichts_projekt/portal/functions.php
Normal file
47
Zweites Jahr/unterrichts_projekt/portal/functions.php
Normal file
@@ -0,0 +1,47 @@
|
||||
|
||||
|
||||
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Check password strength (1/2)
|
||||
* @param string $password password to check
|
||||
* @return bool - true or false
|
||||
*/
|
||||
function isStrongPassword(string $password): bool
|
||||
{
|
||||
// min. 8 Zeichen, 1 Kleinbuchstabe, 1 Großbuchstabe, 1 Zahl, 1 Sonderzeichen
|
||||
return (bool) preg_match(
|
||||
'/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^a-zA-Z0-9]).{8,}$/',
|
||||
$password
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check password strength (2/2)
|
||||
* @param string $password password to check
|
||||
* @return bool - true or false
|
||||
*/
|
||||
function checkPasswordStrength(string $password): bool {
|
||||
$strength = 0;
|
||||
|
||||
//check length
|
||||
$length = strlen($password);
|
||||
if($length >= 8) $strength++;
|
||||
|
||||
//check if caps
|
||||
if(preg_match('/[A-Z]/', $password)) $strength++;
|
||||
|
||||
//check if lower cases
|
||||
if(preg_match('/[a-z]/', $password)) $strength++;
|
||||
|
||||
//check if numbers
|
||||
if(preg_match('/[0-9]/', $password)) $strength++;
|
||||
|
||||
//check if spec. chars
|
||||
if(preg_match('/[^a-zA-Z0-9]/', $password)) $strength++;
|
||||
|
||||
return($strength >= 5);
|
||||
|
||||
|
||||
}
|
||||
@@ -14,8 +14,11 @@
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
||||
<?php
|
||||
require_once '../inc/db_connection_function.php';
|
||||
require_once 'functions.php';
|
||||
$dbh = db_connect();
|
||||
$errors = '';
|
||||
|
||||
@@ -24,6 +27,12 @@ if(!empty($_REQUEST)) { // Button wurde gedrückt?
|
||||
$email = $_REQUEST['email'];
|
||||
$pw = $_REQUEST['pw'];
|
||||
|
||||
if(isStrongPassword($pw)){ // hier kann man die gegeben Funktion nutzen oder "checkPAsswordStrength" (aus functions.php)
|
||||
$pw = password_hash($pw, PASSWORD_DEFAULT);
|
||||
} else {
|
||||
$errors .= "Passwort zu schwach!<br>";
|
||||
}
|
||||
|
||||
$query = "INSERT INTO user VALUES (NULL, :email, :pw)";
|
||||
$stmt = $dbh->prepare($query);
|
||||
|
||||
@@ -36,13 +45,17 @@ if(!empty($_REQUEST)) { // Button wurde gedrückt?
|
||||
$errMsg = $e->getMessage();
|
||||
$errCode = $e->getCode();
|
||||
|
||||
echo $e;
|
||||
|
||||
switch($errCode) {
|
||||
case "23000": $custErrMsg = "<p>Email-Adress already exists!</p>"; break;
|
||||
default: $custErrMsg = "<p>Oooops, something went wrong!</p>";
|
||||
}
|
||||
echo $custErrMsg;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$errors .= "Beide Felder müssen ausgefüllt werden<br>";
|
||||
} // Ende Felder müssen ausgefüllt werden.
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user