Question

develop a registration system. The user can register by submitting the required data in registration form. It needs to be checked whether the email already exists or not. If it exists then it should t...

develop a registration system. The user can register by submitting the required data in registration form. It needs to be checked whether the email already exists or not. If it exists then it should through an error just beside the email field, without submitting the form first. If the user name is unique then the entry should be submitted to the server where it is inserted into the database. This task will involve AJAX, PHP and MySQL

0 0
Add a comment Improve this question Transcribed image text
Answer #1

--------------------------------------------------------server.php--------------------------------------------

<?php
session_start();
//initializing Variables
$username ="";
$email = "";
$errors =array();

//Connect to the datebase

$db = mysqli_connect('localhost','root','','registration');

//Check Connection

if(mysqli_connect_errno())
{


    echo "Failed to connect to MySql";
    mysqli_connect_error();


}
//Register user

if(isset($_POST['reg_user']))
   //Receive all input values from the form
{


$username = mysqli_real_escape_string($db,$_POST['username']);
$email = mysqli_real_escape_string($db,$_POST['email']);
$password_1 = mysqli_real_escape_string($db,$_POST['password_1']);
$password_2 = mysqli_real_escape_string($db,$_POST['password_2']);
//Form validation : ensure that the form is corretly filled ...
//by adding (array_push()) corresponding error into $errors array

if(empty($username))
    {
       array_push($errors,"Username is required");
    }
if(empty($email))
           {
              array_push($errors,"Email is required");
           }
if(empty($password_1))
       {

           array_push($errors,"Password is required");
       }
if($password_1!=$password_2)
   {
       array_push($errors,"The two passwords do not match");
   }
//First check the database to make sure that
//a user does not already exist with the same username and/or email

$user_check_query = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1";
$result = mysqli_query($db,$user_check_query);
$user = mysqli_fetch_assoc($result);

if($user) //if user exits
{
if($user['username']=== $username)
    {
       array_push($errors,"Username already exists");
    }
if($user['email']=== $email)
    {
       array_push($errors,"Email already exists");
    }

}
//Finally register the user

if(count($errors)==0)
{

   $password = md5($password_1); //Encrypt the password before saving to database
   $query="INSERT INTO users (username,email,password) VALUES('$username','$email','$password')";
   mysqli_query($db,$query);
   $_SESSION['username'] = $username;
   $_SESSION['success'] = "You are now logged in";
   header('location:index.php');
}


}

//login User

if(isset($_POST['login_user']))
   {


      $username = mysqli_real_escape_string($db,$_POST['username']);
      $password = mysqli_real_escape_string($db,$_POST['password']);

      if(empty($username))
      {
array_push($errors,"Username is required");
      }
      if(empty($password))
          {
              array_push($errors,"Password is required");
          }
if(count($errors)== 0)
    {
        $password = md5($password);
        $query = "SELECT * FROM users WHERE username='$username' AND password='$password' ";
$results = mysqli_query($db,$query);
if(mysqli_num_rows($results)==1)
   {
      $_SESSION['username'] = $username;
      $_SESSION['success'] = "You are now logged in.";
      header('location: index.php');
   }
else
{
   array_push($errors,"Wrong username password combination");
}
    }


   }
?>

---------------------------------------------index.php-------------------------------------------------------

<?php
session_start();
if(!isset($_SESSION['username']))
{
$_SESSION['msg'] = "You must login first";
header('location:login.php');
}

if(isset($_GET['logout']))
{
    session_destroy();
    unset($_SESSION['username']);
    header('location:login.php');
}
?>

<!DOCTYPE html>
<html>
<haed>
   <title>Home</title>
   <link rel="stylesheets" type="test/css" href="style.css">
</head>
<body bgcolor="cyan">

    <div class="header">
        <h2>Home Page</h2>
        </div>
<div class="content">
    <!-- notification message -->
  
<?php if(isset($_SESSION['success'])) : ?>
<div class="error success" >
   <h3>
       <?php
       echo $_SESSION['success'];
unset($_SESSION['success']);
?>

</h3>
</div>
<?php endif ?>

<!-- Logged in user information-->
<?php if(isset($_SESSION['username'])) : ?>
<p>Welcome To Our site<strong><?php echo $_SESSION['username']; ?></strong></p>
<p><a href="index.php?logout=1" style="color:red;">Logout</a></p>
<?php endif ?>
</div>
    </body>
</html>

-------------------------------------register.php-----------------------------------------

<?php include('server.php') ?>
<!DOCTYPE html>
<html>
<head>
<title>Registration system</title>
<link rel="stylesheet" type="text/css" href="style.css">
<style>
background-image:url("/reg.jpg");
</style>
</head>
<body>
    <div class="header">
        <h2>Register</h2>
</div>
<form method="post" action="register.php">
   <?php include('errors.php'); ?>
   <div class="input-group">
       <label>Username</label>
       <input type="text" name="username" value="<?php echo $username; ?>" >
   </div>
   <div class="input-group">
       <label>Email</label>
       <input type="email" name="email" value="<?php echo $email; ?>">
   </div>
   <div class="input-group" >
       <label>Password</label>
       <input type="password" name="password_1">
   </div>
   <div class="input-group">
       <label>Confirm Password</label>
       <input type="password" name="password_2">
   </div>
   <div class="input-group">
       <button type="submit" class="btn" name="reg_user">Register</button>
   </div>
   <p>
       Already a member? <a href="login.php" >Sign In</a>
   </p>
</form>

</body>

</html>

--------------------------------login.php-------------------------------------------------

<?php include('server.php'); ?>
<!DOCTYPE html>
<html>
<head>
    <title>Registration System Login Page</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <div class="header">
        <h2>Login</h2>
    </div>
    <form method="post" action="login.php">
        <?php include('errors.php'); ?>
        <div class="input-group">
            <label>Username</label>
            <input type="text" name="username">
        </div>
        <div class="input-group">
            <label>Password</label>
            <input type="password" name="password">
        </div>
        <div class="input-group">
            <button type="submit" class="btn" name="login_user">Login</button>
        </div>
        <p>
            Not yet a member ?<a href="register.php">Sign Up</a>
        </p>
    </form>
</body>
</html>

-----------------------------------------errors.php------------------------------

<!--To display errors-->
<?php if(count($errors)>0) : ?>
   <div class="error">
       <?php foreach($errors as $error) : ?>
           <p> <?php echo $error ?></p>
       <?php endforeach ?>
   </div>
   <?php endif ?>

---------------------------------------------style.css---------------------------------

body {


   font-size:20px;
   background:black;


}
.header{


   width:30%;
   margin:50px auto 0px;
   color:red;
   background:black;
   text-align:center;
   border:1px solid #b0c4de;
   border-bottom:none;
   border-radius:10px 10px 0px 0px;
   padding:20px;


}
form, .content{


   width:30%;
   margin:0px auto;
   padding:20px;
   border:1px solid #b0c4de;
   background:white;
   border-radius:0px 0px 10px 10px;

}
.input-group{

   margin :10px 0px 10px 0px;


}
.input-group label{


display:block;
text-align:left;
margin:3px;


}
.input-group input{


   background-color:yellow;
height:30px;
width:90%;
padding:5px 10px;
font-size:16px;
border-radius:5px;
border:1px solid gray;


}
.btn{


   padding:10px;
font-size:25px;
color:white;
background:orange;
border:none;
border-radius:5px;


}


.error{


   width:92%;
   margin:0% auto;
   padding:10px;
   border:1px solid #a94442;
   color:red;
   background:#f2dede;
   border-radius:5px;
   text-align:left;


}
.success {

   color:#3c763d;
   background:#dff0d8;
   border:1px solid #3c763d;
   margin-bottom:20px;


}

Develop a Registration System X Registration System Login Page Registration system php and m × File | F:/Xampp/htdocs/Project

C Registration System Legin Pagex Develop A Registration System.T → C File | F:/Xampp/htdocs/Project/registrationlogin.html L

php treqistration) Sublime Text (UNFREGISTERED Eile Edit Selecti Find Miew Goto Tools Praject Preferences Help FOLDERS array_

0 php treqistration) Suclime Text (UNREGISTERED) Eile Edit Selecti Find Miew Goto Tools Praject Preferences Help FOLDERS く2ph

Add a comment
Know the answer?
Add Answer to:
develop a registration system. The user can register by submitting the required data in registration form. It needs to be checked whether the email already exists or not. If it exists then it should t...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Form Processing HTML One of the most ubiquitous uses of JavaScript is validating form data on...

    Form Processing HTML One of the most ubiquitous uses of JavaScript is validating form data on the client side before it is submitted to the server. It is done everywhere because it is fast and it gives you a great deal of flexibility in how you handle errors insofar as the GUI is concerned. Attached is an image of some code I wrote (so Blackboard can't mess it up). Some things to notice that will help you with the lab....

  • First, read the article on "The Delphi Method for Graduate Research." ------ Article is posted below...

    First, read the article on "The Delphi Method for Graduate Research." ------ Article is posted below Include each of the following in your answer (if applicable – explain in a paragraph) Research problem: what do you want to solve using Delphi? Sample: who will participate and why? (answer in 5 -10 sentences) Round one questionnaire: include 5 hypothetical questions you would like to ask Discuss: what are possible outcomes of the findings from your study? Hint: this is the conclusion....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT