Worth2Read
Home

Forum

PHP Tutorials
Blogs
Children
Article Directory

Contact Us

Disclaimer
Reliable $1 Web Hosting by 3iX
 

If you have any doubts with your PHP code please discuss it in the PHP Forum

Login Screen using PHP

A sample of Login Screen is as follows

Password   

The code for the above form is as follows.

<html>

<body>
<form action='loginck.php' method=post>
<table>

<tr>

<td> Login ID </td>

<td> <input type ='text' name='userid' ></td>

</tr>

<tr><td> Password </td> <td> <input type ='text' name='password' ></font></td></tr>

<tr> <td> <input type='submit' value='Login'> <input type='reset' value='Reset'></td> </tr>

<tr> <td > <a href='signup.php'>New Member Sign UP</a></td>

<td> <a href="forgot_password.php" > Forgot Password ?</a></td></tr>

</table>

</form>

</body></html>

Once this form is submitted the values are passed to loginck.php. Following is the code for loginck.php

<?php

session_start();
$userid=$_POST['userid'];
$password=$_POST['password'];
$db = mysql_connect ( "localhost" , "username" , "password" )or die('Cannot connect to the database because: ' . mysql_error());
mysql_select_db ("databasename");
if($rec=mysql_fetch_array(mysql_query("SELECT * FROM tb_signup WHERE userid='$userid' AND password = '$password'"))){
if(($rec['userid']==$userid)&&($rec['password']==$password)){

echo "<p class=data> <center>Successfully,Logged in<br>

<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>";


$_SESSION['user_is_logged_in'] = true;
$_SESSION['userid'] = $userid;
}
}
else {

session_unset();
echo "<font face='Verdana' size='2' color=red>Wrong Login. Use your correct Userid and Password and Try <br><center>
<input type='button' value='Retry' onClick='history.go(-1)'></center>";
session_destroy();
}

signup.php : Signing Up / Registering with the site

<form id="form1" name="form1" method="post" action="confirm.php">

<label>User ID :<input name="userid" type="text" id="userid" size="20" /> </label>


<p>Password :<label> <input name="password" type="text" id="password" size="30" /> </label> (Minimum 5 characters)</p>

<p> <label>Email : <input name="email" type="text" id="email" size="50" /> </label> </p>

<p>Name <label> <input name="name" type="text" id="name" size="50" /> </label> </p>
<p> <label> <input type="submit" name="Submit" value="Sign Up" /> </label> </p>

</form>

 

confirm.php


<?php
$db = mysql_connect ( "localhost" , "user name " , "password" )or die('Cannot connect to the database because: ' . mysql_error());
mysql_select_db ("database name ");

$userid=$_POST['userid'];
$password=$_POST['password'];
$password2=$_POST['password2'];
$submit=$_POST['submit'];
$email=$_POST['email'];
$name=$_POST['name'];

$status = "OK";
$msg="";

// 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>&nbsp;</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>";}
?>

Copyright © 2008 www.worth2read.org. All rights Reserved. Use of this Site is subject to our Privacy Statement

Untitled Document
PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide
Programming PHP
Beginning PHP and MySQL: From Novice to Professional, Third Edition (Beginning from Novice to Professional)
Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems
PHP and MySQL Web Development (3rd Edition) (Developer's Library)
PHP & MySQL For Dummies 3rd edition (For Dummies (Computer/Tech))
PHP Solutions: Dynamic Web Design Made Easy (Solutions)
Build Your Own Website The Right Way Using HTML & CSS
Learning Web Design: A Beginner's Guide to (X)HTML, StyleSheets, and Web Graphics
Sams Teach Yourself HTML and CSS in 24 Hours (7th Edition) (Sams Teach Yourself)