0% found this document useful (0 votes)
216 views6 pages

Name - Mihir Humar REG NO. - 18BCE0828 Internet and Web Programming Lab Exercise 9

The document describes a lab exercise to create a user registration and login system using PHP and MySQL. It involves: 1) Creating a "Users" database with a "user" table containing username, password hash, and phone number fields. 2) Writing a PHP file to connect to the database. 3) Creating an index.php file with a registration form submitting to itself to insert user details into the database, displaying a confirmation message on success. 4) Creating a login.php file with a form submitting to itself to authenticate and start a session on correct credentials, displaying success or invalid messages. The code provided implements this system with two PHP files for registration and login functions connected to

Uploaded by

Mihir Humar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
216 views6 pages

Name - Mihir Humar REG NO. - 18BCE0828 Internet and Web Programming Lab Exercise 9

The document describes a lab exercise to create a user registration and login system using PHP and MySQL. It involves: 1) Creating a "Users" database with a "user" table containing username, password hash, and phone number fields. 2) Writing a PHP file to connect to the database. 3) Creating an index.php file with a registration form submitting to itself to insert user details into the database, displaying a confirmation message on success. 4) Creating a login.php file with a form submitting to itself to authenticate and start a session on correct credentials, displaying success or invalid messages. The code provided implements this system with two PHP files for registration and login functions connected to

Uploaded by

Mihir Humar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

NAME – MIHIR HUMAR

REG NO. - 18BCE0828

INTERNET AND WEB PROGRAMMING

LAB EXERCISE 9

1) Create a MySQL database called "Users"


2) Create a table called as "user" in the "Users" database with the following fields
◦ USERNAME VARCHAR(100)
◦ PASSWORD_HASH CHAR(40)
◦ PHONE VARCHAR(10)
• The USERNAME field should be designated as UNIQUE.

You can choose to do the above two steps directly in the MySQL Console or using PHP code.

3) Write a PHP file that can be added to other PHP files using the include or require functions. This file should:
• Make a connection to a MySQL database, using proper server name, username, password and db ame.
The connection resource should be stored in a variable with an appropriate name.

4) Sign-up

For this part, the entire code should be done in the same PHP file. The script should respond differently depending on
the situation (whether a POST request exists, whether the username is already taken, etc.). Wherever database connection
is required, use the script created in exercise 8. Do not add the db connection details to this page.

Write a PHP file that will output a form containing 3 fields: username, password, and phone number. These fields should
be sent via POST to the same file, which should take care of inserting them into the users table and then confirm the
registration by displaying the username and phone number back to the browser.

If the username already exists, your INSERT query should fail if you designated the USERNAME field as unique. You
should query the database before attempting the insert, and if the username exists already, display an error message and
a blank registration form again.

Note that the PASSWORD field assumes that you are storing a hex-string representation of a SHA-1/md5 hash of the
password. As explained in the lectures, you should never store passwords in plaintext. There are more secure ways of
storing the password. If you choose to use a different method, the PASSWORD_HASH field of the table may no longer be
CHAR(40) and you should
change it as appropriate.

5) Log-in

Write a PHP file that will output a form containing 2 fields: username and password. Upon submission of the form, the
code should check against the database to see whether the username-password pair was correct. If so, display a welcome
message. If not, display the message “Invalid username or password” followed by the same login form.

Once again, there should only be one PHP file, and you should redirect to the same place after submitting.

The output should be one of three options:


1. The login form.
2. The welcome message, if successful login.
3. The invalid message and the login form, if failed login.
CODE-
1.) INDEX.PHP

<?php
error_reporting(0);
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "users";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}

$username= $_POST["username"];
$password= $_POST["password"];
$number= $_POST["number"];

if (isset($_POST['registerSubmit'])){
    if( $username!="" && $password!="" && $number!=""){

    

$sql = "INSERT INTO user (password,number,username)
VALUES ('$password', '$number', '$username')";

if (mysqli_query($conn, $sql)) {
  echo " WELCOME ! YOU HAVE SUCCESSFULLY SIGNED IN
";
} else {
  echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
} else {
    Echo "enter all the details";
}
}
mysqli_close($conn);

?>
<html>
<head><title></title></head>
<body>
<form method='POST'>
<label>Username</label><input type="text" name="username"></br>
<label>Password</label><input type="password" name="password"></br>
<label>Number</label><input type="number" name="name"></br>
<input type="submit" name="registerSubmit"></form>
<a href="login.php"> LogIn</a>
</body>
<script>
    if ( window.history.replaceState ) {
        window.history.replaceState( null, null, window.location.href );
    }
</script>
</html>

2.) LOGIN.PHP

<?php
error_reporting(0);
session_start();
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "users";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}
$username= $_POST["username"];

$password= $_POST["password"];

if (isset($_POST['loginSubmit'])){
    if( $username!="" && $password!="" ){

    

        $sql = "SELECT id, username, password FROM user ";
        $result = mysqli_query($conn, $sql);
        
        if (mysqli_num_rows($result) > 0) {
          // output data of each row
          while($row = mysqli_fetch_assoc($result)) {
             if($row["password"]==$password){
                 Echo "success";
                 $_SESSION["user"] = $row["username"];
                 $_SESSION["uid"] = $row["id"];
                 header("Location: home.php");

             }
            
             else{
                 Echo "Incorrect email or password";
             }
          }
        } else {
          echo "0 results";
        }
      }
        }
        mysqli_close($conn);
      

?>
<html>
<head><title></title></head>
<body>
<form method='POST'>
<label>Username</label><input type="text" name="email"></br>

<label>Password</label><input type="password" name="password"></br>
<input type="submit" name="loginSubmit"></form>
<a href="index.php">SignUp</a>
</body>
<script>
    if ( window.history.replaceState ) {
        window.history.replaceState( null, null, window.location.href );
    }
</script>
</html>

SCREENSHOTS-
SIGNUP PAGE -

LOGIN PAGE -

You might also like