Implementing trial period for local PHP application - php

I recently finished my PHP app and i would like to know if there's a possibility to have a kind of trial period. So that when that period is reached an error appears on the screen.
Thanks for your time and answers. I most appreciate them.

You can encrypt your sources using Zend Encoder or Ioncube, or obfuscate them.
But the fact is that everything from above can be patched. So if target user is experienced enough - they could remove your trial period checks.

Yes you can: I did the same when the need arose some time ago. Its simple and you can improve it.
Follow this algorithm;
Get today's date
Set the trial period
Add the trial period to today( as start date) to get the expiry date
Check to see if the above code have been executed before
If not then send startdate and `expierydate` into a table in mysql
Else get the start date and compare with today
If it matches displ
Here is the code.
function timebomb(){
$today = date("d-M-Y",time());
$trialPeriod = 1;
$startDate = date("d-M-Y", time());
$getExpiryDate = strtotime('+'.$trialPeriod."days", strtotime($startDate));
$expiryDate = date("d-M-Y", $getExpiryDate);
$checkStatus = mysql_num_rows(mysql_query("SELECT * FROM timebomb"));
if($checkStatus == 0){
mysql_query("INSERT INTO timebomb(StartDate,ExpiryDate) values
('$startDate','$expiryDate')") or die(mysql_error());
}else{
$getPeriod = mysql_query("SELECT * FROM timebomb");
WHILE($period = mysql_fetch_object($getPeriod)){
$endOfTrial = $period->ExpiryDate;
}
IF($endOfTrial == $today){`enter code here`
echo
<center><font size='5' color='red'>
PLEASE YOUR TRIAL PERIOD IS OVER.
IF YOU ENJOYED USING THIS PRODUCT, <br/>
CONTACT ALBERT (0205173224) FOR THE FULL VERSION.
THANK YOU."
;
exit();
}
}
}
timebomb();

One solution can be the using some compiled (or byte compiled ) form of your application instead of source files. checkout this links.
http://php.net/manual/en/book.bcompiler.php, http://www.phpcompiler.org

Follow below steps for fully secured trial period in php projects...
1. create a table in your database for trial period
ex. trial(id,reg_date,days_of_trial).
2. In your index page or login page whatever it is,add code logic as below
i.get 'reg_date','days_of_trial' from 'trial' table.<br/>
ii.get today's date.<br/>
iii.calculate difference using as $dDiff= date_diff($reg_date,$today); <br/>
iv.now compare difference with 'days_of_trial'.<br/><br/>
ex. <br/>
if($dDiff->days>=$row['days_of_trial'])<br/>
{ <br/>
v.update trial table set days_of_trial=0.<br/><br/>
//redirect to expired page..<br/><br/>
ex.
header("Location:http://localhost/project_dir/expired/index.php");}.
3. in expired/index.php file remove your projects main directories as..<br/><br/>
ex.
<br/>
if(file_exists("../folder1"))<br/>
rmdir("../folder1");
redirect to expired page wherein you can display "Trial expired message"<br/>
ex.
<br/>
header("Location: http://localhost/proj_dir/expired/expired.html");<br/><br/>
4. now replace your original home page code to redirect to expired page..
ex.
$file = fopen("../index.php","w");<br/>
fwrite($file,"<?php header('Location: http://localhost/proj_dir/expired/');?>");<br/>
fclose($file);<br/>
Don't forget to take backups of project directory and database..
Your php project is fully secured as in trial period .....

Related

Limit the access of a function to once every 24 hours PHP + MySQL

I would like to limit the access of a function i've created to once every 24 hour based on the users IP address. I would also like the PHP script to delete the MySQL record if it's older than 24 hours.
If the user already has used the function within 24 hours, show them a message and prevent the script from continue running.
If the user already has used the function but 24 hours has passed since he used the function, delete the MySQL record and let the script continue running.
I'm lost and as you can see i am also missing some statements for deleting old records (-24 hours)..
Could anyone provide me with an example of how to do this? Thanks
Get client's IP address and store it with current date and time if the record doesn't exist.
Fetch the record and add 24 hours to its date and time value and check it with the current date and time every time the script is executed.
You need if else conditional statements to check if the 24 hours time is over or not. Based on that, you will control the execution of the function you want to.
I think I don't want to write much of theory. Here, I've written the pattern what the code looks like:
if(!$record_in_db) {
// create record with the client's ip address and the current date and time
// invoke the function you want - This is the code to trigger the function first time for the new IP address
} else {
// fetch_record_from_db
// add 24 hours to its date and time value
// check it with current date and time
$record_date_time = new DateTime('22-12-2016 22:45:20'); // this value should be fetched from database
$record_date_time_by_24_hours = $record_date_time->modify('+1 day');
$current_date_time = new DateTime(date('d-m-Y H:i:s', strtotime('now')));
$date_time_diff = $current_date_time->diff($record_date_time_by_24_hours);
if($date_time_diff->invert == 0) {
// Do something
} else {
// update the date and time of the record to NOW which is current date and time
// invoke the function you want
}
}
I can't write you the whole code. I could only give you some hints. You need to build the code from it. Hope I've given you right hints that could help you.

PHP Timezone adjust as per user

Im creating a UI where users get to choose their timezone. My backend is PHP and I know you can do it a few ways but I had questions about all of them and which way is the best:
http://ca2.php.net/date_default_timezone_set
//where America/Los_Angeles will be changed based on user
date_default_timezone_set('America/Los_Angeles');
Does this change the server timezone for ever user or just what is being exported? is there any performance issues with this?
What if it was a function like this...?
date_default_timezone_set('America/Los_Angeles'); -08:00 set in php.ini
$date = "2014-01-01 12:00:00";
$new_time_zone = "America/New_York" // -05:00 = +3 hours difference
function adjust_time_zone($string, $zone){
$datetime = new DateTime($string);
$new_zone = new DateTimeZone($zone);
$datetime->setTimezone($new_zone);
return ($datetime->format('Y-m-d H:i:s'));
}
adjust_time_zone($date, $new_time_zone);
Results(i tested this):
2014-01-01 15:00:00 //3 hours ahead.
in this format everything stamped in the system would be on LA time and exported would be the change...
I know there is allot of these Timezone threads but there seems to be allot of "i do it like this" and not any solid "this way blows my socks off="
Thanks for any help you can give!
I store everything in the db as a datetime and UTC timezone.
I store the user's timezone in the db ($timezone).
So when a time ($time) is taken from the db, I run it through the following function:
function changetimefromUTC($time, $timezone) {
$changetime = new DateTime($time, new DateTimeZone('UTC'));
$changetime->setTimezone(new DateTimeZone($timezone));
return $changetime->format('m/d/y h:i a');
}
This returns the time in the user's timezone, formatted.
edit: you can see the db table of timezones in this thread:
Generating a drop down list of timezones with PHP
Well it looks like what you have should work for you. This is an alternate method, although not necessarily better. You can just use PHP's mktime and pass the hour offset to it.
<?php
print "<br>Date: ".date("Y-m-d H:i:s");
// DEFINE AN OFFSET
$offset = -5; // TIMEZONE OFFSET: '-5' FOR NEW YORK
// ADD THE OFFSET TO THE CURRENT TIME
print "<br>Date Offset: ".$date_offset = date("Y-m-d H:i:s", mktime(date("H") + $offset, date("i"), date("s"), date("m"), date("d"), date("Y")));
I found a great solution to the problem - and to adapt the date and time settings on my page according to the users settings.
Ready build js code
Some developers that had the exact same problem build a javascript solution to the code. You don't need to know javascript as long as you follow these instructions.
Go to https://bitbucket.org/pellepim/jstimezonedetect/src/default/
Read through the readme file if you want to
Either download the script as an NPM package or download it manually by pressing "downloads" in the left menu.
If you pressed downloads to manually download the script press "Download repository" to get the files.
Create a js folder - named "js" in your project. (optional)
Unzip the jstimezonedetect files into the js folder. I chose to name the unziped file "jstimezonedetect".
To reach the files above my current directory is /js/jstimezonedetect/.
In your header code (important it is in header since the time-zone will affect the full code on the rest of your page unless you run a function.
Enter the following code to your php file:
echo '
<script src="js/jstimezonedetect/dist/jstz.min.js"></script>
<script>
document.cookie = "usertimezone="+jstz.determine().name();
</script>
';
$settimezone = $_COOKIE['usertimezone'];
date_default_timezone_set($settimezone);
The first line of script will import the jstz.min.js file from the importat jstimezonedetect project.
Second line will create a cookie (you can later delete the cookie if you want to).
In the third line I get the cookie into the php script and then I use PHP date_default_timezone_set();
(https://www.php.net/manual/en/function.date-default-timezone-set.php)
to set the timezone.
Worked perfectly for me. :) Ask if something is unclear.

PHP MySQL Negative Value (Balance) Issue

I am developing a desktop software where it charge user per execution the main action. For example say it will charge user 0.1$ for per PDF print.
and my software provide multithreading. .
so, if it run single thread it works fine :)
but the problem is if user run multiple thread at one (say 10/20 threads)
it (php) also continues user to allow the server/execution even balance get below zero..
though my php script check whether balance is positive ..
but after user run multiple threads balance become like -5.95$ or -25.75$ etc
and that is a big security/financial issue..
here is the code I am using:
<?php
$strSQL = "Select * from users where Email = '$strUser'";
$return = mysql_query($strSQL, $strDBConn);
$strDBData = mysql_fetch_array($return, MYSQL_ASSOC);
//checking balance
$strBalance = $strDBData['Balance'];
if($strBalance < 0)
{
// if balance 0 then exit so, my software/thread will not process further
mysql_close($strDBConn);
exit('Balance Exceed');
}
//rest of the codes that realted to service executaion
// code that substract the balnce
$dblCost = 0.25;
$strSQL = "Update users set Balance = Balance - '$dblCost' where Email = '$strUser'";
$return = mysql_query($strSQL, $strDBConn);
//rest finising codes
?>
any help/suggestion would be highly appreciated..
thanks in advance.
best regards
I think, this is a quite similar question:
What is equivalent of the C# lock statement in PHP?
First, try to switch away from the old "mysql" to somethin new, maybe some PDO like DB access ;).
Then, for getting around with multi-thread in php, it can be a good idea, to write a file for every userid (!) and lock this file, when there's a request. When file is locked in another thread, wait for x seconds for the file to be unlocked by the locker-thread. If it is not unlocked within time, something went wrong. When in locked-thread all went good, unlock the file after every operation needed.
Theoraticaly you will be good with then till there's a multi-thread soloution in PHP ;)

Destroy session in PHP

Hi guys I am working on a program which will execute the number of people visit a website and when the date changes it will start from 0. So I have nearly figure out how to do it but it doesn't appear as 0 when the date changes here is my code:
<?php
session_start();
?>
<?php
if(isset($_SESSION['views']))
$_SESSION['views']=$_SESSION['views']+1;
else
$_SESSION['views']=1;
echo "You are the ". $_SESSION['views'] ." Visitor";
?>
As #Zwirbelbart said, don't use sessions for solving this. Use a DB, or at least a file, where you'll store the number of visitors.
Something like this:
function incrementVisitorsCount() {
$currentDay=date("Ymd");
if(!isset$_SESSION["visited"] || $_SESSION["visited"] != $currentDay) {
incrementYourDailyCounter($currentDay);
$_SESSION["visited"]=$currentDay;
}
}
incrementYourDailyCounter being the function that will increment the relevant value in the storage you chose (I would suggest a table in a DB you're most certainly already using).
You can base your counter on IP instead of sessions, but it means that you keep a record of each IP that visited your website each day.

Display content based on date with PHP

I'm putting together a contest site built on Wordpress. Legally, the contest has to start at midnight. To avoid being up at midnight to set up the content, i'd like to build some PHP logic to show everything after the start date, and before then display some basic HTML for a splash page. I'm a TOTAL programming newb, but i'm trying to work the problem out, here's what I have so far:
<?php
// The current date
$date = date('Y, j, m');
// Static contest start date
$contestStart = ('2012, 02, 03');
// If current date is after contest start
if ($date > $contestStart) {
//contest content?
}
else {
// splash content?
}
?>
I feel like there is smarter ways to do this. My site is fairly large... wrapping the whole thing in an if statement seems ridiculous. Maybe a redirect to a different page altogether based on the date?
Any help is appreciated.
You will want to modify your WordPress theme so that the changes can be site-wide.
Log in to your WordPress installation.
Navigate to Appearance > Editor
In the Templates section, go to header.php
At the very beginning of header.php, insert your code :
<?php
$date = time();
$contestStart = strtotime('2012-02-03 00:00:00');
if ($date < $contestStart) {
?>
<html>
Insert your whole splash page here.
</html>
<?php
exit;
}
?>
//The normal template code should be below here.
Don't forget to click the "Update File" button on WordPress when you're done; and like the others said, make sure that your specified time is synced with whatever timezone the server is in.
I suppose technically your code's logic would work but yes, there are much better ways of doing this. However, for your purpose we will go simple.
You should be comparing against a timestamp and not a string. Try this:
<?php
// The current date
$date = time();
// Static contest start date
$contestStart = strtotime('2012-02-03 00:00:00');
// If current date is after contest start
if ($date > $contestStart) {
//contest content?
}
else {
// splash content?
}
?>
You can put the splash and content components in separate .php files, and simply include() then within the conditionals.
if ($date > $contestStart) {
include("SECRETDIR/contest.php");
}
else {
include("SECRETDIR/splash.php");
}
You'll want to set up an .htaccess so that people cannot point their browsers towards secretdir and directly access contest.php
You don't have to wrap the content, you can have something like:
<?php
if ( time() < strtotime('2012-02-03 00:00:00') ) {
echo "not yet!";
exit;
}
//code here wont be executed
Be careful of timezones! Your server might be in a different timezone than your content.
// setup the start date (in a nice legible form) using any
// of the formats found on the following page:
// http://www.php.net/manual/en/datetime.formats.date.php
$contestStart = "February 1, 2012";
// check if current time is before the specified date
if (time() < strftime($contestStart)){
// Display splash screen
}
// date has already passed
else {
// Display page
}

Categories