Introduction

This article makes it easy to learn and use functions and sessions. Look at the following procedure.
Step 1
Create a database as in the following:
  1. Create database login_db;
Step 2
Create a table as in the following:
  1. CREATE TABLE IF NOT EXISTS `users` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `username` varchar(30) NOT NULL,
  4. `emailid` varchar(30) NOT NULL,
  5. `password` varchar(30) NOT NULL,
  6. PRIMARY KEY (`id`)
  7. ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
Step 3
Create a form named index.php as in the following:
  1. <!DOCTYPE html>
  2. <html lang="en" class="no-js">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>Login and Registration Form with HTML5 and CSS3</title>
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <meta name="description" content="Login and Registration Form with HTML5 and CSS3" />
  8. <meta name="keywords" content="html5, css3, form, switch, animation, :target, pseudo-class" />
  9. <meta name="author" content="Codrops" />
  10. <link rel="shortcut icon" href="../favicon.ico">
  11. <link rel="stylesheet" type="text/css" href="css/demo.css" />
  12. <link rel="stylesheet" type="text/css" href="css/style2.css" />
  13. <link rel="stylesheet" type="text/css" href="css/animate-custom.css" />
  14. </head>
  15. <body>
  16. <div class="container">
  17. <header>
  18. <h1>Login and Registration Form </h1>
  19. </header>
  20. <section>
  21. <div id="container_demo" >
  22. <a class="hiddenanchor" id="toregister"></a>
  23. <a class="hiddenanchor" id="tologin"></a>
  24. <div id="wrapper">
  25. <div id="login" class="animate form">
  26. <form name="login" method="post" action="">
  27. <h1>Log in</h1>
  28. <p>
  29. <label for="emailsignup" class="youmail" data-icon="e" > Your email</label>
  30. <input id="emailsignup" name="emailid" required="required" type="email" placeholder="[email protected]"/>
  31. </p>
  32. <p>
  33. <label for="password" class="youpasswd" data-icon="p"> Your password </label>
  34. <input id="password" name="password" required="required" type="password" placeholder="eg. X8df!90EO" />
  35. </p>
  36. <p class="keeplogin">
  37. <input type="checkbox" name="loginkeeping" id="loginkeeping" value="loginkeeping" />
  38. <label for="loginkeeping">Keep me logged in</label>
  39. </p>
  40. <p class="login button">
  41. <input type="submit" name="login" value="Login" />
  42. </p>
  43. <p class="change_link">
  44. Not a member yet ?
  45. <a href="#toregister" class="to_register">Join us</a>
  46. </p>
  47. </form>
  48. </div>
  49. <div id="register" class="animate form">
  50. <form name="login" method="post" action="">
  51. <h1> Sign up </h1>
  52. <p>
  53. <label for="usernamesignup" class="uname" data-icon="u">Your username</label>
  54. <input id="usernamesignup" name="username" required="required" type="text" placeholder="mysuperusername690" />
  55. </p>
  56. <p>
  57. <label for="emailsignup" class="youmail" data-icon="e" > Your email</label>
  58. <input id="emailsignup" name="emailid" required="required" type="email" placeholder="[email protected]"/>
  59. </p>
  60. <p>
  61. <label for="passwordsignup" class="youpasswd" data-icon="p">Your password </label>
  62. <input id="passwordsignup" name="password" required="required" type="password" placeholder="eg. X8df!90EO"/>
  63. </p>
  64. <p>
  65. <label for="passwordsignup_confirm" class="youpasswd" data-icon="p">Please confirm your password </label>
  66. <input id="passwordsignup_confirm" name="confirm_password" required="required" type="password" placeholder="eg. X8df!90EO"/>
  67. </p>
  68. <p class="signin button">
  69. <input type="submit" name="register" value="Sign up"/>
  70. </p>
  71. <p class="change_link">
  72. Already a member ?
  73. <a href="#tologin" class="to_register"> Go and log in </a>
  74. </p>
  75. </form>
  76. </div>
  77. </div>
  78. </div>
  79. </section>
  80. </div>
  81. </body>
  82. </html>
Step 4
Create a config file named config.php as in the following:
  1. <?php
  2. define("DB_HOST", 'localhost');
  3. define("DB_USER", 'root');
  4. define("DB_PASSWORD", '');
  5. define("DB_DATABSE", 'mypratice');
  6. ?>
Step 5
Make a database connection class. Create the file named dbConnect.php as in the following:
  1. <?php
  2. class dbConnect {
  3. function __construct() {
  4. require_once('config.php');
  5. $conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
  6. mysql_select_db(DB_DATABSE, $conn);
  7. if(!$conn)// testing the connection
  8. {
  9. die ("Cannot connect to the database");
  10. }
  11. return $conn;
  12. }
  13. public function Close(){
  14. mysql_close();
  15. }
  16. }
  17. ?>
Step 6
Make a Function class. Create the file named dbFunction.php as in the following:
  1. <?php
  2. require_once 'dbConnect.php';
  3. session_start();
  4. class dbFunction {
  5. function __construct() {
  6. // connecting to database
  7. $db = new dbConnect();;
  8. }
  9. // destructor
  10. function __destruct() {
  11. }
  12. public function UserRegister($username, $emailid, $password){
  13. $password = md5($password);
  14. $qr = mysql_query("INSERT INTO users(username, emailid, password) values('".$username."','".$emailid."','".$password."')") or die(mysql_error());
  15. return $qr;
  16. }
  17. public function Login($emailid, $password){
  18. $res = mysql_query("SELECT * FROM users WHERE emailid = '".$emailid."' AND password = '".md5($password)."'");
  19. $user_data = mysql_fetch_array($res);
  20. //print_r($user_data);
  21. $no_rows = mysql_num_rows($res);
  22. if ($no_rows == 1)
  23. {
  24. $_SESSION['login'] = true;
  25. $_SESSION['uid'] = $user_data['id'];
  26. $_SESSION['username'] = $user_data['username'];
  27. $_SESSION['email'] = $user_data['emailid'];
  28. return TRUE;
  29. }
  30. else
  31. {
  32. return FALSE;
  33. }
  34. }
  35. public function isUserExist($emailid){
  36. $qr = mysql_query("SELECT * FROM users WHERE emailid = '".$emailid."'");
  37. echo $row = mysql_num_rows($qr);
  38. if($row > 0){
  39. return true;
  40. } else {
  41. return false;
  42. }
  43. }
  44. }
  45. ?>
Step 7
After the preceding procedure use the Function in index.php as in the following:
  1. <?php
  2. include_once('dbFunction.php');
  3. $funObj = new dbFunction();
  4. if($_POST['login']){
  5. $emailid = $_POST['emailid'];
  6. $password = $_POST['password'];
  7. $user = $funObj->Login($emailid, $password);
  8. if ($user) {
  9. // Registration Success
  10. header("location:home.php");
  11. } else {
  12. // Registration Failed
  13. echo "<script>alert('Emailid / Password Not Match')</script>";
  14. }
  15. }
  16. if($_POST['register']){
  17. $username = $_POST['username'];
  18. $emailid = $_POST['emailid'];
  19. $password = $_POST['password'];
  20. $confirmPassword = $_POST['confirm_password'];
  21. if($password == $confirmPassword){
  22. $email = $funObj->isUserExist($emailid);
  23. if(!$email){
  24. $register = $funObj->UserRegister($username, $emailid, $password);
  25. if($register){
  26. echo "<script>alert('Registration Successful')</script>";
  27. }else{
  28. echo "<script>alert('Registration Not Successful')</script>";
  29. }
  30. } else {
  31. echo "<script>alert('Email Already Exist')</script>";
  32. }
  33. } else {
  34. echo "<script>alert('Password Not Match')</script>";
  35. }
  36. }
  37. ?>
  38. <!DOCTYPE html>
  39. <html lang="en" class="no-js">
  40. <head>
  41. <meta charset="UTF-8" />
  42. <title>Login and Registration Form with HTML5 and CSS3</title>
  43. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  44. <meta name="description" content="Login and Registration Form with HTML5 and CSS3" />
  45. <meta name="keywords" content="html5, css3, form, switch, animation, :target, pseudo-class" />
  46. <meta name="author" content="Codrops" />
  47. <link rel="shortcut icon" href="../favicon.ico">
  48. <link rel="stylesheet" type="text/css" href="css/demo.css" />
  49. <link rel="stylesheet" type="text/css" href="css/style2.css" />
  50. <link rel="stylesheet" type="text/css" href="css/animate-custom.css" />
  51. </head>
  52. <body>
  53. <div class="container">
  54. <header>
  55. <h1>Login and Registration Form </h1>
  56. </header>
  57. <section>
  58. <div id="container_demo" >
  59. <a class="hiddenanchor" id="toregister"></a>
  60. <a class="hiddenanchor" id="tologin"></a>
  61. <div id="wrapper">
  62. <div id="login" class="animate form">
  63. <form name="login" method="post" action="">
  64. <h1>Log in</h1>
  65. <p>
  66. <label for="emailsignup" class="youmail" data-icon="e" > Your email</label>
  67. <input id="emailsignup" name="emailid" required="required" type="email" placeholder="[email protected]"/>
  68. </p>
  69. <p>
  70. <label for="password" class="youpasswd" data-icon="p"> Your password </label>
  71. <input id="password" name="password" required="required" type="password" placeholder="eg. X8df!90EO" />
  72. </p>
  73. <p class="keeplogin">
  74. <input type="checkbox" name="loginkeeping" id="loginkeeping" value="loginkeeping" />
  75. <label for="loginkeeping">Keep me logged in</label>
  76. </p>
  77. <p class="login button">
  78. <input type="submit" name="login" value="Login" />
  79. </p>
  80. <p class="change_link">
  81. Not a member yet ?
  82. <a href="#toregister" class="to_register">Join us</a>
  83. </p>
  84. </form>
  85. </div>
  86. <div id="register" class="animate form">
  87. <form name="login" method="post" action="">
  88. <h1> Sign up </h1>
  89. <p>
  90. <label for="usernamesignup" class="uname" data-icon="u">Your username</label>
  91. <input id="usernamesignup" name="username" required="required" type="text" placeholder="mysuperusername690" />
  92. </p>
  93. <p>
  94. <label for="emailsignup" class="youmail" data-icon="e" > Your email</label>
  95. <input id="emailsignup" name="emailid" required="required" type="email" placeholder="[email protected]"/>
  96. </p>
  97. <p>
  98. <label for="passwordsignup" class="youpasswd" data-icon="p">Your password </label>
  99. <input id="passwordsignup" name="password" required="required" type="password" placeholder="eg. X8df!90EO"/>
  100. </p>
  101. <p>
  102. <label for="passwordsignup_confirm" class="youpasswd" data-icon="p">Please confirm your password </label>
  103. <input id="passwordsignup_confirm" name="confirm_password" required="required" type="password" placeholder="eg. X8df!90EO"/>
  104. </p>
  105. <p class="signin button">
  106. <input type="submit" name="register" value="Sign up"/>
  107. </p>
  108. <p class="change_link">
  109. Already a member ?
  110. <a href="#tologin" class="to_register"> Go and log in </a>
  111. </p>
  112. </form>
  113. </div>
  114. </div>
  115. </div>
  116. </section>
  117. </div>
  118. </body>
  119. </html>
Step 8
Create a Home Page named home.php as in the following:
  1. <?php
  2. include_once('dbFunction.php');
  3. if($_POST['welcome']){
  4. // remove all session variables
  5. session_unset();
  6. // destroy the session
  7. session_destroy();
  8. }
  9. if(!($_SESSION)){
  10. header("Location:index.php");
  11. }
  12. ?>
  13. <!DOCTYPE html>
  14. <html lang="en" class="no-js">
  15. <head>
  16. <meta charset="UTF-8" />
  17. <title>Login and Registration Form with HTML5 and CSS3</title>
  18. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  19. <meta name="description" content="Login and Registration Form with HTML5 and CSS3" />
  20. <meta name="keywords" content="html5, css3, form, switch, animation, :target, pseudo-class" />
  21. <meta name="author" content="Codrops" />
  22. <link rel="shortcut icon" href="../favicon.ico">
  23. <link rel="stylesheet" type="text/css" href="css/demo.css" />
  24. <link rel="stylesheet" type="text/css" href="css/style2.css" />
  25. <link rel="stylesheet" type="text/css" href="css/animate-custom.css" />
  26. </head>
  27. <body>
  28. <div class="container">
  29. <header>
  30. <h1>Welcome Form </h1>
  31. </header>
  32. <section>
  33. <div id="container_demo" >
  34. <a class="hiddenanchor" id="toregister"></a>
  35. <a class="hiddenanchor" id="tologin"></a>
  36. <div id="wrapper">
  37. <div id="login" class="animate form">
  38. <form name="login" method="post" action="">
  39. <h1>Welcome </h1>
  40. <p>
  41. <label for="emailid" class="uname" > Your Name </label>
  42. <?=$_SESSION['username']?>
  43. </p>
  44. <p>
  45. <label for="email" class="youpasswd" > Your Email </label>
  46. <?=$_SESSION['email']?>
  47. </p>
  48. <p class="login button">
  49. <input type="submit" name="welcome" value="Logout" />
  50. </p>
  51. </form>
  52. </div>
  53. </div>
  54. </div>
  55. </section>
  56. </div>
  57. </body>
  58. </html>
Step 9
After the preceding procedure has been done run your program. After running it the following will be the output screens.
1- Login Screen
Login Screen
2- Registration Screen
Registration Screen
3- Home Screen
Home Screen