Registration,login & Email validation in php

Step 1: insert.php

lt;?php
session_start();
echo $_SESSION['UNAME'];

?gt;


lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"gt;
lt;html xmlns="http://www.w3.org/1999/xhtml"gt;
lt;headgt;
lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /gt;
lt;titlegt;Untitled Documentlt;/titlegt;

lt;bodygt;

lt;?php echo $_REQUEST['err']; ?gt;
lt;table width="300" border="1" align="center" cellpadding="0" cellspacing="1"gt;
lt;trgt;
lt;tdgt;lt;form name="form1" method="post" action="insert_action.php" gt;
lt;table width="100%" border="0" cellspacing="1" cellpadding="3"gt;
lt;trgt;
lt;td colspan="3" align="center"gt;lt;stronggt;Registrationlt;/stronggt;lt;/tdgt;
lt;/trgt;
lt;trgt;
lt;td colspan="3" align="center"gt;lt;stronggt;
lt;?php
if($_SESSION['UNAME'])
{
?gt;
lt;a href="logout.php"gt;Logoutlt;/stronggt;lt;/agt;
lt;?php
}
else
{
?gt;
lt;a href="login.php"gt;Loginlt;/stronggt;lt;/agt;
lt;?php
}
?gt;


lt;/tdgt;
lt;/trgt;
lt;trgt;
lt;td width="71"gt;Namelt;/tdgt;
lt;td width="6"gt;:lt;/tdgt;
lt;td width="301"gt;lt;input name="name" type="text" id="name"gt;lt;/tdgt;
lt;/trgt;
lt;trgt;
lt;tdgt;Lastnamelt;/tdgt;
lt;tdgt;:lt;/tdgt;
lt;tdgt;lt;input name="lastname" type="text" id="lastname"gt;lt;/tdgt;
lt;/trgt;
lt;trgt;
lt;tdgt;Emaillt;/tdgt;
lt;tdgt;:lt;/tdgt;
lt;tdgt;lt;input name="email" type="text" id="email" gt;lt;/tdgt;
lt;/trgt;

lt;trgt;
lt;td colspan="3" align="center"gt;lt;input type="submit" name="Submit" value="Submit"gt;lt;/tdgt;
lt;/trgt;

lt;/tablegt;
lt;/formgt;
lt;/tdgt;
lt;/trgt;
lt;/tablegt;
lt;/bodygt;
lt;/htmlgt;



Step 2: insert_action.php
lt;?php
include("connect.php");

// get values from from

function isValidEmail($email){
$pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";

if (eregi($pattern, $email)){
return true;
}
else {
return false;
}
}


if ($_POST['Submit'])
{

$name=$_POST['name'];
$lastname=$_POST['lastname'];
$email= $_REQUEST['email'];


if(!isValidEmail($email))
{
$err = "Invalid Email";

}

if($err==NULL)
{

$sql="INSERT INTO test_mysql(name,lastname,email)VALUES ( '$name','$lastname', '$email')";
$result=mysql_query($sql);
}
else

{
?gt;
lt;body onload="insertform.submit();"gt;
lt;form name="insertform" method="post" action="insert.php"gt;
lt;input type="hidden" name="err" value="lt;?php echo $err; ?gt;"
lt;/formgt;
lt;/bodygt;
lt;?php

}

}

// Insert data into mysql
if($result)
{
echo "Congratulations!Please login ";
echo "lt;BRgt;";
echo "lt;a href='login.php'gt;loginlt;/agt;";
echo "lt;BRgt;";

echo "lt;a href='list_records.php'gt;Click here to edit/deletelt;/agt;";
}


/*
{
echo "sorry ! U are not succesful";
}*/
mysql_close();
?gt;


Step 3: login.php


lt;htmlgt;
lt;bodygt;
lt;table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"gt;
lt;trgt;
lt;form name="form1" method="post" action="login_action.php"gt;
lt;tdgt;
lt;table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"gt;
lt;trgt;
lt;td colspan="3"gt;lt;stronggt;Member Login lt;/stronggt;lt;/tdgt;
lt;/trgt;
lt;trgt;
lt;td width="78"gt;namelt;/tdgt;
lt;td width="6"gt;:lt;/tdgt;
lt;td width="294"gt;lt;input name="uname" type="text" id="name"gt;lt;/tdgt;
lt;/trgt;
lt;trgt;
lt;tdgt;Emaillt;/tdgt;
lt;tdgt;:lt;/tdgt;
lt;tdgt;lt;input name="email" type="text" id="email"gt;lt;/tdgt;

lt;/trgt;
lt;trgt;
lt;tdgt; lt;/tdgt;
lt;tdgt; lt;/tdgt;
lt;tdgt;lt;input type="submit" name="Submit" value="Login"gt;lt;/tdgt;
lt;/trgt;
lt;/tablegt;
lt;/tdgt;
lt;/formgt;
lt;/trgt;
lt;/tablegt;
lt;/bodygt;
lt;/htmlgt;


Step 4: login_action.php


lt;?php
session_start();
include("connect.php");


// get value from from
$name=$_REQUEST['uname'];
$email=$_REQUEST['email'];
$id=$_REQUEST['id'];

/// To protect MySQL injection (more detail about MySQL injection)
$name = stripslashes($name);
$email = stripslashes($email);
$name = mysql_real_escape_string($name);
$email = mysql_real_escape_string($email);


$sql="SELECT * FROM test_mysql where name='$name' and email='$email' ";
$rs=mysql_query($sql);

$result = mysql_fetch_array($rs);

echo $result['name'];
echo $result['email'];

if($result['name']==$name && $result['email']==$email)
{


$_SESSION['UNAME']=$name;

header("location:list_records.php");

}
else
echo "Wrong Username or Password";






//$count=mysql_num_rows($result);
//if($count==1)
//{
//session_register='$name';
//session_register='$email';
//header:"location:success.php";

//}


//else {
//echo "Wrong Username or Password";
//}

?gt;

0 Response to "Registration,login & Email validation in php"

Post a Comment

powered by Blogger | WordPress by Newwpthemes | Converted by BloggerTheme