CREATE DATABASE user_login;
USE user_login;
CREATE TABLE users (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL
);
<?php
session_start();
if (isset($_POST['submit'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$conn = mysqli_connect('localhost', 'username', 'password', 'user_login');
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) == 1) {
$_SESSION['username'] = $username;
header('Location: home.php');
} else {
echo "Invalid username or password";
}
}
?>
<form method="POST" action="">
<label>Username:</label>
<input type="text" name="username" required><br>
<label>Password:</label>
<input type="password" name="password" required><br>
<input type="submit" name="submit" value="Login">
</form>
<?php
session_start();
if (!isset($_SESSION['username'])) {
header('Location: login.php');
}
$conn = mysqli_connect('localhost', 'username', 'password', 'user_login');
$username = $_SESSION['username'];
$query = "SELECT * FROM users WHERE username='$username'";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);
if (isset($_POST['change_password'])) {
$new_password = $_POST['new_password'];
$confirm_password = $_POST['confirm_password'];
if ($new_password == $confirm_password) {
$query = "UPDATE users SET password='$new_password' WHERE username='$username'";
mysqli_query($conn, $query);
echo "Password updated successfully";
} else {
echo "New password and confirm password do not match";
}
}
if (isset($_POST['update_profile'])) {
$email = $_POST['email'];
$query = "UPDATE users SET email='$email' WHERE username='$username'";
mysqli_query($conn, $query);
echo "Profile updated successfully";
}
?>
<h1>Welcome <?php echo $row['username']; ?></h1>
<h3>Profile Information</h3>
<ul>
<li>Username: <?php echo $row['username']; ?></li>
<li>Email: <?php echo $row['email']; ?></li>
</ul>
<h3>Change Password</h3>
<form method="POST" action="">
<label>New Password:</label>
<input type="password" name="new_password" required><br>
<label>Confirm Password:</label>
<input type="password" name="confirm_password" required><br>
<input type="submit" name="change_password" value="Change Password">
</form>
<h3>Update Profile</h3>
<form method="POST" action="">
<label>Email:</label>
<input type="email" name="email" value="<?php echo $row['email']; ?>" required><br>
<input type="submit" name="update_profile" value="Update Profile">
</form>
<a href="logout.php">Logout</a>
<?php
session_start();
session_destroy();
header('Location: login.php');
?>
how about the current password what is the codeSure, here's an example code for creating a multi-user login system with change password and update profile features using PHP and MySQL:
1. Creating the Database
First, let's create a MySQL database that will store user information such as username, password, and email. We will call this database "user_login" and it will have a table called "users" with the following fields:
- id (int, primary key, auto-increment)
- username (varchar(50))
- password (varchar(50))
- email (varchar(50))
Here's the SQL code to create the database and table:
Code:CREATE DATABASE user_login; USE user_login; CREATE TABLE users ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, username VARCHAR(50) NOT NULL, password VARCHAR(50) NOT NULL, email VARCHAR(50) NOT NULL );
2. Creating the Login Page
Next, let's create a login page where users can enter their username and password to login. We will use PHP to check if the username and password match the ones stored in the database. If they do, we will set a session variable to indicate that the user is logged in.
Code:<?php session_start(); if (isset($_POST['submit'])) { $username = $_POST['username']; $password = $_POST['password']; $conn = mysqli_connect('localhost', 'username', 'password', 'user_login'); $query = "SELECT * FROM users WHERE username='$username' AND password='$password'"; $result = mysqli_query($conn, $query); if (mysqli_num_rows($result) == 1) { $_SESSION['username'] = $username; header('Location: home.php'); } else { echo "Invalid username or password"; } } ?> <form method="POST" action=""> <label>Username:</label> <input type="text" name="username" required><br> <label>Password:</label> <input type="password" name="password" required><br> <input type="submit" name="submit" value="Login"> </form>
3. Creating the Home Page
Once the user is logged in, they will be redirected to the home page where they can see their profile information and update their password. We will use PHP to display the user's information and allow them to update their password.
Code:<?php session_start(); if (!isset($_SESSION['username'])) { header('Location: login.php'); } $conn = mysqli_connect('localhost', 'username', 'password', 'user_login'); $username = $_SESSION['username']; $query = "SELECT * FROM users WHERE username='$username'"; $result = mysqli_query($conn, $query); $row = mysqli_fetch_assoc($result); if (isset($_POST['change_password'])) { $new_password = $_POST['new_password']; $confirm_password = $_POST['confirm_password']; if ($new_password == $confirm_password) { $query = "UPDATE users SET password='$new_password' WHERE username='$username'"; mysqli_query($conn, $query); echo "Password updated successfully"; } else { echo "New password and confirm password do not match"; } } if (isset($_POST['update_profile'])) { $email = $_POST['email']; $query = "UPDATE users SET email='$email' WHERE username='$username'"; mysqli_query($conn, $query); echo "Profile updated successfully"; } ?> <h1>Welcome <?php echo $row['username']; ?></h1> <h3>Profile Information</h3> <ul> <li>Username: <?php echo $row['username']; ?></li> <li>Email: <?php echo $row['email']; ?></li> </ul> <h3>Change Password</h3> <form method="POST" action=""> <label>New Password:</label> <input type="password" name="new_password" required><br> <label>Confirm Password:</label> <input type="password" name="confirm_password" required><br> <input type="submit" name="change_password" value="Change Password"> </form> <h3>Update Profile</h3> <form method="POST" action=""> <label>Email:</label> <input type="email" name="email" value="<?php echo $row['email']; ?>" required><br> <input type="submit" name="update_profile" value="Update Profile"> </form> <a href="logout.php">Logout</a>
4. Creating the Logout Page
Finally, let's create a logout page where we will destroy the session and redirect the user back to the login page.
Code:<?php session_start(); session_destroy(); header('Location: login.php'); ?>
That's it! With these files, you should have a basic multi-user login system with change password and update profile features using PHP and MySQL.
To allow the user to change their password, we need to add a field for the "current password" in the "change password" form. Here's an updated code for the "change password" section of the home page:how about the current password what is the code
<?php
session_start();
if (!isset($_SESSION['username'])) {
header('Location: login.php');
}
$conn = mysqli_connect('localhost', 'username', 'password', 'user_login');
$username = $_SESSION['username'];
$query = "SELECT * FROM users WHERE username='$username'";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);
if (isset($_POST['change_password'])) {
$current_password = $_POST['current_password'];
$new_password = $_POST['new_password'];
$confirm_password = $_POST['confirm_password'];
if ($current_password == $row['password']) {
if ($new_password == $confirm_password) {
$query = "UPDATE users SET password='$new_password' WHERE username='$username'";
mysqli_query($conn, $query);
echo "Password updated successfully";
} else {
echo "New password and confirm password do not match";
}
} else {
echo "Current password is incorrect";
}
}
if (isset($_POST['update_profile'])) {
$email = $_POST['email'];
$query = "UPDATE users SET email='$email' WHERE username='$username'";
mysqli_query($conn, $query);
echo "Profile updated successfully";
}
?>
<h1>Welcome <?php echo $row['username']; ?></h1>
<h3>Profile Information</h3>
<ul>
<li>Username: <?php echo $row['username']; ?></li>
<li>Email: <?php echo $row['email']; ?></li>
</ul>
<h3>Change Password</h3>
<form method="POST" action="">
<label>Current Password:</label>
<input type="password" name="current_password" required><br>
<label>New Password:</label>
<input type="password" name="new_password" required><br>
<label>Confirm Password:</label>
<input type="password" name="confirm_password" required><br>
<input type="submit" name="change_password" value="Change Password">
</form>
<h3>Update Profile</h3>
<form method="POST" action="">
<label>Email:</label>
<input type="email" name="email" value="<?php echo $row['email']; ?>" required><br>
<input type="submit" name="update_profile" value="Update Profile">
</form>
<a href="logout.php">Logout</a>
create a code home.php separate to change password and update profileTo allow the user to change their password, we need to add a field for the "current password" in the "change password" form. Here's an updated code for the "change password" section of the home page:
Code:<?php session_start(); if (!isset($_SESSION['username'])) { header('Location: login.php'); } $conn = mysqli_connect('localhost', 'username', 'password', 'user_login'); $username = $_SESSION['username']; $query = "SELECT * FROM users WHERE username='$username'"; $result = mysqli_query($conn, $query); $row = mysqli_fetch_assoc($result); if (isset($_POST['change_password'])) { $current_password = $_POST['current_password']; $new_password = $_POST['new_password']; $confirm_password = $_POST['confirm_password']; if ($current_password == $row['password']) { if ($new_password == $confirm_password) { $query = "UPDATE users SET password='$new_password' WHERE username='$username'"; mysqli_query($conn, $query); echo "Password updated successfully"; } else { echo "New password and confirm password do not match"; } } else { echo "Current password is incorrect"; } } if (isset($_POST['update_profile'])) { $email = $_POST['email']; $query = "UPDATE users SET email='$email' WHERE username='$username'"; mysqli_query($conn, $query); echo "Profile updated successfully"; } ?> <h1>Welcome <?php echo $row['username']; ?></h1> <h3>Profile Information</h3> <ul> <li>Username: <?php echo $row['username']; ?></li> <li>Email: <?php echo $row['email']; ?></li> </ul> <h3>Change Password</h3> <form method="POST" action=""> <label>Current Password:</label> <input type="password" name="current_password" required><br> <label>New Password:</label> <input type="password" name="new_password" required><br> <label>Confirm Password:</label> <input type="password" name="confirm_password" required><br> <input type="submit" name="change_password" value="Change Password"> </form> <h3>Update Profile</h3> <form method="POST" action=""> <label>Email:</label> <input type="email" name="email" value="<?php echo $row['email']; ?>" required><br> <input type="submit" name="update_profile" value="Update Profile"> </form> <a href="logout.php">Logout</a>
In the above code, we added a new input field for "current password" in the "change password" form. We then check if the current password matches the one stored in the database. If it does, we proceed to check if the new password and confirm password match before updating the password in the database. If the current password does not match, we display an error message.
Sure, here's an updated code that separates the "change password" and "update profile" forms into separate pages:create index.php to seperate change pass word and update profile
<?php
session_start();
if (!isset($_SESSION['username'])) {
header('Location: login.php');
}
$conn = mysqli_connect('localhost', 'username', 'password', 'user_login');
$username = $_SESSION['username'];
$query = "SELECT * FROM users WHERE username='$username'";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);
?>
<h1>Welcome <?php echo $row['username']; ?></h1>
<h3>Profile Information</h3>
<ul>
<li>Username: <?php echo $row['username']; ?></li>
<li>Email: <?php echo $row['email']; ?></li>
</ul>
<a href="change_password.php">Change Password</a><br>
<a href="update_profile.php">Update Profile</a><br>
<a href="logout.php">Logout</a>
<?php
session_start();
if (!isset($_SESSION['username'])) {
header('Location: login.php');
}
$conn = mysqli_connect('localhost', 'username', 'password', 'user_login');
$username = $_SESSION['username'];
$query = "SELECT * FROM users WHERE username='$username'";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);
if (isset($_POST['change_password'])) {
$current_password = $_POST['current_password'];
$new_password = $_POST['new_password'];
$confirm_password = $_POST['confirm_password'];
if ($current_password == $row['password']) {
if ($new_password == $confirm_password) {
$query = "UPDATE users SET password='$new_password' WHERE username='$username'";
mysqli_query($conn, $query);
echo "Password updated successfully";
} else {
echo "New password and confirm password do not match";
}
} else {
echo "Current password is incorrect";
}
}
?>
<h3>Change Password</h3>
<form method="POST" action="">
<label>Current Password:</label>
<input type="password" name="current_password" required><br>
<label>New Password:</label>
<input type="password" name="
Warning: Trying to access array offset on value of type null on line 13Sure, here's an updated code that separates the "change password" and "update profile" forms into separate pages:
index.php:
Code:<?php session_start(); if (!isset($_SESSION['username'])) { header('Location: login.php'); } $conn = mysqli_connect('localhost', 'username', 'password', 'user_login'); $username = $_SESSION['username']; $query = "SELECT * FROM users WHERE username='$username'"; $result = mysqli_query($conn, $query); $row = mysqli_fetch_assoc($result); ?> <h1>Welcome <?php echo $row['username']; ?></h1> <h3>Profile Information</h3> <ul> <li>Username: <?php echo $row['username']; ?></li> <li>Email: <?php echo $row['email']; ?></li> </ul> <a href="change_password.php">Change Password</a><br> <a href="update_profile.php">Update Profile</a><br> <a href="logout.php">Logout</a>
change_password.php:
Code:<?php session_start(); if (!isset($_SESSION['username'])) { header('Location: login.php'); } $conn = mysqli_connect('localhost', 'username', 'password', 'user_login'); $username = $_SESSION['username']; $query = "SELECT * FROM users WHERE username='$username'"; $result = mysqli_query($conn, $query); $row = mysqli_fetch_assoc($result); if (isset($_POST['change_password'])) { $current_password = $_POST['current_password']; $new_password = $_POST['new_password']; $confirm_password = $_POST['confirm_password']; if ($current_password == $row['password']) { if ($new_password == $confirm_password) { $query = "UPDATE users SET password='$new_password' WHERE username='$username'"; mysqli_query($conn, $query); echo "Password updated successfully"; } else { echo "New password and confirm password do not match"; } } else { echo "Current password is incorrect"; } } ?> <h3>Change Password</h3> <form method="POST" action=""> <label>Current Password:</label> <input type="password" name="current_password" required><br> <label>New Password:</label> <input type="password" name="
<?php
session_start();
if (isset($_POST['submit'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$conn = mysqli_connect('localhost', 'root', '', 'user_login');
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_array($result); {
if ($row["user_type"] == "user") {
$_SESSION["username"] = $username;
header("location:indexuser.php");
} elseif ($row["user_type"] == "admin") {
$_SESSION["username"] = $username;
header("location:index.php");
} else {
echo "<script>alert('email or password incorrect');</script>";
}
}
}
?>
<form method="POST" action="">
<label>Username:</label>
<input type="text" name="username" required><br>
<label>Password:</label>
<input type="password" name="password" required><br>
<input type="submit" name="submit" value="Login">
</form>