Osc - Experiment - 6

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

EXPERIMENT - 6

MEMBERS : 1. Atharva Tarlekar (201070066)


2. Tanmay Navandar (201070080)

AIM : To Create login and Registration forms for web application using PHP script.

THEORY :
PHP is a server-side scripting programming language, and MySQL is an open-source relational
database management system. These two frameworks, when used together, are capable of
providing highly unique solutions, like creating a login form.

CREATING DATABASE TABLE USING MYSQL :

Here we created a database expt_6 which has a table named as signup. All the
inputs in the form will be recorded in this table.
CODE :

signup.php :

<?php
session_start();

include("connection.php");
include("functions.php");

if($_SERVER['REQUEST_METHOD'] == "POST")
{
//something was posted
$first_name = $_POST['firstName'];
$last_name = $_POST['lastName'];
$gender = $_POST['gender'];
$email = $_POST['email'];
$password = $_POST['password'];
$contact = $_POST['number'];

if(!empty($email) && !empty($password) && !is_numeric($first_name))


{
//save to database
$user_id = random_num(20);
$query = "insert into signup
(user_id,first_name,last_name,gender,email,password,contact) values
('$user_id','$first_name','$last_name','$gender','$email','$password','$contact')
";

mysqli_query($con, $query);

header("Location: login.php");
die;
}else
{
echo "Please enter some valid information!";
}
}
?>

<!-- Form Code -->


<!DOCTYPE html>
<html>

<head>
<title>Registration Page</title>
<link rel="stylesheet" type="text/css" href="signup.css" />
</head>

<body>
<div class="container">
<div class="row col-md-6 col-md-offset-3">
<div class="panel panel-success">
<div class="panel-heading text-center">
<h1>SignUp</h1>
</div>
<div class="panel-body">
<form action="signup.php" method="post">
<div class="form-group">
<label for="firstName">First Name</label>
<input type="text" class="form-control" id="firstName"
name="firstName" />
</div>
<div class="form-group">
<label for="lastName">Last Name</label>
<input type="text" class="form-control" id="lastName"
name="lastName" />
</div>
<div class="form-group">
<label for="gender">Gender</label>
<div>
<label for="male" class="radio-inline"><input type="radio"
name="gender" value="m"
id="male" />Male</label>
<label for="female" class="radio-inline"><input type="radio"
name="gender" value="f"
id="female" />Female</label>
<label for="others" class="radio-inline"><input type="radio"
name="gender" value="o"
id="others" />Others</label>
</div>
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="text" class="form-control" id="email" name="email" />
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" id="password"
name="password" />
</div>
<div class="form-group">
<label for="number">Phone Number</label>
<input type="number" class="form-control" id="number" name="number"
/>
</div>
<input type="submit" class="btn btn-dark" />
</form>
</div>

</div>
</div>
</div>

</body>

</html>

login.php :
<?php

session_start();

include("connection.php");
include("functions.php");

if($_SERVER['REQUEST_METHOD'] == "POST")
{
//something was posted
$email = $_POST['email'];
$password = $_POST['password'];

if(!empty($email) && !empty($password) )


{

//read from database


$query = "select * from signup where email = '$email' limit 1";
$result = mysqli_query($con, $query);

if($result)
{
if($result && mysqli_num_rows($result) > 0)
{

$user_data = mysqli_fetch_assoc($result);

if($user_data['password'] === $password)


{

$_SESSION['user_id'] = $user_data['user_id'];
header("Location: index.php");
die;
}
}
}

echo "wrong username or password!";


}else
{
echo "wrong username or password!";
}
}

?>
<!DOCTYPE html>
<html>
<head>
<title>login Page</title>
<link rel="stylesheet" type="text/css" href="login.css">
</head>
<body>
<div class="container vh-100">
<div class="row justify-content-center h-100">
<div class="card w-25 my-auto shadow">
<div class="card-header text-center bg-dark text-white">
<h2>Login</h2>
</div>
<div class="card-body">
<form action="login.php" method="post">
<div class="form-group">
<label for="email">Email</label>
<input type="email" id="email" class="form-control"
name="email" />
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" id="password" class="form-
control" name="password" />
</div>
<input type="submit" class="btn btn-outline-dark w-100"
value="Login" name="">
</form>
</div>
</div>
</div>
</div>
</body>
</html>

index.php :
<?php
session_start();

include("connection.php");
include("functions.php");
$user_data = check_login($con);
?>

<!DOCTYPE html>
<html>
<head>
<title>Experiment 6</title>
<link rel="stylesheet" type="text/css" href="login.css">
</head>
<body>
<h1>Logged in successfully!</h1>
<br>
Welcome, <?php echo $user_data['email']; ?>
<br>
<a href="logout.php" class="btn btn-dark btn-lg active"
role="button">Logout</a>
</body>
</html>

logout.php :
<?php

session_start();

if(isset($_SESSION['user_id']))
{
unset($_SESSION['user_id']);

header("Location: login.php");
die;

connection.php :
<?php

$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "expt_6";

if(!$con = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname))
{

die("failed to connect!");
}
?>

functions.php :
<?php

function check_login($con)
{

if(isset($_SESSION['user_id']))
{

$id = $_SESSION['user_id'];
$query = "select * from signup where user_id = '$id' limit 1";

$result = mysqli_query($con,$query);
if($result && mysqli_num_rows($result) > 0)
{

$user_data = mysqli_fetch_assoc($result);
return $user_data;
}
}

//redirect to login
header("Location: login.php");
die;

}
function random_num($length)
{

$text = "";
if($length < 5)
{
$length = 5;
}

$len = rand(4,$length);

for ($i=0; $i < $len; $i++) {


# code...

$text .= rand(0,9);
}

return $text;
}
?>

OUTPUT :
After clicking on Logout it redirects to login page for new user to log in.
CONCLUSION :
In this experiment, we made a simple login and registration system using PHP and
MySQL.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy