Page 1 of 2

i want to create this thing but i have no clue how to start

Posted: Tue Jan 17, 2006 7:38 pm
by soul814
I want to create something where special users have special privilleges. For example a guest is only allowed to download one file a day, how do I keep track of it so that the user cant do things like clear his cookie or end the session to by past this, if you tell me a way to keep track of it, I can write the code myself. Thanks

Posted: Tue Jan 17, 2006 7:57 pm
by raghavan20
you cannot allow a guest to download.
You can only keep a count on members usage of files.
You cannot rely on IP to prohibit members.
Allow members to register and have a table where you store member id and number of downloads, keep updating it and apply necessary business logic.

Posted: Tue Jan 17, 2006 9:02 pm
by soul814
How would I like reset it daily, like say I control it to one download a day through a database how would I reset it the next day.

Posted: Tue Jan 17, 2006 9:07 pm
by Todd_Z
You could have a cron job that runs at midnight everyday.

Or you could have a wrapper class for downloading a file - if there has been a download that day, then it returns false [as in denied the right to download].

Heres something to start you off

Code: Select all

create table downloads ( 
  filename varchar(64),
  member int(10) unsigned,
  timestamp datetime,
  id int(30) unsigned primary key auto_increment 
);

Posted: Tue Jan 17, 2006 9:08 pm
by soul814
i'll have to research on cron jobs =)

the time right does it take off their pc or the hosting server's time?

Posted: Tue Jan 17, 2006 9:12 pm
by Todd_Z
server.

crontab -l
crontab -e

are the commands you will be using

Posted: Tue Jan 17, 2006 9:15 pm
by soul814
So wait if I don't own a server I won't be able to do anything about it right? I buy hosting right now and it seems a cron wouldnt work cause I need like direct access to the server

Posted: Tue Jan 17, 2006 9:16 pm
by Todd_Z
Often the host has a gui for stuff like that...

cpanel
vdeck

sound familiar?

Posted: Tue Jan 17, 2006 9:18 pm
by soul814
ahah you are right =) i so didnt know that, lol i see it in my cpanel thats cool =) lol now I guess I'll have to do a little planning and sort out all my details and start coding it lol

Posted: Tue Jan 17, 2006 9:38 pm
by Todd_Z
nah - no planning - once you have the databases, just throw objects at your keyboard and the code should be the output of the mayhem. good luck!

Re: i want to create this thing but i have no clue how to st

Posted: Tue Jan 17, 2006 10:59 pm
by Christopher
soul814 wrote:I want to create something where special users have special privilleges. For example a guest is only allowed to download one file a day, how do I keep track of it so that the user cant do things like clear his cookie or end the session to by past this, if you tell me a way to keep track of it, I can write the code myself. Thanks
You need to write some code for a user account system. It is a fairly large subject and there is probably plenty of code in this forum if you search. You will need an log-in page for authentication and some code to do Access Control on the pages that you want to have "special privilleges" on. Then you can start on something for limiting downloads to a certain number per timeframe. The easiest would be to just add a column to the user account database table that held the date/time of the last download. Then you can only allow access to downloads if it has been a day since the date of the last download in the user's account. Each time the user does a download you update the last download date in their user record.

Posted: Wed Jan 18, 2006 12:07 am
by josh
Using cron for this is silly,

although cron jobs would work here it is really overkill.

You need a datetime field in your table and you just store the timestamp of the last download (this is on your users table), when they go to download again you check if the current value returned by time() is greater then the value in the database multiplied by how many seconds in a day (3600*24), if so they haven't downloaded in the past 24 hours. If you want to actually reset it every night at midnight one could argue cron is better but I'd disagree, you can still work something out with the php time functions

main reason for avoiding cron here is portability (your code will run anywhere timestamps are available) and if that cron fails to run for whatever reason (server reboots when it is set to run or something) you're restricting your users from downloading for periods longer then one day

Posted: Wed Jan 18, 2006 2:23 am
by raghavan20
I do not understand why you are bringing cron,,,this is such easy
example table

Date downloads MemberId
18/01 2 5
19/01 1 5

Lets say you want to check whether member with id 5 has reached his limits. Let's say the max download is two per day. First check for
select downloads from table_name where MemberId = $memberId and Date = date(sysdate());

if no rows returned create a row,
insert into table_name values(date(sysdate), 1, $memberId);

If already an entry exists, update row
update table_name set downloads = downloads + 1

Now just before you update make sure that that downloads limit is not exceeded

Posted: Wed Jan 18, 2006 5:34 am
by soul814
Yes, I would like to reset it at midnight but checking the time with each click makes very good sense.

I'm thinking of doing it by bandwidth/files depending on the membership. Shouldn't be hard, two tables one for bandwidth used one for the # of files dled, same table. I'll write up a simple login script today, I rather code myself without stealing code, it takes the fun out of things.

Posted: Wed Jan 18, 2006 6:13 am
by Jenk
jshpro2 has hit the button, use a time() comparison to check for a 24hour period between downloads..

Code: Select all

function allowDownload ($userid)
{
    $sql = "SELECT `lastDownload` FROM `users` WHERE `userid` = '{$userid}'";
    $result = mysql_query($sql);
    $row = mysql_fetch_assoc($result);

    if ((intval($row['lastDownload']) + 86400) > time()) {
         //user has downloaded a file within the last 24hrs..
        return false;
    } else {
        return true;
    }
}
Just remember to update the lastDownload field when the user does download.