0% found this document useful (0 votes)
5 views13 pages

REGISTRATION

The document contains PHP code for a registration and login system for a job portal, including database connection, user input validation, and password hashing. It features HTML forms for user registration and login, with appropriate alerts for errors and redirection based on account type. Additionally, it provides SQL commands to create a 'users' table in the database to store user information.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views13 pages

REGISTRATION

The document contains PHP code for a registration and login system for a job portal, including database connection, user input validation, and password hashing. It features HTML forms for user registration and login, with appropriate alerts for errors and redirection based on account type. Additionally, it provides SQL commands to create a 'users' table in the database to store user information.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

REGISTRATION

<?php

// Database connection

$conn = new mysqli('localhost', 'root', '', 'job_portal'); // Update


'job_portal' with your actual database name

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

$username = trim($_POST['username']);

$phone_number = trim($_POST['phone_number']);

$account_type = trim($_POST['account_type']);

$password = trim($_POST['password']);

// Basic validation

if (empty($username) || empty($phone_number) || empty($account_type)


|| empty($password)) {

echo "<script>alert('Please fill out all fields


correctly.');</script>";

exit;

// Validate phone number (e.g., length and format)

if (!preg_match('/^\d{10}$/', $phone_number)) {

echo "<script>alert('Please enter a valid 10-digit phone


number.');</script>";

exit;

}
// Hash the password securely

$hashed_password = password_hash($password, PASSWORD_BCRYPT);

// Check if the username or phone number already exists

$checkStmt = $conn->prepare("SELECT * FROM users WHERE username = ?


OR phone_number = ?");

$checkStmt->bind_param("ss", $username, $phone_number);

$checkStmt->execute();

$result = $checkStmt->get_result();

if ($result->num_rows > 0) {

echo "<script>alert('Username or phone number already exists.


Please try again.');</script>";

$checkStmt->close();

exit;

// Insert into the database

$stmt = $conn->prepare("INSERT INTO users (username, phone_number,


account_type, password) VALUES (?, ?, ?, ?)");

$stmt->bind_param("ssss", $username, $phone_number, $account_type,


$hashed_password);

if ($stmt->execute()) {

// Redirect based on account type

if ($account_type === 'admin') {

header("Location: dashboard2.php"); // Admin dashboard

} else {
header("Location: dashboard.php"); // Employee/employer
dashboard

exit;

} else {

echo "<script>alert('Error saving data. Please try


again.');</script>";

$stmt->close();

$conn->close();

?>

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-


scale=1.0">

<title>Registration</title>

<!-- Bootstrap CSS -->

<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min
.css" rel="stylesheet">

<!-- Bootstrap Icons -->

<link
href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-
icons.css" rel="stylesheet">
<style>

body {

background-color: #f8f9fa;

.card {

box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);

border-radius: 10px;

button {

font-weight: bold;

.bi-cloud {

font-size: 3rem;

color: orange;

</style>

</head>

<body>

<div class="container d-flex justify-content-center align-items-


center" style="height: 100vh;">

<div class="card p-4" style="width: 400px;">

<div class="text-center mb-4">

<i class="bi bi-cloud"></i>

<h3>Register</h3>

</div>

<form action="" method="POST">

<div class="mb-3">
<label for="username"
class="form-label">Username</label>

<input type="text" class="form-control" id="username"


name="username" placeholder="Enter your username" required>

</div>

<div class="mb-3">

<label for="phone_number" class="form-label">Phone


Number</label>

<input type="text" class="form-control"


id="phone_number" name="phone_number" placeholder="Enter your phone
number" required>

</div>

<div class="mb-3">

<label for="account_type" class="form-label">Account


Type</label>

<select class="form-select" id="account_type"


name="account_type" required>

<option value="" disabled selected>Select your


account type</option>

<option value="admin">Admin</option>

<option value="employee">Employee</option>

<option value="employer">Employer</option>

</select>

</div>

<div class="mb-3">

<label for="password"
class="form-label">Password</label>

<input type="password" class="form-control"


id="password" name="password" placeholder="Enter your password" required>

</div>

<button type="submit" class="btn btn-warning w-


100">Register Now</button>
<hr>

<p>Already a member? <a href="login.php" class="text-


decoration-none">Login here</a></p>

</form>

</div>

</div>

<!-- Bootstrap JS Bundle -->

<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundl
e.min.js"></script>

</body>

</html>

LOGIN

<?php

// Database connection

$conn = new mysqli('localhost', 'root', '', 'job_portal'); // Update


'job_portal' with your actual database name

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

$username = trim($_POST['username']);

$account_type = trim($_POST['account_type']);

$password = trim($_POST['password']);
// Basic validation

if (empty($username) || empty($account_type) || empty($password)) {

echo "<script>alert('Please fill out all fields


correctly.');</script>";

} else {

// Check user credentials

$stmt = $conn->prepare("SELECT password FROM users WHERE BINARY


username = ? AND BINARY account_type = ?");

if (!$stmt) {

die("Error preparing statement: " . $conn->error);

$stmt->bind_param("ss", $username, $account_type);

$stmt->execute();

$stmt->store_result();

if ($stmt->num_rows > 0) {

$stmt->bind_result($hashed_password);

$stmt->fetch();

// Verify password

if (password_verify($password, $hashed_password)) {

// Redirect based on account type

if ($account_type === 'admin') {

header("Location: dashboard2.php");

} elseif ($account_type === 'employee') {

header("Location: dashboard.php");

} elseif ($account_type === 'employer') {


header("Location: employer_dashboard.php");

exit;

} else {

echo "<script>alert('Incorrect password. Please try


again.');</script>";

} else {

echo "<script>alert('User not found or account type


mismatch.');</script>";

$stmt->close();

$conn->close();

?>

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-


scale=1.0">

<title>Login</title>

<!-- Bootstrap CSS -->


<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min
.css" rel="stylesheet">

<!-- Font Awesome Icons -->

<link
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/
css/all.min.css" rel="stylesheet">

<style>

body {

background: #f8f9fa;

font-family: 'Arial', sans-serif;

.login-card {

background-color: white;

width: 100%;

max-width: 400px;

border-radius: 10px;

box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);

padding: 20px;

.login-card h3 {

font-weight: bold;

.input-group-text {

background-color: #feb47b;

color: white;
}

.btn-primary {

background-color: rgb(175, 127, 117);

border-color: rgb(243, 137, 110);

.btn-primary:hover {

background-color: rgb(248, 186, 145);

a {

color: #ff7e5f;

a:hover {

text-decoration: underline;

</style>

</head>

<body>

<div class="container d-flex justify-content-center align-items-


center vh-100">

<div class="login-card text-center">

<div class="mb-4">

<i class="fas fa-user-circle fa-4x"></i>

</div>
<h3 class="mb-4">Login</h3>

<form action="" method="POST">

<!-- Username Field -->

<div class="mb-3 input-group">

<span class="input-group-text"><i class="fas fa-


user"></i></span>

<input type="text" class="form-control"


name="username" placeholder="Username" required>

</div>

<!-- Select Account Type -->

<div class="mb-3 input-group">

<span class="input-group-text"><i class="fas fa-


list"></i></span>

<select class="form-select" name="account_type"


required>

<option value="" disabled selected>Select Account


Type</option>

<option value="admin">Admin</option>

<option value="employee">Employee</option>

<option value="employer">Employer</option>

</select>

</div>

<!-- Password Field -->

<div class="mb-3 input-group">

<span class="input-group-text"><i class="fas fa-


lock"></i></span>

<input type="password" class="form-control"


name="password" placeholder="Password" required>
</div>

<!-- Login Button -->

<button type="submit" class="btn btn-primary w-100 mb-


3">Login Now</button>

</form>

<hr>

<p>Not a member? <a href="signup.php" class="text-decoration-


none">Create an account</a></p>

</div>

</div>

<!-- Bootstrap JS Bundle -->

<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundl
e.min.js"></script>

</body>

</html>

Database check

CREATE TABLE users (

id INT AUTO_INCREMENT PRIMARY KEY,

username VARCHAR(100) NOT NULL,

phone_number VARCHAR(15) NOT NULL UNIQUE,

account_type ENUM('admin', 'employee', 'employer') NOT NULL,


password VARCHAR(255) NOT NULL,

created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP

);

Or this

CREATE TABLE users (

id INT AUTO_INCREMENT PRIMARY KEY,

username VARCHAR(100) NOT NULL,

phone_number VARCHAR(15) NOT NULL UNIQUE,

account_type ENUM('admin', 'employee', 'employer') NOT NULL,

password VARCHAR(255) NOT NULL,

created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP

);

You might also like