Recent profile visitor and mailing system - php

Hi there I am creating a community website where if user A visit to profile of user B then it will notify user B that 1 person visited to your profile, if user is offline then it will send email notification that 1 person visit to your profile.
Problem: If I enter a log into database every time user visit to anothers profile... it will show wrong data.. for example if i visited to a user profile and just refresh page many times then it will log many times and i have to use grop by query which is waste of data. So should I use time to something? what will be best approach?
Second, how frequent I send email if user if offline. If i send once i enter log then in a day I am sending many mails to users. What should i follow to keep system spam free and effective?

First:
The data is not wrong! YOU must decide what you want to log! If you want to log only one visit from A to B per Day add a date column to your log and check if thee is already a row With matching "from", "to" and "date" values before you insert a new row.
This would however create two rows if A visits B at 23:58 and reloads at 00:01.
You can add a datetime column to your log and only insert a row if the timespan between two visits is greater then or equal to 24 hours.
Second:
Let your users choose how often they want to get a notification! You can use cronjobs and/or workers to send the notifications depending on the users settings. I would suggest using one worker to do all the mailing to avoid conflicts, but this highly depends on your server. If you can not set up a dedicated mailing thread you can use multiple cronjobs but then you will have to take into account that a user might change his settings, or get a visitor while your cronjob is still working.
IMHO Sending notification mails more than once a hour for profile visits is a reasons to block all mails from a site. Daily updates should be enough.

Related

How to count the number of times a non-registered User presses "Search", then save it in MySQL database and limit that number for every user?

So I'm working on a website which brings up movie/tv-show posters on clicking "search". Now I'm wondering how can I limit the search button for non-users to 10-15 searches and have unlimited searches for registered users. Think of it like a premium membership sorta deal where the people who pay, get the extra-clicks and people who're using the trial product, get only 10 clicks. What I want to ask is, how do I log the number of times a non-registered user uses the search engine and how do I put a cap on it? And remove that cap for registered users.So please let me know where I can start from.
Wow this awesome I usually find app that limits visitors but after they make free account then they reach the limit after that they should pay to have premium account without limits
But any way that can be achieved easy I will explain without codes cause it needs long run
First you should create function to retrieve users
IP address so you may use remote_addr or http_forward in case of you use proxy
Second go to database and create table called
Ip_users or whatever you need This table should have 2 column. Ip column and id_hash column now you should set ip_column as varchar and id_hash as init
Third is most easy step make Ajax script that take the ip value that we get above in first step and send it to .php script call it uservalid.php as example
Fourth. Uservalid.php should first select count of records for ip if it null it make insert for new ip if it more than zero it update ip_hash + 1 If ip_hash equals 10 it make redirect to account page
This why you need index on IP address column and this is general steps
Maybe free account as start with limits then premium account is better
cheking the number of clicks of each ip adress is not bad idea but it wont solve the problem beacause if the user restart the modem he ll get new ip adress.
in my opinion, you can make all users need an account to use the search option and do your check according to that, if the user want to search more than 10-15 searches he/she need to get premium account.

MySQL And PHPMyAdmin Events Scheduler

I've created an iOS and android app that uses a database where users can login which in my case "User_active" gets changes to 1 and logged out gets changed to 0. The only problem I have is that I cannot check to see if the user has left the app, or if they have closed it, so what I want to do is create an event on phpmyadmin to change the "User_active" to 0 after 30min, but I can't seam to get things right. I'm using Awardspace as my host and I've only seen tutorials for localhost. Errors that I'm getting are that my user account does not have the write privileges to create the events and I cannot find any user tabs to change user privileges. If anyone can send me off in the right direction that would be great or even better a solution.
phpMyAdmin is a fantastic interactive tool, but it is not the tool for this.
In some ways, this is a fairly common problem. Typical web based applications don't know when a user has left, they only know when a user does something. One method is to do the following:
1 - Add a field to the user table (the same table that has the "user_active" field) to track the last activity by that user of any type. This should be a DATETIME or TIMESTAMP field and updated always with the current time.
2 - Add a cronjob to check any records in the user table to see if they are (a) still logged in (user_active=1) and the timestamp is more than 30 minutes old. If so, set user_active to 0 - essentially force a logout after 30 minutes. You may want to have an additional field to indicate it was a forced automatic logout rather than a user-initiated logout.
If your hosting service allows cronjobs, then run this periodically (perhaps every 5 minutes) and you are done. If your hosting service doesn't allow cronjobs then one option is to add a call to the "automatic user logout" function as part of every regular page. That won't work if you have thousands of active users as the overhead would be too much (and the delay on each page display). But if you have a small number of active users it will get the job done - I have done plenty of system housekeeping using that method.

Need help setting up a logic behind a Facebook notification system style

I want build a notification system to my website, similar to Facebook. The notifications don't need to be in real time.
This is what I have in mind:
User create an event (upload a new photo, add a new comment, like a photo, or even an administration alert to all users)
Run a cronjob every 5 minutes to add the notifications into the notifications table: id|id_content|type_of_content_enum|affected_user_id|date|seen_bool
The cronjob, will run several functions for each type of notification, for example:
add_photos_notification() // This function will check all photos added in the past 5 minutes, and insert a row in the notification table, for each user following this person. The function will group all photos added into the past 5 minutes, so the follower don't get many notifications for the same type of content. Resulting in a notification like: User X added Y photos in his profile.
add_admin_notification() // This function will check all news added by the administration of the site in the past 5 minutes, and insert a row in the notification table, for each user on the system ...
Is this a correct approach to build a notification system?
Is it possible to miss an event, running a cron every 5 minutes where functions retrieve the past 5 minutes events?
To be safe, do you think an alternative could be checking all events where a field 'notified' is not true? The same function that will grab the events where 'notified' = false, update it to true after adding that notification into the notifications table.
Thanks,
I went with the cronjob route and working good so far. Since our system got so many users to be notified, I found it the most appropriate way to do for two reasons.
I don't need to edit my current scripts code, inserting functions to add notifications for every event I want notify.
Since there gonna be some actions where many users are affected, adding notifications in real time could result in long script delays and time outs.
I built a class called notifications and inside this class, there are functions to add notification for every event I want notify, for example: user_added_a_new_photo(); user_commented_on_a_photo();
For every notification generate, Im adding 1 entry per user to be notified. This is how my notifications db looks like:
id
affected_user_id //user being notified
user_generating_the_notification_id
content_type // enum containing all type of notifications my system has (example: photo, video, comment) ...
content_json // a json containing the notification content. Based on the content type, on the display view file, I call helpers that will format the notification row using the json info.
date // the date the notification was added
seen_on // the date the user saw the notification
clicked_on // if user clicked on the notification, store the date he clicked on it
display // true or false
For this purpose, I added the display field cause for every new notification I create, I check the database if the same user, has another not seen notification, from the same generating user. If this condition is true, I set the old notification to display = false, and group the two new notifications resulting in something like: User X added X new photos in his gallery.
The clicked_on field, stores the date the item was clicked so I can generate reports based on this info if I need to. When displaying the content, if this item is not null, I highlight the notification to mark those not checked yet.
I created a text field to store the notification content in json, cause different notifications has different styles to present the users. For example, a new comment notification, has only texts, but a new photo notification, has a preview thumb.
So far, no issue running it and working for my needs.
The only downside, since the cronjobs can only be run only every 1 minute, the notifications may have 1 minute delay. But since I don't need it in real time, I set the cronjob to run every 5 minutes.
I've been looking into something like this as well and i just found this question on stackoverflow
Building a notification system
Have a look at the answer, the user does shed a fair bit of light on the theory of how to implement this sort of system.
As far as i can see you would need to create a record or object for each notification, if 100 people are subscribe to that event then 100 records will be generated. When a page is loaded your system will find all the notifications that correspond to the user logged in (Maybe by record ID) and then you would notify the user of how many notifications they have.
Now another way of doing this (Note i haven't implemented any of these, they are just ideas) would be to make a notification in a a table, then we'd have a second table that will hold a users ID and a notification ID, a user will be added to this table when they are subscribed to a notification, for example, confirmation of a friend request.
Please note again, these aren't proven methods, they're a result of some of my research of the matter, i'd read the post i gave you before doing anything.

CakePHP: Single Post Per Day based on IP Address

I'm still getting the hang of CakePHP but I think I'm making progress.
Background: I'm developing an application that doesn't require registration. I essentially have posts that can be made by any visitor to the site.
Question: How can I limit the number of times a guest can post per day? Say I want to allow a guest to post once per day. Would I essentially create a users table based on IP, log a new IP address every time one visits the site, then limit actions based on that table?
Yes...that would do it but I wouldn't recommend it. A user can change their IP by using a proxy or just reconnecting to their ISP. So ..basically I wouldn't use IP based filtering if I were you.
What I would do is create a .txt file that stores the IP of a user, with PHP's $_SERVER['REMOTE_ADDR']. After 24 hours, at 12:00, have your webserver wipe the IP file, enabling everybody to post again for the next day.
Depends if you want to have users + guest or just guest.
If you have users (logged to the site, with profile and everything a normal user will get) and also allow guess to post, then it's easier to add a guest + ip (or another filtering option) to the users table and associate it with the post table by an user_id column in the post table (kind of what is done here in SO).
But, if there's only guest in the site and it's not intended to have logged users, then you just need a post table with an ip column where you can register which ip did the post.
Also, as Iansen said, the IP filter can be easily bypassed. Is it a strong requisite that the user only post once per day?

How to do a registration confirmation email which will expires within 24 hr

I have a php registration form but now I want to create a registration confirm email which will send to provided email and expires within 24 hr. and when that link is clicked then registration will be confirmed.
Please anybody help and provide some code.
Thanks in advance.
The idea that's generally used is as follow :
When generating / sending the mail, you include in it a unique identifier (random, hard to guess) -- that identifier is in the link the user has to click on
This means thins link will look like http://www.yoursite.com/validate.php?id=HQGETBDC
At the same time, you record a piece of data in your database, with :
the user's to which the mail has been sent
the unique identifier (to be able to find this record)
the current date / time
when the user clicks the link, he'll visit a page on your server
that page will use the unique identifier (present in the link), to find the relevant record in the database
if that record is more that 24 hours old, the user will not be allowed to validate his account.
In addition, you'll probably want to code something to remove old entries (more than 24 hours and not validated) from your database -- using a cronjob, for instance.
You can store a list of email addresses, confirmation codes, and dates they were sent in a database. When the user tries to run the confirmation you check if the current time is less than 24 hours ago.
Once a day or once a week you run an automated script to delete stale entries.
If you don't use a database you can also use a bunch of text files as an "ad-hoc" database.
Supposing the e-mail only sends a link to an activation page with a key (/activate.php?key=14315515151...), then it is as easy as blacklisting/disabling the key.
If you explained better how your activation system works, it would result in a more precise answeer.
For email verification, you want to provide a token. And since you store that in the database, just save the expiry time too:
db("INSERT INTO confirmtoken ...", $confirm_token, time()+24*3600);
The time()+24*3600 represents a timestamp 24 hours from then.
When the user clicks your confirmation link ../confirm?token=3281nfakjnih98 then simply check if the time hasn't passed:
db("SELECT * FROM confirmtokens WHERE id=? AND maxtime<UNIX_TIMESTAMP()");
These are the steps you need to follow
On registration create a new user and set his status as pending (or similar). Also create a hash to uniquely identify the user. Remember to have a created (or similar) column
Send out a mail with a link that points to your confirmation page. You just need the hash as the identifier
When the user clicks the hash, you can compare the current time with the created column and check whether it falls withing your date range (24 hours)
Normally you would enter a line in a database which has the uid attached to the registration with the date/time it was sent, and a clean up that runs every so often, depending on how busy you expect the site to be.
Therefore, if the link is clicked before 24 hours is up, then its removed and the registration complete, if not, after 24 hours even if the clean up hasnt run its then removed and the user told its expired, if its cleaned up, its already expired and the user can be told it cant be found, this could be because more than 24 hours has elapsed.

Categories