Reputation: 404
How to convert a password string to specified symbol like *
or something else.
I'm currently working on change password page.
I would like to display the password to page, and i want to avoid the password overlook by someone, so i want to covert the password's string to symbol like *
with same lengths.
An example like <input type="password" />
.
Sorry for my bad english...
Upvotes: 3
Views: 4817
Reputation: 88677
While there are many answers here demonstrating the user of str_repeat()
, I sort of doubt that is what you want. After all, any idiot can pad a string with a character, and there would be little point in doing so when one can simply use, as you rightly pointed out, <input type="password" />
(yes, you could still go get the password from the source code, but then why bother populating an obfuscated field at all? Or not just populating it with a static fixed number of * characters?).
I suspect you are looking for something more like this:
<?php
$thePassword = "thisIsMyPassword";
?>
<input type="text" id="input_user_types_in" value="<?php echo str_repeat('*', strlen($thePassword)); ?>" />
<input type="hidden" id="value_to_post_back" name="value_to_post_back" value="<?php echo $thePassword; ?>" />
<script type="text/javascript">
var textInput = document.getElementById('input_user_types_in');
var hiddenInput = document.getElementById('value_to_post_back');
textInput.onfocus = function() {
this.value = hiddenInput.value;
};
textInput.onblur = function() {
var i;
hiddenInput.value = this.value;
this.value = '';
for (i = 0; i < hiddenInput.value.length; i++) {
this.value = this.value + '*';
}
};
</script>
Have a fiddle around with it ;-)
Upvotes: 0
Reputation: 7517
You don't output your password in your HTML, you create an asterix (*
) representation of your password. This is easily done by the str_repeat function:
<?php
$password = "MyPasswordThatIWantToMask";
$maskedPassword = str_repeat("*", strlen($password));
?>
Now you just output the $maskedPassword
as the value of the password field.
However, another very interesting thing: how do you know the users password length? I sincerely hope you hash your passwords rather than having them around plain text.
Upvotes: 1
Reputation: 268364
If you're looking for a simple way to just create a string of asterisks equal to the length of a password:
$modified = str_repeat( "*", strlen( "secretpass" ) );
echo $modified; // **********
Upvotes: 1
Reputation: 5374
One way would be to use an input of type password but set it to disabled.
<input type='password' disabled='disabled' value='somepassword' />
Upvotes: 0
Reputation: 1710
do it like this:
$passwordstring = "password";
$outputpassword = "";
while(strlen($outputpassword)<strlen($passwordstring))
{
$outputpassword .= "*";
}
echo $outputpassword
Upvotes: 0