John Davidson

The trial period system along with subscription - PHP and MySQL

0 comments
Message:


I built a trial period system using PHP and Mysql and it is working completely fine. But what I'm trying to achieve is that it should automatically update the time period, once the user buys the subscription. For example, if a user signs up for 30 days subscription, then it should increase by 30 days.


I'm not expecting someone to write the code for me. Just want to know how it could be done. Because this is the first time I'm building such a thing. Need your guidance on it!


and I know code is vulnerable to SQL Injection. You can ignore it because this is just for testing purpose


<?php 

session_start();

include 'db-connection.php';

$user_email = mysqli_real_escape_string($connection, $_POST['user_email']);
$user_pass = mysqli_real_escape_string($connection, $_POST['user_pass']);

$select_user = "select * from users where user_email='$user_email'";
$select_user_query = mysqli_query($connection, $select_user);
$user_email_count = mysqli_num_rows($select_user_query);

if($user_email_count) {
$user_result_array = mysqli_fetch_array($select_user_query);
$user_db_pass = $user_result_array['user_pass'];
$user_id = $user_result_array['user_id'];
$user_name = $user_result_array['user_name'];



if($user_pass === $user_db_pass ) {
$_SESSION['user_id'] = $user_id;
$_SESSION['user_name'] = $user_name;

$user_registration_date = new DateTime($user_result_array['user_registration']); // This fetch the user registration date from database
$today_date = new DateTime();
$trial_period = 7;
$date_diff = date_diff($user_registration_date,$today_date);
$date_difference = $date_diff->format('%d days');

if($date_difference >= $trial_period){
echo "Trial period is over";
} else {
echo "<span class='alert alert-success'>Login Successful</span>";
}

} else {
echo "<span class='alert alert-danger'>You have entered wrong password</span>";
}

} else {
echo "<span class='alert alert-danger'>No Record Found</span>";
}

?>

Once the trial period is over, it would redirect the user to the trial.php page where the user would be asked to do the payment.


Once the payment is made, it should increase the time period (this is the step which I don't know how to achieve)


User Table


User_id | user_registration | user_name | user_email | user_pass | user_phone



My Answer:
To automatically update the time period once a user buys a subscription, you can follow these steps:

1. Create a column in your user table to store the subscription end date. This column will be updated whenever a user buys a subscription.

2. When a user buys a subscription, calculate the new end date based on the current end date (if any) and the duration of the new subscription. For example, if the current end date is '2022-12-31' and the user buys a 30-day subscription, the new end date will be '2023-01-30'.

3. Update the subscription end date in the user table with the new end date.

4. Whenever a user logs in, check the subscription end date. If the current date is greater than the subscription end date, it means the subscription has expired. You can then prompt the user to renew their subscription.

5. You can also set up a cron job to check for expired subscriptions periodically and take appropriate actions (e.g., send a notification to the user, disable access to premium features, etc.).

By following these steps, you can ensure that the trial period system automatically updates the time period when a user buys a subscription. Let me know if you need further clarification or assistance.

Rate this post

3 of 5 based on 4230 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us