note 127629 added to function.strlen
| From: | un4tune8 at main dot php dot net | Date: | Wed, 14 Sep 2022 22:06:38 +0000 |
| Subject: | note 127629 added to function.strlen | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-219573@lists.php.net to get a copy of this message | ||
Strlen for Validation example:
<?php
session_start();
$pdo = new PDO('mysql:dbname=webshop;host=localhost','root','');
$name = '';
$plz = '';
$land = '';
$email = '';
$password = '';
$errors = [];
function validateHelp($label, $key, $value)
{
global $errors;
if (strlen($value) == 0) {
$errors[$key] = "$label darf nicht leer sein!";
return false;
} else {
return true;
}
}
function validate()
{
global $name;
global $plz;
global $land;
global $email;
global $password;
return validateHelp('Name', 'name', $name) &
validateHelp('PLZ', 'plz', $plz) &
validateHelp('Land', 'land', $land) &
validateHelp('Email', 'email', $email) &
validateHelp('Password', 'password', $password)
;
}
if (isset($_POST['submit'])) {
$name = isset($_POST['name']) ? $_POST['name'] : '';
$plz = isset($_POST['plz']) ? $_POST['plz'] : '';
$land = isset($_POST['land']) ? $_POST['land'] : '';
$email = isset($_POST['email']) ? $_POST['email'] : '';
$password = isset($_POST['password']) ? $_POST['password'] : '';
if(validate()){
$st = $pdo->prepare("select * from benutzer where email = ?");
$res=$st->execute(array($email));
$user= $st->fetch();
if($user == true){
echo 'Diese E-Mail-Adresse ist bereits vergeben<br>';
}else{
$ph=password_hash($password,PASSWORD_DEFAULT);
$st= $pdo->prepare("INSERT INTO benutzer (name, email, password) values
(?,?,?)");
$res= $st->execute(array($name,$email,$ph));
if($res){
echo "ERFOLG!.<a href='index.php'>ZUM LOGIN</a>";
}else{
echo "FEHLER";
}
}
}else{
foreach ($errors as $err){
echo "<li>".$err."</li><br>";
}
}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0,
minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Register</title>
</head>
<body>
<fieldset>
<form action="registrierung.php" method="post">
<label for="name" style="float: left; width:
100px">Name:</label>
<input
name="name"
type="text"
placeholder="Bitte ihren Vornamen"
value="<?= htmlspecialchars($name) ?>"
/><br></br>
<label for="plz" style="float: left; width:
100px">Postleitzahl:</label>
<input
name="plz"
type="number"
placeholder="Bitte ihre PLZ"
value="<?= htmlspecialchars($plz) ?>"
/><br></br>
<label for="land" style="float: left; width:
100px">Land:</label>
<input
name="land"
type="text"
placeholder="Ihr Heimatland?"
value="<?= htmlspecialchars($land) ?>"
/><br></br>
<label for="email" style="float: left; width:
100px">Email:</label>
<input
name="email"
type="email"
placeholder="Bitte gültige E-Mail Adresse eintragen"
value="<?= htmlspecialchars($email) ?>"
/><br></br>
<label for="password" style="float: left; width:
100px">Password:</label>
<input name="password"
type="password"
minlength="3"
value="<?= htmlspecialchars($password) ?>"
/><br></br>
<input type="submit"
value="Speichern"
name="submit"
>
</body>
</html>
----
Server IP: 45.112.84.4
Probable Submitter: 185.85.3.173 (proxied: 178.165.185.83)
----
Manual Page -- https://php.net/manual/en/function.strlen.php
Edit -- https://main.php.net/note/edit/127629
Del: integrated -- https://main.php.net/note/delete/127629/integrated
Del: useless -- https://main.php.net/note/delete/127629/useless
Del: bad code -- https://main.php.net/note/delete/127629/bad+code
Del: spam -- https://main.php.net/note/delete/127629/spam
Del: non-english -- https://main.php.net/note/delete/127629/non-english
Del: in docs -- https://main.php.net/note/delete/127629/in+docs
Del: other reasons-- https://main.php.net/note/delete/127629
Reject -- https://main.php.net/note/reject/127629
Search -- https://main.php.net/manage/user-notes.php