Bman900 Posted June 24, 2009 Share Posted June 24, 2009 So lets say I want to make a site where people can buy memberships for 30 days or for a whole year. I have a user system but I have no clue on how to do this. I was thinking after the payment is made I can enter a code in the database that get checked when the user is logged in. The problem that I am running into is how to I change that code in the database after 30 days or 12 months after they signed up so they wont have a subscription any more? Quote Link to comment https://forums.phpfreaks.com/topic/163448-paid-subscription/ Share on other sites More sharing options...
MadTechie Posted June 24, 2009 Share Posted June 24, 2009 Just store the date they paid up to, so time()+(30*24*60*60) for 30days Or the date they paid, and the type of account (30 days / 1 year / etc) Quote Link to comment https://forums.phpfreaks.com/topic/163448-paid-subscription/#findComment-862383 Share on other sites More sharing options...
pkedpker Posted June 24, 2009 Share Posted June 24, 2009 Just store the date they paid up to, so time()+(30*24*60*60) for 30days Or the date they paid, and the type of account (30 days / 1 year / etc) yah or to save the cpu power of processing all those multiplication cpu instructions just type in google 30 days to seconds which gives you 2592000 time()+2592000; = 30 days Quote Link to comment https://forums.phpfreaks.com/topic/163448-paid-subscription/#findComment-862385 Share on other sites More sharing options...
Bman900 Posted June 24, 2009 Author Share Posted June 24, 2009 Alright, so I store the date till they paid for in a column but how would I do a check if they still have access to their subscription and than drop the value after the time is up so they can order again? Quote Link to comment https://forums.phpfreaks.com/topic/163448-paid-subscription/#findComment-862390 Share on other sites More sharing options...
MadTechie Posted June 24, 2009 Share Posted June 24, 2009 You don't need to drop any values, if the paid for a subscription to until 06/24/2010 then you just need to compare to the current date ie SELECT * FROM payements WHERE paidto >= CURDATE(); Personally I find it quicker to do (30*24*60*60), its quicker to debug and update vs microseconds to process! infact i just ran a benchmark, 5000 iterations it seams sometimes mine is quicker!, which is kinda unexpected! 0.00064196524620056 //pkedpker 0.00064111461639404 //mine 0.00065427861213684 //pkedpker 0.00064351334571838 //mine 0.00067055644989014 //pkedpker 0.00067531914710999 //mine 0.00056252417564392 //pkedpker 0.0005777334690094 //mine 0.00063268013000488 //pkedpker 0.00069262042045593 //mine Quote Link to comment https://forums.phpfreaks.com/topic/163448-paid-subscription/#findComment-862393 Share on other sites More sharing options...
pkedpker Posted June 24, 2009 Share Posted June 24, 2009 haha like a few microseconds in the cpu really matter.. but yah i guess its nicer doing it your way of course.. .. maybe even make a static constant with that value and called it THIRTY_DAYS but then accessing the constant may be slower then typing the magic number lol so either way its sucks Quote Link to comment https://forums.phpfreaks.com/topic/163448-paid-subscription/#findComment-862395 Share on other sites More sharing options...
Bman900 Posted June 24, 2009 Author Share Posted June 24, 2009 You don't need to drop any values, if the paid for a subscription to until 06/24/2010 then you just need to compare to the current date ie SELECT * FROM payements WHERE paidto >= CURDATE(); Correct me if am wrong. I will put that in an query and run it. Then it should return a value if there is something in there. If not that obviously they don't have a subscription or it was expired. So after the query I can run an if (something got returend) { do thid} else { you don't have a subscription} Quote Link to comment https://forums.phpfreaks.com/topic/163448-paid-subscription/#findComment-862400 Share on other sites More sharing options...
MadTechie Posted June 24, 2009 Share Posted June 24, 2009 Correct me if am wrong. I will put that in an query and run it. Then it should return a value if there is something in there. If not that obviously they don't have a subscription or it was expired. So after the query I can run an if (something got returend) { do thid} else { you don't have a subscription} if the paidto is set in the future (from when they paid) (ie they have paid up to that date) then anyone found has a valid membership maybe even make a static constant with that value and called it THIRTY_DAYS wouldn't that use a few extra bytes of memory!! LOL Its all down to personal preference, I guess! but I try to keep in mind what makes upgrades quicker and safer Quote Link to comment https://forums.phpfreaks.com/topic/163448-paid-subscription/#findComment-862401 Share on other sites More sharing options...
pkedpker Posted June 24, 2009 Share Posted June 24, 2009 It'll give everyone who still has a subscription not 1 person.. so I have no idea how that will help you.. too much you can either check every name then while the loop is going or you can just check right away in the query WHERE name='username' or id='userId' AND paidto >= CUTDATE(); yah you can do a if statement with the results from mysql_num_rows() @MadTechie: lol yah its all personal preference but not really that big of a deal to remember since you know NOW() always returns seconds and anything that big has to be alot! P.S> you think you could look at the TIMESTAMP question of mines I still don't know which function is better and since you are good at benchmarking all I care about is execution speed Quote Link to comment https://forums.phpfreaks.com/topic/163448-paid-subscription/#findComment-862402 Share on other sites More sharing options...
Bman900 Posted June 24, 2009 Author Share Posted June 24, 2009 Ye that is exactly what I was going to do. And I was going to get more specific with that query where it checks for one user like u just said above. Quote Link to comment https://forums.phpfreaks.com/topic/163448-paid-subscription/#findComment-862406 Share on other sites More sharing options...
Andy-H Posted June 24, 2009 Share Posted June 24, 2009 /* ###Check user is registered/logged in ###Process Payment */ // If payment valid // Subscription length stored in $subscriptionLength if ( $subscriptionLength == "30 days" ) { $subscriptionLength = time() + (( 3600 * 24 ) * 30); } elseif ($subscriptionLength == "1 year" ) { $subscriptionLength = time() + (( 3600 * 24 ) * 365); } $query = "UPDATE userTable SET subscription = " . $subscriptionLegth . " WHERE userField = '" . mysql_real_escape_string($userSession) . "' LIMIT 1"; $result = mysql_query($query); // or handle error if ($result) { Header("Location: subscriptionSuccess.htm"); } else { Header("Location: subscriptionFail.htm"); } // else give error /* ### Check subscription valid */ $query = "SELECT subscription FROM userTable WHERE userField = '" . mysql_real_escape_string($userSession) . "' LIMIT 1"; $result = mysql_query($query); // or handle error $row = mysql_fetch_row($result); $subscriptionLeft = $row[0] - time(); if ($subscriptionLeft <= 0) { Header("Location: expired.php?act=Renew"); } /* ###Include this file at top of each page where subscription is required after session_start() and db connection */ Could do something like that? Quote Link to comment https://forums.phpfreaks.com/topic/163448-paid-subscription/#findComment-862409 Share on other sites More sharing options...
pkedpker Posted June 24, 2009 Share Posted June 24, 2009 Yah andy thats nice but it always strikes me odd.. how people use LIMIT 1.. after that wouldn't it give 1 result either way?.. not like your using a LIKE command which gets things that have similar names.. like if you do a query for 'And' it will return Andy, Andrew. etc.. other then that whats the point? of using LIMIT 1 (correct me if im wrong i'm still a noobie at php only been using it for a week now lol) Quote Link to comment https://forums.phpfreaks.com/topic/163448-paid-subscription/#findComment-862411 Share on other sites More sharing options...
Andy-H Posted June 24, 2009 Share Posted June 24, 2009 Because the registered username/id should be unique, in effect, the query SHOULD only select one field; so if there is an error in the registration script (or something) and somehow two entries of the same name have "somehow" got in there, it wont update or select the user field twice and lose subscription money. It would probably never happen but, who knows... Quote Link to comment https://forums.phpfreaks.com/topic/163448-paid-subscription/#findComment-862412 Share on other sites More sharing options...
MadTechie Posted June 24, 2009 Share Posted June 24, 2009 The other reason for using LIMIT 1 is to stop SQL searching for more after its found one, and considering only 1 should be found this helps with performance Quote Link to comment https://forums.phpfreaks.com/topic/163448-paid-subscription/#findComment-862420 Share on other sites More sharing options...
Andy-H Posted June 24, 2009 Share Posted June 24, 2009 Never knew that, as the saying goes; you learn something new every day. =D Anyhow, off to bed for me or else I won't be learning anything in college today. lol Quote Link to comment https://forums.phpfreaks.com/topic/163448-paid-subscription/#findComment-862422 Share on other sites More sharing options...
pkedpker Posted June 24, 2009 Share Posted June 24, 2009 yup thanks Mad I care about performance. Quote Link to comment https://forums.phpfreaks.com/topic/163448-paid-subscription/#findComment-862457 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.