Home » Generate unique password using PHP function
Php

Generate unique password using PHP function

In this tutorial, I will discuss generate unique password using PHP function. Nowadays the security of user’s data is the most important part in web development. There are too many ways which are used for making user’s information secure. Secure password of the user’s account is the most important one among all of theme. For that, I have to make our password string strong and unique. If your password is weak and easy then the hacker can easily get that using brute force attack.

So that I have created one PHP function for generating unique password very easily. All the PHP developers can easily set up in their project and use it very well manner. I will show you how to setup PHP function and how you can use it in your project.

I have divided this tutorial in 4 steps:

1) PHP function code
2) Implementation
3) Use the Function
4) Conclusion

Follow the steps for generate unique password using PHP function

Generate unique password using PHP function

1) PHP function code

Here is the main PHP function code for generate unique password. In this function I have used four type of charecters. Numeric, Alpha numeric, Integer and Special charecters. Special charecters make password string very strong.

function getPassword($length)
{
    $myChars = "abcdefghijklmnopqrstuvwxyz";
    $myChars .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $myChars .= "1234567890";
    $myChars .= "!@#$%^&*()_+<>?:-|\/";
	
    $myPass = "";
	
    mt_srand((double)microtime()*1234567);
	
    for($r=0;$r<$length;$r++)
    $myPass .= $myChars[mt_rand(0,strlen($myChars)-1)];
	
    return $myPass;
}

2) Implementation

You can copy the function code and paste it in your "function.php" or "header.php" files. So that you can use this function in your whole project anywhere you want. You just need to call the function. How to call the function? check the next step.

3) Use the Function

Now after the implementation of PHP function you can use this function anywhere you want in your project. Just follow the below instructions.

$length = 6;  // Length of password
$pass = getPassword($length);
echo $pass;

4) Conclusion

Now you can generate unique password using PHP function. All the steps are very easy to understand and the implementation of the function is also very easy.

Most recommending articles :

I hope you will like this tutorial and enjoyed it. If you have any questions then please comment below your question or contact me by contact us form. Don't forget to share this post with your friends.

About the author

John Patel

John Patel is an experienced blogger and web developer. He always likes to learn something new. He is helping the new developers through Web development and SEO(Search Engine Optimization) tutorials, tips and tricks articles.

Add Comment

Click here to post a comment

SUBSCRIBE TO JOIN US

Join as a subscriber and get notifications of our latest articles,tutorials & tips/tricks directly to your inbox. For Free.
%d bloggers like this: