Forum Moderators: coopster

Message Too Old, No Replies

Need direction for major updating script

         

dkin

5:59 pm on May 9, 2005 (gmt 0)

10+ Year Member



What I am trying to do is, I would like to create a dynamic page that my users can send to friends, when a friend clicks the link on the page I want the user to be rewarded, but I would only like each "friend" to be able to click the link once per 24 hours. Now cookies would work but can easily be deleted, so I would like to use IP Addresses, I know this will somewhat restrict who clicks the link but I would rather that then having people clearing cookies and reclicking.

So my question is how can I record the time username and IP Address? Will I have to create another table in my db and if so how will this update and clear after 24 hours?

Any help would be appreciated.

kazecoder

9:46 pm on May 9, 2005 (gmt 0)

10+ Year Member



Using IP's will definitely work better however don't forget also that a simple reboot of the computer in most cases will cause the ISP to distribute a new IP unless they use static.

As far as grabbing time and IP:

<?php

$today = date("F j, Y);

$ip = $_SERVER['REMOTE_ADDR'];

**Notes**

I used the date format F j, Y just as an example but visit here to see all available formats: [us4.php.net...]

The IP is automatically stored in a global variable so all you have to do is call on it like I did above.

As far as the username...it all depends on how your links they are clicking on are setup. I would recommend doing a URL encode and passing it to the same page. Ex. page.php?username=bob.

Questions?

kazecoder

9:48 pm on May 9, 2005 (gmt 0)

10+ Year Member



Oh...I forgot about the other table...

Yes you would probably have to put that data in another table and use SQL to do some kind of date comparison and flush it after 24hrs.

ironik

10:07 pm on May 9, 2005 (gmt 0)

10+ Year Member



Since you may only be allowing users to send emails to friends you can store a temporary mail table of times emails have been sent:

user_id
time

You could probably store the email they are sending to as well to prevent more than one user from emailing the same person (don't want to flood one person with requests).

After your 24 hours is up then allow the user to send more, and perhaps regularly flush the table of old entries.

dkin

7:27 am on May 10, 2005 (gmt 0)

10+ Year Member



So does anyone know what I record to ensure 100% that the user is not clicking the link more than once every 24 hours?

grandpa

8:08 am on May 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You can require a confirmation e-mail. The user will need to reply to an auto-responder e-mail in order to send the link to their friend. The auto-responder will link to your script. Now you have the users IP, e-mail address, date/time and friends e-mail address.

I might assign a random code to the user at this time, and send that code in the message to the friend. When it gets returned your user gets credit.

The friend would not be able to trigger extra credits to the user.

So does anyone know what I record to ensure 100% that the user is not clicking the link more than once every 24 hours?
If you have the users data stored in a table perform a time check. If within 24 hours - no go. If not in the table add a record and go. If older than 24 hours, delete the old record, write a new one and go. Now you have a self-cleaning table too.

dkin

4:24 pm on May 10, 2005 (gmt 0)

10+ Year Member



I would rather not have to do this with emails, I would just like a link for the user to click, when he does I write to the db and reward the "user" then the person that clicks the link is not allowed to click again for 24 hours.

dkin

7:15 pm on May 10, 2005 (gmt 0)

10+ Year Member



any other suggestions?

ironik

10:07 pm on May 10, 2005 (gmt 0)

10+ Year Member



If you want to restrict it once every 24 hours, then add an extra column in your users table for the clicked time. Everytime the link is clicked, check the last updated time, then (if valid) update the field with the current time. I would normally use a unix time stamp:

e.g.


$q = mysql_query('SELECT user_email_time FROM user_table WHERE user_id=\'' . $user_id . '\'');
$user = mysql_fetch_array($q);

$currentTime = time();

$timeDifference = $currentTime - $user['user_email_time'];

$oneDay = 24 * 60 * 60;

if ($timeDifference >= $oneDay)
{
// Allow an email to be sent and update user row
$updateQuery = mysql_query('UPDATE user_table SET user_email_time=\'' . time() .'\' WHERE user_id=\'' . $user_id . '\'');
// [... mailing code here ...]
} else {
// Not allowed to send another email
}

You could modify that into a temporary email table as suggested before and allow the user to send multiple emails, but restrict them from sending to the same address more than once, or go with this example and allow them to only send one email per day.

dkin

11:50 pm on May 10, 2005 (gmt 0)

10+ Year Member



I think you have the wrong idea, I dont want the users link to only be able to be clicked once every 24 hours, I want a visitor to be able to click it only once every 24 hours, so it can be click 2000 times a day, but it has to be 2000 different people clicking it.

Make sense?

ironik

1:04 am on May 11, 2005 (gmt 0)

10+ Year Member



Yep, that's why you have the time clicked stored 'per user'. Here's another example.

You script uses a session variable called $_SESSION['userid']. This holds the user id of the current person logged in. In a simplistic way, all you have to do is test if that variable is set, and if it is you can assume the user is logged in. You then use that variable to get information specific to that user and display it accordingly on your page.

Conceivably you could disable the link, hide the form and/or stop the script from processing the email all from fairly simple if else statements.

dkin

1:10 am on May 11, 2005 (gmt 0)

10+ Year Member



but I want people visiting my site for the first time to be able to click these links, whether they are a user or not.

ironik

2:28 am on May 11, 2005 (gmt 0)

10+ Year Member



Ah, now I feel stupid! Sorry mate ;)

You can take the same time concept and apply it to something like a client-side cookie. Cookies aren't fullproof though (people turn them off or delete them), maybe you could combine that with a temporary database table of IP and time, then you might at least control a larger percentage of visitors ability to click on that link twice.

With any method you choose though, you'll still be at the mercy of the user's technical knowledge, and inclination to abuse the application. Using the combination of cookie + IP + user agent you might be able to knock down the percentage people able to abuse the application sufficiently though.

Create a DB table with:

IP
click_time
user_agent

Then when a user clicks on the link for the first time:

check for an entry in the table for the IP (if yes, check if the user agents match, and also if the click time is less than 24 hours ago).

If they don't have an entry, check for a cookie set by your site (in which you should store an encrypted version of a timestamp). If there is a cookie, unencrypt the time and check if it is less than 24 hours.

If they don't have a cookie or a Database entry, or the time is less than 24 hours then allow the link to be used and then write a new cookie and insert/update the database entry.

As you can probably tell, it's going to get a little tedious trying to verify the user, and it's definitely not going to be fullproof... but it'll probably work for something like 98% of visitors, since most people probably aren't technically minded or malicious.

Look up the following in the php manual if you want to take the idea into practice and see what you come up with:

setCookie()
$_SERVER['HTTP_USER_AGENT']
$_SERVER['REMOTE_ADDR']

dkin

4:33 am on May 11, 2005 (gmt 0)

10+ Year Member



how can I check if click time is less than 24 hours ago?

dkin

3:33 pm on May 11, 2005 (gmt 0)

10+ Year Member



anyone?

jatar_k

4:01 pm on May 11, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



use ironik's method

compare last click time with now

dkin

4:03 pm on May 11, 2005 (gmt 0)

10+ Year Member



but thats my quesrion, I dont know how.

dkin

4:22 pm on May 11, 2005 (gmt 0)

10+ Year Member



and also how do I encrypt the date

jatar_k

4:35 pm on May 11, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



depends on how you store the date, easiest is to use a unix timestamp created by mktime then save that. That value is in seconds.

When they go to click again, grab the last clicked time from the db, use mktime again, minus then from now and compare that to the number of seconds in 24 hrs. If the remainder is greater then they get to click again.

encrypting the date, well you need to be able to undo it so don't use any one way encryption. I would say you just need a little obfuscation, mix it up so they don't know its a timestamp. You can do that however you want.

dkin

5:49 pm on May 11, 2005 (gmt 0)

10+ Year Member



Ok I have created/borrowed this script

$data1 = mktime (13,45,50,5,10,2005);

$h = date("G");
$m = date("i");
$s = date("s");
$mo = date("n");
$d = date("j");
$y = date("Y");

$data2 = mktime ($h,$m,$s,$mo,$d,$y);

$diff = ($data2 - $data1) / 86400;

echo $diff;

Now, if $data2 is more then 24 hours after $data1 it returns a number greater then 1, now, if it is less how would I convert this to hours minutes and seconds?

jatar_k

6:07 pm on May 11, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



86400 is the number of seconds in 24 hrs (60*60*24) so if you don't divide by 86400 you are left with seconds

if ($diff < 86400) echo 'it hasnt been 24 hrs yet';

you can take diff and divide it any way you want, you are left with seconds so you could turn it into minutes, hrs, whatever

ironik

11:32 pm on May 11, 2005 (gmt 0)

10+ Year Member



Obsfucation is probably all you really need for the date. The idea behind encrypting/obsfucating data is so that curious minds will find it difficult to interpret the information you store in the cookie, and thus being able to abuse it. A 10 digit number is a dead giveaway as a timestamp.

I don't know of any obsfucation scripts, but I know of a handy, lightweight 2-way encryption utility rc4crypt:

[sourceforge.net...]

All you'd have to do is instantiate the class and use it's encrypt() method when you store the date in the cookie. It's an extremely simple class to use.

Remember to unencrypt the data when you get it from the cookie and test that the unencrypted data matches your expected result (someone may have attempted to edit it).