News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

Time sensitive access

Started by Ardenn, June 04, 2004, 12:39:17 PM

Previous topic - Next topic

Ardenn

Its the first time I have ever attempted to program a time sensative access to a part of my site.

Basically what I have is a subscription service, where the subscription last for 30 days.

So I figured I could store the results of the time function (time();  ) in the database when the user signed up for the subscription.  So lets say the field is called 'member_signup_time'.
So then I figure that there are 86400 seconds in a day so I can multiply that by 30 and get a value to compare.  Therefore I came up with this script ( please assume that I already have openned my database and table already :


$currenttime=time();
$signuptime=$row['member_signup_time'];
$plus30days = 2592000;  // 86400*30
$timecheck=$signuptime + $plus30days;

IF ($currenttime <= $timecheck)

{
echo 'IT WORKS';
}
ELSE
{
ECHO 'IT DOESNT WORK';
}


Now I think that this should work.  The logic says, if the current time is less than or equal to the timecheck value then print "IT WORKS" or on my website, give the user access to the website utility, ELSE  it doesnt work and on my website it denies access.

However, when I try to use this script I always get the reply "IT DOESNT WORK" meaning that there is something wrong with my logic.    I know subscription service coding is used alot.  Can anyone suggest a better way to code this or a different way to code it?

Ardenn // Traxxus
http://www.twinwand.com
D&D Play by Post Community Looking for Players and DM's

Jack.R.Abbit™

Looking at the script, you never assign anything to $time before you use it in your IF.

But, when ever I run into issues like this where a logic check is not acting the way I think it should... the first thing I do is just print out all the variables I'm working with.  That should tell you if they are what you think they are.  At least then you know what is causing the trouble... that makes it easier to figure out why it causing trouble.


Ardenn

It was a typo on my part .. I have modded the script.. ran into the same problem.  I did that too.  I echoed all the variables before the if statement in my test script and they all look like I think they should.
Ardenn // Traxxus
http://www.twinwand.com
D&D Play by Post Community Looking for Players and DM's

Christian Land

<?php

// $timestamp = date when subscription started
// $valid = number of days the subscription is valid

function isExpired($timestamp=0,$valid=0)
{
if (time() <= ($timestamp + ($valid*86400)))
return false;
return true;
}

// demo-code

$signuptime time()-2593000; // set a date where subscription is expired

if (isExpired($signuptime,30))
{
echo 'is expired';
}
else
{
echo 'is NOT expired';
}

echo '<br />';

$signuptime time()-2000000; // set a date where subscription is NOT expired

if (isExpired($signuptime,30))
{
echo 'is expired';
}
else
{
echo 'is NOT expired';
}

?>

Ardenn

Im not even going to tell you how long it took me to understand your script.  It is however exactly what I was looking for.  This time thing messes with my mind HARD!

Thank you for posting this!
Ardenn // Traxxus
http://www.twinwand.com
D&D Play by Post Community Looking for Players and DM's

Christian Land


Ardenn

Ok so here is my script.  Its pretty big, so Im going to cut it down a little bit.

Im not sure why its not working.



GLOBAL $ID_MEMBER;

function isExpired($timestamp=0,$valid=0)
{
if (time() <= ($timestamp + ($valid*86400)))
    return true;
    return false;   
}

$results= mysql_query("SELECT * FROM wf_employers WHERE ID_MEMBER = '$ID_MEMBER'");
$row=mysql_fetch_array($results);
$jobpostNumber=$row['jobpostNumber'];
$MG=$row['productID'];
$maxpost=$row['maximum_post'];
$signuptime=$row['member_signup_time'];

IF (isExpired($signuptime,30) AND $MG === EmployerUnlimited OR $jobpostNumber < $maxpost OR $MG === Administrator)
{


ECHO ' A FORM GOES HERE';

}
ELSE
{

echo ' <div align = "center"><B>YOU HAVE EXCEEDED YOUR MAXIMUM JOB POST!<BR/>OR YOUR SUBSCRIPTION HAS ENDED</b><br/><a href="index.php?page=Signup"> CLICK HERE TO BUY MORE ADS </A></div>';   

}



Now the value that is the key to this whole thing is $signuptime which is created when the users register and is stored in a database.  When I change the value of $signuptime in the database to be a date that is expired it doesnt work which is good.  However, When I change it to a date like today, it should work, however its not, it still gives the expired message.
Ardenn // Traxxus
http://www.twinwand.com
D&D Play by Post Community Looking for Players and DM's

Christian Land

Uhmm...  OK.... ;D

first, I would put

error_reporting(E_ALL); // Report ALL Warnings/Errors

at the beginning of your code...

I guess it will report something like:

Notice: Use of undefined constant EmployerUnlimited - assumed 'EmployerUnlimited' in xxxxxx on line xxx


... or did you define EmployerUnlimited and Administrator as constants? If not, you'll probably mean 'Administrator' and 'EmployerUnlimited' (the ' are missing)

Another problem could be missing brackets in the IF-Statement... I don't know what you wanna check there, but its always a good idea to use some brackets... so instead of something like:

if ($some_var === false AND $some_other_var == 'xxx' OR $user_is_admin === true)

you should write something like:

if (($some_var === false AND $some_other_var == 'xxx') OR ($user_is_admin === true))

... i hope this gives you some ideas ...

Ardenn

Thank you for all your help SnowCrash.  I found the problem, it was indeed with the "IF" Statement.

IF (isExpired($signuptime,30) AND $MG === EmployerUnlimited OR [color=Red]$jobpostNumber < $maxpost[/color] OR $MG === Administrator)

You see your expiration function was working great, the problem was that the $jobpost logic was openning the gate anyway.  To clarify, I created this script for my client with 3 services in mind.  unlimited entries in database for 30 days, 15 entries in database (no time limit) and 1 entry in database no time limit.    The  jobpost part of the if statement is for the 15 entries and 1 entries products, but without specifying a $MG value for the Jobpost logic like I did for the 30 day logic it was letting everything through.

You helped me so much I thought you might like to know what went haywire.  Thank you again for your help!
Ardenn // Traxxus
http://www.twinwand.com
D&D Play by Post Community Looking for Players and DM's

Davy-D

Hi Ardenn,

I am looking for a solution exactly to what you have developed. Is there a way to obtain your script with documentation?

Thanks
Davy

Advertisement: