Simple user registration page
Student db
create a student database.
Open your XAMPP control panel, start the Apache and MySQL
servers, and then navigate to phpMyAdmin in your web browser.
Click on "New" to create a new database.
Enter a name for your database, for example,
"student_db."
Choose "utf8_general_ci" as the collation.
Click "Create" to create the database.
Once you've created the database, and we move on to the PHP
code for the signup and login pages.
*************************************************************************************
<?php
// Establish a connection to the database
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "student_db";
$conn = new mysqli($servername, $username, $password,
$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Process signup form
if ($_SERVER["REQUEST_METHOD"] ==
"POST") {
$username =
$_POST["username"];
$password =
$_POST["password"];
// Hash the
password for security
$hashed_password =
password_hash($password, PASSWORD_DEFAULT);
// Insert user
data into the database
$sql =
"INSERT INTO users (username, password) VALUES ('$username',
'$hashed_password')";
if ($conn->query($sql)
=== TRUE) {
echo
"Signup successful!";
} else {
echo
"Error: " . $sql . "<br>" . $conn->error;
}
}
$conn->close();
?>
we have a table named "users" with columns
"id" (auto-increment), "username," and "password"
in your "student_db" database.
Now create login page with “login.php”
<?php
// Same connection code as signup.php
// Process login form
if ($_SERVER["REQUEST_METHOD"] ==
"POST") {
$username =
$_POST["username"];
$password =
$_POST["password"];
// Retrieve hashed
password from the database
$sql =
"SELECT password FROM users WHERE username = '$username'";
$result =
$conn->query($sql);
if
($result->num_rows > 0) {
$row =
$result->fetch_assoc();
$hashed_password = $row["password"];
// Verify the
entered password with the hashed password
if
(password_verify($password, $hashed_password)) {
echo
"Login successful!";
} else {
echo
"Incorrect password.";
}
} else {
echo
"User not found.";
}
}
$conn->close();
?>
Comments
Post a Comment