Random Password Generator Tutorial

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Random Password Generator Tutorial

Post by evilcoder »

Well, i'm still at work, going on 12hours now. So i thought i'd write a small tutorial on how to send random passwords to people who signup for an account on your site, if you have that option.

Here goes:

Page: register.php

Code: Select all

<html>
<head>
<title>Site name</title>
</head>
<body>

<form name="signup" method="post" action="register.php?join=1">
<table width="100%" border="0" cellspacing="0" cellpadding="3" align="center">
  <tr> 
    <td><b> 
      Membership
      </b></td>
  </tr>
  <tr>
    <td class="menubold">Enter Details

<hr size="1" width="100%" noshade>
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr> 
            <td width="36%">First Name</td>
            <td width="0%"> 
              <div align="center">:</div>
            </td>
            <td width="64%"> 
              <input type="text" name="fname" size="16" value="" style="font-size: 10px; font-family: arial; 
			  border: 1px #39597D solid; background: #D2D2D2; color: #000000">
            </td>
          </tr>
          <tr> 
            <td width="36%">Surname</td>
            <td width="0%"> 
              <div align="center">:</div>
            </td>
            <td width="64%"> 
              <input type="text" name="surname" size="16" value="" style="font-size: 10px; font-family: arial; 
			  border: 1px #39597D solid; background: #D2D2D2; color: #000000">
            </td>
          </tr>
          <tr> 
            <td width="36%">Other Names</td>
            <td width="0%"> 
              <div align="center">:</div>
            </td>
            <td width="64%"> 
              <input type="text" name="othername" size="16" value="" style="font-size: 10px; font-family: arial; 
			  border: 1px #39597D solid; background: #D2D2D2; color: #000000">
            </td>
          </tr>
          <tr> 
            <td width="36%" height="34">Year of Birth</td>
            <td width="0%" height="34"> 
              <div align="center">:</div>
            </td>
            <td width="64%" height="34"> 
              <input type="text" name="dob" size="16" value="" style="font-size: 10px; font-family: arial; 
			  border: 1px #39597D solid; background: #D2D2D2; color: #000000" maxlength="4">
              ( yyyy) </td>
          </tr>
          <tr> 
            <td width="36%">Country</td>
            <td width="0%"> 
              <div align="center">:</div>
            </td>
            <td width="64%"> 
              <input type="text" name="country" size="16" value="" style="font-size: 10px; font-family: arial; 
			  border: 1px #39597D solid; background: #D2D2D2; color: #000000">
            </td>
          </tr>
          <tr> 
            <td width="36%">State</td>
            <td width="0%"> 
              <div align="center">:</div>
            </td>
            <td width="64%"> 
              <input type="text" name="state" size="16" value="" style="font-size: 10px; font-family: arial; 
			  border: 1px #39597D solid; background: #D2D2D2; color: #000000">
            </td>
          </tr>
          <tr> 
            <td width="36%">Postcode</td>
            <td width="0%"> 
              <div align="center">:</div>
            </td>
            <td width="64%"> 
              <input type="text" name="postcode" size="16" value="" style="font-size: 10px; font-family: arial; 
			  border: 1px #39597D solid; background: #D2D2D2; color: #000000">
            </td>
          </tr>
          <tr> 
            <td width="36%">&nbsp;</td>
            <td width="0%"> 
              <div align="center"></div>
            </td>
            <td width="64%">&nbsp;</td>
          </tr>
          <tr> 
            <td width="36%">Email Address</td>
            <td width="0%"> 
              <div align="center">:</div>
            </td>
            <td width="64%"> 
              <input type="text" name="email" size="16" value="" style="font-size: 10px; font-family: arial; 
			  border: 1px #39597D solid; background: #D2D2D2; color: #000000">
            </td>
          </tr>
          <tr> 
            <td width="36%">Username</td>
            <td width="0%"> 
              <div align="center">:</div>
            </td>
            <td width="64%"> 
              <input type="text" name="username" size="16" value="" style="font-size: 10px; font-family: arial; 
			  border: 1px #39597D solid; background: #D2D2D2; color: #000000">
            </td>
          </tr>
          <tr> 
            <td width="36%">Password</td>
            <td width="0%"> 
              <div align="center">:</div>
            </td>
            <td width="64%"><b>Will be sent to your email address.</b></td>
          </tr>
          <tr> 
            <td width="36%">Join Mailing List?</td>
            <td width="0%"> 
              <div align="center">:</div>
            </td>
            <td width="64%"> 
              <input type="radio" name="mailing" value="1">
              <b></b> Yes 
              <input type="radio" name="mailing" value="0">
              No </td>
          </tr>
          <tr> 
            <td width="36%">&nbsp;</td>
            <td width="0%"> 
              <div align="center"></div>
            </td>
            <td width="64%">&nbsp;</td>
          </tr>
          <tr> 
            <td width="36%">&nbsp; </td>
            <td width="0%"> 
              <div align="center"></div>
            </td>
            <td width="64%">
              <input type="submit" name="submit" value="Join" style="font-size: 10px; font-family: arial; 
			  border: 1px #39597D solid; background: #D2D2D2; color: #000000">
              &nbsp; 
              <input type="reset" name="submit2" value="Reset" style="font-size: 10px; font-family: arial; 
			  border: 1px #39597D solid; background: #D2D2D2; color: #000000">
            </td>
          </tr>
        </table>
    </td>
  </tr>
</table>
</form>
<p>
<?php
@mysql_connect('hostname','username','password') or die ("Can't connect with the database."); // your Database Settings
@mysql_select_db('database_name') or die ("Can't connect with the database table."); // Database Name

foreach($HTTP_GET_VARS as $key=>$value) 
{ 
$$key = $value; 
global $$key; 
}
foreach ($HTTP_POST_VARS as $key=>$value) 
{ 
$$key = $value; 
global $$key; 
}
foreach ($HTTP_SERVER_VARS as $key=>$value)
{
$$key = $value;
global $key;
}

$date = date("d/m/Y");
$siteinfo = mysql_query("SELECT `url`,`site_name` FROM `site_info`");
$aemail = mysql_fetch_array(mysql_query("select email from admin where ID='1'"));
$get = mysql_fetch_array($siteinfo);

	if(IsSet($join))
	{
		$_post['email'];
		$_post['mailing'];
		$_post['username'];
		$_post['country'];
		$_post['dob'];
		$_post['fname'];
		$_post['surname'];
		$_post['postcode'];
		$_post['othername'];
		$_post['state'];

			function random_char($string)
			{
				$length = strlen($string);
				$position = mt_rand(0, $length - 1);
				return($string[$position]);
			}
			function random_string ($charset_string, $length)
			{
				$return_string = random_char($charset_string);
				for ($x = 1; $x < $length; $x++)
				$return_string .= random_char($charset_string);
				return($return_string);
			} 
		mt_srand((double)microtime() * 1000000);
		$charset = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTWXYZ";
		$charset1 = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTWXYZ";
		$password = random_string($charset, 6);
		$login_code = random_string($charset1, 6);
		$Epassword = MD5($password);

			if(Empty($username))
			{
				echo "<b>Please Fill in all the fields</b>";
			}
			
			if (Empty($email))
			{
				echo "<b>Please Fill in all the fields</b>";
			}
			else
			{
				$check = mysql_query("SELECT `username` FROM `users` WHERE 1 AND `username` LIKE '$username'");
				$records = mysql_num_rows($check);
				$checke = mysql_query("SELECT `email` FROM `users` WHERE 1 AND `email` LIKE '$email'");
				$recordse = mysql_num_rows($checke);
				
					if($records > 0)
					{
						echo .$username. " is already in use, please choose a differnet username...";
					}
					if($recordse > 0)
					{
						echo .$email. " has already signed up for an account. Please use another one.";
					}
					else
					{
						$query1 = "INSERT INTO `users` (`username`, `password`, `email`, `mailing_list`, `date`, 
						`last_login`, `pass`, `login_code`, `fname`, `surname`, `othername`, `country`, `state`, 
						`postcode`, `dob`) VALUES ('$username','$Epassword', '$email', '$mailing', '$date', 
						'00/00/00', '$password', '$login_code', '$fname', '$surname','$othername', '$country',
						'$state', '$postcode', '$dob')";
						
						$in = mysql_query($query1) or die ("Unable to Insert new Member...please contact the 
						webmaster...");
						
						echo "<div class='menubold'><b>Congratulations! You are now a member						Thank you and Enjoy!</b></div>";
						
						$femail = "DONT-REPLY@yoursite.com";
						
$body1 = "Welcome to my site, thanks for signing up, heres ya details:

Username: $username
Password: $password

Got here to login: Login page here.

";
mail("$email","Site Signup","$body1","From:$femail");
							echo "<meta http-equiv ="Refresh" content = "3;  URL=../index.php">";
}
}
}


?>
</p>
</body>
</html>
In order for this to work however, the following you must save the following and dump it into your MySQL DB:

Code: Select all

CREATE TABLE `users` (
  `ID` int(11) NOT NULL auto_increment,
  `username` text NOT NULL,
  `password` text NOT NULL,
  `email` text NOT NULL,
  `mailing_list` text NOT NULL,
  `date` text NOT NULL,
  `last_login` text NOT NULL,
  `pass` text NOT NULL,
  `login_code` text NOT NULL,
  PRIMARY KEY  (`ID`)
) TYPE=MyISAM;

CREATE TABLE `admin` (
  `ID` int(11) NOT NULL auto_increment,
  `username` text NOT NULL,
  `password` text NOT NULL,
  `email` text NOT NULL,
  PRIMARY KEY  (`ID`)
) TYPE=MyISAM;

INSERT INTO `admin` (`ID`, `username`, `password`, `email`) VALUES (1, 'admin', '1a1dc91c907325c69271ddf0c944bc72', 'admin@yoursite.com');
There ya go. Now happy registering. Maybe if i were a big more bothered i would have explained it all, but i'm a lazy sh!t and i've just finsihed early so i'm off to get <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span>!
Post Reply