<?php
$password = $_POST["password"];
if (isset($password) == true){
echo "your password = ".$password."<hr>";
echo "Encrypted password = ".md5($password)."<hr>";
}else{
echo "please enter your password";
};
echo'
<form action="index.php" method="post" >
password : <input type="password" name="password">
<input type="submit" />
';
?>
in this code i want to encrypt user password using md5 function
but when the user enter nothing it echo
d41d8cd98f00b204e9800998ecf8427e
and used isset function to fix this but it is not working
this code worked
<?php
$password = $_POST["password"];
if ($password == ""){
echo "please enter your password";
}else{
echo "your password = ".$password."<hr>";
echo "Encrypted password = ".md5($password)."<hr>";
};
echo'
<form action="index.php" method="post" >
password : <input type="password" name="password">
<input type="submit" />
';
?>
Try this way:
// set the variable if it was given, otherwise emtpy
$password = array_key_exists('password', $_POST) ? $_POST['password'] : '';
// confirm this is not an empty string
if ($password != '') {
// ... the rest
Change this
$password = $_POST["password"];
if (isset($password) == true){
echo "your password = ".$password."<hr>";
echo "Encrypted password = ".md5($password)."<hr>";
}else{
echo "please enter your password";
};
to this
if (isset($_POST["password"]) == true){
$password = $_POST["password"];
echo "your password = ".$password."<hr>";
echo "Encrypted password = ".md5($password)."<hr>";
}else{
echo "please enter your password";
};
Because, when you first time load you page the $_POST['password']
is not set. So it will face trouble to load you form.
Actually $_POST
as explained here is an associative array of variables passed to the current script via the HTTP POST. So its good practice to check existence of any expected variables directly using the $_POST
itself and before using them
e.g. array_key_exists('password', $_POST);
, isset($_POST['password']);
, etc.
In other word the condition in your script can be changed to:
if (array_key_exists('password', $_POST)){
$password = $_POST["password"];
echo "your password = ".$password."<hr>";
echo "Encrypted password = ".md5($password)."<hr>";
}else{
echo "please enter your password";
};