Hi friends,Today I am going to teach you how to create a Register and Login form using PHP.Firstly you should want to create a database.You can create it using WampServer or Mysql.You can create a database using following code.
CREATE TABLE admin ( id INT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(30) UNIQUE, password VARCHAR(30) );
2)After that you want create a Database configuration file.
Config.php
<?php $mysql_hostname = "localhost";//host name $mysql_user = "root";//user name $mysql_password = "industrial";//pass word $mysql_database = "database";//database name $bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password); /* if ( !$bd ) { echo "Error connecting to database.\n";//This is check whether to db connect or not } else { echo " succesfully connnected" ; } */ mysql_select_db($mysql_database, $bd); ?>
3)Login.php
<html> <head> <title>Pharmacy Database</title> </head> <body> <h1>Pharmacy Database</h1> <?php include("Config.php"); session_start(); if($_SERVER["REQUEST_METHOD"] == "POST") { // username and password sent from Form $myusername=addslashes($_POST['username']); $mypassword=addslashes($_POST['password']); $sql="SELECT id FROM admin WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql); $row=mysql_fetch_array($result); $active=$row['active']; $count=mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1) { session_register("myusername"); $_SESSION['login_user']=$myusername; header("location: welcome.php"); } else { $error="Your Login Name or Password is invalid"; } } ?> <table> <tr> <form action="" method="post"> <td width="81">Username:</td> <td width="247"><input name="username" size="30" autocomplete="off" value="" type="text" /></td> </tr> <tr> <td>Password:</td> <td><input name="password" size="30" type="password" /></td> </tr> </table> <p><input type="submit" value="Submit"/></p> </form> </body> </html>
4)Next step is lock.php…This is Session verification. If no session value page redirect to Login.php
<?php include('config.php'); session_start(); $user_check=$_SESSION['login_user']; $ses_sql=mysql_query("select username from admin where username='$user_check' "); $row=mysql_fetch_array($ses_sql); $login_session=$row['username']; if(!isset($login_session)) { header("Location: login.php"); } ?>
5)welcom.php
<?php include('lock.php'); ?> <body> <h1>Welcome <?php echo $login_session; ?></h1> </body>
6)logout.php
SignOut Destroy the session value
SignOut Destroy the session value
<?php session_start(); if(session_destroy()) { header("Location: login.php"); } ?>
7)This is Register.html Form
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Register to Pharmacy</title> </head> <body><h1>Register to Pharmacy Database</h1> <table> <tr> <form action=register.php method=post> <td width="81">Username:</td> <td width="247"><input name="username" size="30" autocomplete="off" value="" type="text" /></td> </tr> <tr> <td>Password:</td> <td><input name="password" size="30" type="password" /></td> </tr> </table> <p><input type="submit" value="Register"/></p> </form> </body> </html>
8. register.php
<?php include 'Config.php'; $username = $_POST['username']; $password = $_POST['password']; //$ip = $_SERVER['REMOTE_ADDR']; $result = mysql_num_rows(mysql_query("SELECT * FROM admin WHERE username = '$username'")); if($result == 1) { echo "The username you have chosen already exists!"; } else { mysql_query("INSERT INTO admin (username, password) VALUES ('$username', '$password')"); echo "<p>Congratulations! You have successfully registered! </p> <p>Click <a href = \"Login.php\">here</a> to login.</p>"; } ?>