Showing posts with label login. Show all posts
Showing posts with label login. Show all posts

Wednesday, 20 November 2013

PHP MYSQL Login and Registration using Functions

// siddhu vydyabhushana // 172 comments
Today am going to explain you the code for Login and Registration using functions in php,mysql . The best methodology in any language is using Object Oriented Principles.Dealing with classes, objects very easy to manipulate information. Below code for how to develop user registration and login  form in php.

login and registration form using php functions

This tutorial contain only 2 files  functions.php and login_reg.php

Database
simple database with 4 fields id, name, email, password

CREATE TABLE IF NOT EXISTS `javatyro_registration` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` text NOT NULL,
  `email` varchar(100) NOT NULL,
  `password` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;

Functions.php
class name profile() -> object $profile

<?php
class profile
{
public function db()
{
mysql_connect("localhost","root","")or die(mysql_error());
mysql_select_db("demos") or die(mysql_error());
}
public function registration($name,$email,$password)
{
$query=mysql_query("SELECT * FROM javatyro_registration where email='$email' ");
if(!$query)
{
$query=mysql_query("INSERT INTO javatyro_registration VALUES ('','$name','$email','$password')");
if($query>0)
echo "<h2> hiee..<font color=green>".$name."</font>
</h2><br/><font color=green> You Registered Successfully</font>";
else
echo "<font color=red>failed</font>";
}
else
echo "<h3><font color=red>Sorry,Already Registered</font></h3>";
}
public function login($email,$password)
{
$query=mysql_query("SELECT name FROM javatyro_registration where 
email='$email' AND password='$password' ");
$query=mysql_fetch_array($query);
if($query>0)
echo "<h2> hiee..<font color=blue>".$query['name'].
"</font></h2><br/><font color=green>Login Success</font>";
else
echo "<font color=red>failed</font>";
}
}
?>
login_reg.php
code for login , registration from including php code
<!Doctype>
<html>
<head>
<?php include_once 'functions.php';?>
<style>
body{font-family:segoe ui}
.registration{background:#eeeeee;padding:20px}
.login{background:pink;padding:20px}
input{padding:5px}
</style>
</head>
<body>
<center>
<h2>JAVATYRO-<a href="http://www.facebook.com/siddhucse"
 target="_blank">SIDDHU VYDYABHUSHANA</a></h2>
<table>
<tr>
<td>
<div class="registration">
<form method="POST" action="login_reg.php">
Full Name:<br/>
<input type="text" name="name" required><br/>
Email:<br/>
<input type="email" name="email" required><br/>
Password:<br/>
<input type="password" name="pass" required><br/><br/>
<input type="submit" value="Register">
</form>
</div>
</td>
<td>
<div class="login">
<form method="POST" action="login_reg.php">
Email:<br/>
<input type="email" name="login_email" required><br/>
Password:<br/>
<input type="password" name="login_pass" required><br/><br/>
<input type="submit" value="Login">
</form>
</div>
</td>
</tr>
</table>
<?php
if($_SERVER['REQUEST_METHOD']=="POST")
{
//object for class
$profile=new profile();
//database calling
$profile->db();

//registration function calling
if(isset($_POST['name']) && isset($_POST['email'])
&& isset($_POST['pass']))
$profile->registration($_POST['name'],$_POST['email'],$_POST['pass']);

//login function calling
if(isset($_POST['login_email']) && isset($_POST['login_pass']))
$profile->login($_POST['login_email'],$_POST['login_pass']);

}
?>
</center>
</body>
</html>
How to add new Function:

public function function_name(argument1,argument2)
{
//query to execute
}
I hope you enjoyed my tutorial..
Read More