<br><a href='logout.php'> Log OUT </a><br>
<br><a href=welcome.php>Click here if your browser is not redirecting automatically or you don't want to wait.</a><br></center>";
print "<script>";
//print " self.location='welcome.php';"; // Comment this line if you don't want to redirect
print "</script>";
// if userid is less than 3 char then status is not ok
if(!isset($userid) or strlen($userid) <3){
$msg=$msg."User id should be =3 or more than 3 char length<BR>";
$status= "NOTOK";}
if(!ctype_alnum($userid)){
$msg=$msg."User id should contain alphanumeric chars only<BR>";
$status= "NOTOK";}
if(mysql_num_rows(mysql_query("SELECT userid FROM tb_signup WHERE userid = '$userid'"))){
$msg=$msg."Userid already exists. Please try another one<BR>";
$status= "NOTOK";}
if(mysql_num_rows(mysql_query("SELECT email FROM tb_signup WHERE email = '$email'"))){
$msg=$msg."An account on this email id already exists. Please Use the correct email address. If you forgot your user id request to send it to your email address<BR>";
echo "<font face='Verdana' size='2' color=green><br><br><a href=forgot.php>Forgot my password</a><br></font>";
$status= "NOTOK";}
if ( strlen($password) < 5 ){
$msg=$msg."Password must be more than 5 char legth<BR>";
$status= "NOTOK";}
if($status<>"OK"){
echo "<font face='Verdana' size='2' color=red>$msg</font><br><input type='button' value='Retry' onClick='history.go(-1)'>"; }
else{ // if all validations are passed.
$query=mysql_query("insert into tb_signup(userid,password,email,name) values('$userid','$password','$email','$name')");
echo "<font face='Verdana' size='2' color=green>Welcome, You have successfully signed up<br><br><a href=login.php>Click here to login</a><br></font>";
}
mysql_close($db);
?>
You can see a demo of this code at http://www.worth2read.org/test/signup.php
Sending the forgotten password to the mail id
Forgot.php
<form id="form1" name="form1" method="post" action="send_password.php">
<p>
<label>Enter your mail Id
<input name="email" type="text" id="email" size="30" />
</label>
</p>
<p>You will receive a mail with your login details shortly ! </p>
<p>
<label>
<input type="submit" name="Submit" value="Send me the password" />
</label>
</p>
<p> </p>
</form>
send_password.php
<?php
$email=$_POST['email'];
$msg="";
$db = mysql_connect ( "localhost" , "username" , "password" )or die('Cannot connect to the database because: ' . mysql_error());
mysql_select_db ("database name ");
$query="SELECT email,userid,password FROM tb_signup WHERE tb_signup.email = '$email'";
$st=mysql_query($query);
$recs=mysql_num_rows($st);
$row=mysql_fetch_object($st);
$em=$row->email;// email is stored to a variable
if ($recs == 0) { // No records returned, so no email address in our table
echo "<center><font face='Verdana' size='2' color=red><b>No Password</b><br> Sorry Your address is not there in our database . You can signup and login to use our site. <BR><BR><a href='signup.php'> Sign UP </a> </center>";
exit;}
// formating the mail posting
// headers here
$headers4="reachme@worth2read.org"; // Change this address within quotes to your address
$headers.="Reply-to: $headers4\n";
$headers .= "From: $headers4\n";
$headers .= "Errors-to: $headers4\n";
//$headers = "Content-Type: text/html; charset=iso-8859-1\n".$headers;
// for html mail un-comment the above line
// mail funciton will return true if it is successful
if(mail("$em","Your Request for login details","This is in response to your request for login detailst at Worth2read.org \n \nLogin ID: $row->userid \n Password: $row->password \n\n Thank You \n \n siteadmin","$headers")){echo "<center><font face='Verdana' size='2' ><b>THANK YOU</b> <br>Your password is send to your email address . Please check your mail after some time. </center>";}
else{// there is a system problem in sending mail
echo " <center><font face='Verdana' size='2' color=red >There is some problem with the system in sending login details to your address. Please contact directly at reachme@worth2read.org. <br><br><input type='button' value='Retry' onClick='history.go(-1)'></center></font>";}
}
else {// Validation failed so show the error message
echo "<center><font face='Verdana' size='2' color=red >$msg <br><br><input type='button' value='Retry' onClick='history.go(-1)'></center></font>";}
?>