You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
170 lines
5.5 KiB
170 lines
5.5 KiB
6 months ago
|
<?php
|
||
|
session_start();
|
||
|
|
||
|
// Check if the user is logged in
|
||
|
if (isset($_POST['loginBtn'])) {
|
||
|
// Database connection
|
||
|
$conn = new mysqli('localhost', 'root', '', 'user'); // Replace with your database credentials
|
||
|
|
||
|
if ($conn->connect_error) {
|
||
|
die("Connection failed: " . $conn->connect_error);
|
||
|
}
|
||
|
|
||
|
if (!empty($_POST['email']) && !empty($_POST['password'])) {
|
||
|
$email = mysqli_real_escape_string($conn, $_POST['email']);
|
||
|
$password = mysqli_real_escape_string($conn, $_POST['password']);
|
||
|
|
||
|
// Check user in registration table
|
||
|
$userCheck = mysqli_query($conn, "SELECT * FROM registration WHERE email='$email' AND password='$password'");
|
||
|
|
||
|
if ($userCheck && mysqli_num_rows($userCheck) > 0) {
|
||
|
$_SESSION['loggedInUser'] = $email;
|
||
|
$_SESSION['show_message'] = 'Logged In Successfully';
|
||
|
|
||
|
// Check if user exists in streak table
|
||
|
$streakCheck = mysqli_query($conn, "SELECT * FROM user_streak WHERE email='$email'");
|
||
|
|
||
|
$currentDate = date("Y-m-d");
|
||
|
if (mysqli_num_rows($streakCheck) > 0) {
|
||
|
// User exists, update streak
|
||
|
$streakRow = mysqli_fetch_assoc($streakCheck);
|
||
|
$lastLoginDate = $streakRow['login_date'];
|
||
|
$streak = $streakRow['streak'];
|
||
|
|
||
|
// Check if the user logged in on consecutive days
|
||
|
$lastLoginTimestamp = strtotime($lastLoginDate);
|
||
|
$currentTimestamp = strtotime($currentDate);
|
||
|
|
||
|
$dateDiff = ($currentTimestamp - $lastLoginTimestamp) / (60 * 60 * 24); // Difference in days
|
||
|
|
||
|
if ($dateDiff == 1) {
|
||
|
// User logged in consecutive days, increment streak
|
||
|
$newStreak = $streak + 1;
|
||
|
} elseif ($dateDiff > 1) {
|
||
|
// User missed a day, reset streak
|
||
|
$newStreak = 1;
|
||
|
} else {
|
||
|
// User logged in today again, no change to streak
|
||
|
$newStreak = $streak;
|
||
|
}
|
||
|
|
||
|
// Update streak and last login date
|
||
|
$updateStreak = "UPDATE user_streak SET streak='$newStreak', login_date='$currentDate' WHERE email='$email'";
|
||
|
|
||
|
if (mysqli_query($conn, $updateStreak)) {
|
||
|
echo "Streak updated!";
|
||
|
} else {
|
||
|
echo "Error updating streak: " . mysqli_error($conn);
|
||
|
}
|
||
|
} else {
|
||
|
// User doesn't exist, insert new streak record
|
||
|
$insertStreak = "INSERT INTO user_streak (email, streak, login_date) VALUES ('$email', 1, '$currentDate')";
|
||
|
if (mysqli_query($conn, $insertStreak)) {
|
||
|
echo "New streak started!";
|
||
|
} else {
|
||
|
echo "Error inserting streak: " . mysqli_error($conn);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Redirect to homepage after successful login
|
||
|
header('Location: homepage.php');
|
||
|
} else {
|
||
|
$_SESSION['show_message'] = 'Invalid Email or Password';
|
||
|
}
|
||
|
} else {
|
||
|
$_SESSION['show_message'] = 'All fields are required';
|
||
|
}
|
||
|
}
|
||
|
?>
|
||
|
|
||
|
<!DOCTYPE html>
|
||
|
<html lang="en">
|
||
|
<head>
|
||
|
<meta charset="UTF-8">
|
||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
|
<meta name="description" content="Login to your account.">
|
||
|
<title>Login Form</title>
|
||
|
<style>
|
||
|
html, body {
|
||
|
font-family: Arial, Helvetica, sans-serif;
|
||
|
}
|
||
|
.text-center {
|
||
|
text-align: center;
|
||
|
}
|
||
|
.login-card {
|
||
|
display: block;
|
||
|
margin: 30px auto 0px auto;
|
||
|
width: 30%;
|
||
|
border: 1px solid #ccc;
|
||
|
box-shadow: 3px 3px 9px 3px #ddd;
|
||
|
padding: 20px;
|
||
|
}
|
||
|
.input-box {
|
||
|
width: 95%;
|
||
|
margin-top: 10px;
|
||
|
margin-bottom: 20px;
|
||
|
}
|
||
|
.input-box input {
|
||
|
width: 100%;
|
||
|
display: block;
|
||
|
border: 1px solid #777;
|
||
|
border-radius: 4px;
|
||
|
padding: 8px 8px;
|
||
|
}
|
||
|
.submit-btn {
|
||
|
width: 100%;
|
||
|
padding: 10px;
|
||
|
background-color: blue;
|
||
|
color: #fff;
|
||
|
outline: 0;
|
||
|
border-radius: 6px;
|
||
|
margin-bottom: 10px;
|
||
|
cursor: pointer;
|
||
|
}
|
||
|
.alert {
|
||
|
background-color: skyblue;
|
||
|
padding: 14px 20px;
|
||
|
color: #fff;
|
||
|
}
|
||
|
</style>
|
||
|
</head>
|
||
|
<body>
|
||
|
|
||
|
<div>
|
||
|
<div class="login-card">
|
||
|
<?php
|
||
|
if (isset($_SESSION['show_message'])) {
|
||
|
echo '<h4 class="alert">' . $_SESSION['show_message'] . '</h4>';
|
||
|
unset($_SESSION['show_message']);
|
||
|
}
|
||
|
?>
|
||
|
|
||
|
<h2 class="text-center">Login Form</h2>
|
||
|
<hr>
|
||
|
<br/>
|
||
|
|
||
|
<form action="" method="POST">
|
||
|
<div class="input-box">
|
||
|
<label>Email Address</label>
|
||
|
<input type="email" name="email" />
|
||
|
</div>
|
||
|
<div class="input-box">
|
||
|
<label>Password</label>
|
||
|
<input type="password" name="password" />
|
||
|
</div>
|
||
|
<div>
|
||
|
<button name="loginBtn" class="submit-btn">Login Now</button>
|
||
|
</div>
|
||
|
<div class="text-center">
|
||
|
<br/>
|
||
|
<a href="register.html">Don't have an account? Register now</a>
|
||
|
<br/><br/>
|
||
|
<a href="forgot_password.html">Forgot Password?</a>
|
||
|
</div>
|
||
|
</form>
|
||
|
</div>
|
||
|
</div>
|
||
|
|
||
|
</body>
|
||
|
</html>
|