I am making a PHP & MySQL (and CodeIgniter) video/chat program that 1) displays current online users, and 2) forces all users to disconnect when the moderator leaves.
However HTTP is stateless. What is the best method to implement the above features?
Should I use a heartbeat AJAX request for each user to determine the current state? How will I know if a user (such as the moderator) gets disconnected?
This would require a close to real-time implementation. You could have a table that has all the rooms / chat sessions created. Then in your users table you could have a moderators column as well as a current room id column.
Rooms Table
Columns: ID, USER_ID, CREATED_AT, UPDATED_AT, NAME, PASSWORD, HAS_MODERATOR (bool) ....
When a moderator joins a room, you could update the "HAS_MODERATOR" field to true or 1. Then you could write a simple long-polling script that would simply make an ajax request to a php page or route and return a value of true or false. If it returns true, then a moderator is still in the room, otherwise you can close the room and force a redirect through javascript or something. The php script would simply check if has_moderator is true, AND you would go through the users table, retrieve all the moderators, and check what room they are in. Match the current room the moderator is in with the room your checking for. This will ensure that a moderator is in the room.
This might help you out: Notify Ajax/Javascript that background PHP has completed
I think you can implement it without using state logic of HTTP. You will need to store a current conversation state in database. Every user request will have a authenticated username cookie and a conversation id which will be used to pull the session from back end DB. Also, there will be a variable to check whether the moderator is present or not (heartbit style check). When a moderator leaves, simply change state of session which will for all others to logout. Let me know if you need any details of any step I suggested.
Related
I know this question is not coding specific and might be broad, but I just want to know the thinking process because I am a bit lost.
I am trying to make a simple board game with Ajax and PHP. I know making games with web sockets would be far better for real time experiences but for now I am keeping to Ajax and PHP only. Till now I have been able to create rooms for users but cant understand how to make other users join that room and account for their details like name, score etc.
My situation:
I have an index page where users visit, and then the user fills in his name and chooses board size, then he is redirected to another page with a link like playground.php?room=123456
Another user uses that link and enters that room but then I don't know how to identify that second player and store his details like his name etc to the db.
I should store data in a database and cookies.
In the database, you have 3 tables
tblUsers (username, userid, ip,...)
tblRooms (roomid, creatorid,..)
tblPlayers (roomid,userid,..)
If a user visit your webpage, check for a cookie that has the userinfo.
If there is no cookie let the user login or register.
If a user creates a room, store this data in tblRooms.
Now he can share the link.
Another player visits the room. First you have to check if the player is registered (check for cookie). If not, let him show a registrationform.
If registered, store the user id in tblPlayers. With that table you know the players for that room.
I am not an expert in PHP of course, but I would do this:
1) When a user fills in details in index page, you can save some basic info in local storage or using cookies, so you can get those details later.
If you have a database, then you can have a table in which you will save users...
2)When that user enters playground.php?room=123456 you can get those details you saved in localstorage or cookies and add that user to the database.
3) You check the database for the player who created the room every x amount of time and if there is a user you encounter for the first time, then you can do some action..
I hope this helps you a bit :)
Good luck with you project ;)
Description:
I am creating comment/reply functionality to a web app that I built. I have a post that I want to link these comments too. To decrease spam and encourage community involvement I want to implement a voting system on each comment/reply.
Problem:
I know how to set up the database and I know how to show upvotes/downvotes. The only thing I don't know what to do is to keep the vote... voted even if the user refreshes the page. I don't want a user to be able to vote up more than once on a single post. Something like the voting on this site, it tracks that you have already voted with a yellow upvote.
What I have thought of:
Place post id in cookie with the user_id appended to it. A simple check of the cookie can stop the user from voting again
Place a unique constraint in table post id... but this is where I get confused. Should I have a separate table just for voted posts? Database schema idea ( I think ) here: https://stackoverflow.com/a/12350981/185672
Keep all the ids in a session array and check against all voted on ids... but that would get huge.
Combination of cookie and database to reduce number of db calls.
In the future there may be 1000s of votes cast by a single user.
edit:
I figured out that storing the results in a database is a must. How can I check for every reply/comment if the user has valid voting privileges without making 1000s of calls?
Resources:
Helped with some further spamming problems, but didn't answer initial question:
https://stackoverflow.com/a/2333085/185672
Old solution that explains how to count votes but not keep the "upvote" checked.
http://www.9lessons.info/2009/08/vote-with-jquery-ajax-and-php.html
Bonus question:
If you guys know of a great script that can allow me to sync up a commenting system ( with votes ) into my already built app?
Also, I tried to find duplicates, but I just can't.
Yeah, just keep a separate table to track user votes. Since you know which user is requesting the page, you can easily join the votes table to determine the current user's eligibility to vote on each post on the page. For each post, if they're eligible, output one version of html, if they're not then output another.
Once the ajax request asks for the php file that does the vote, you can then check once more that that user is eligible to vote - I.e they're changing their vote, or they haven't voted before.
Take yahoo's news stories for example - when you request a page that you've made a comment on, your own comment has disabled voting buttons. With some hacking of the page inside the browser's dev tools, you can enable the buttons. You can even click on them and vote for your own post - though only once.
So, you can see that they got 2/3rds of it right, and output html based on the user's eligibility to vote. They also prevent multiple voting (server-side), they just don't do a server-side check to ensure you're not voting for your own comment.
Stack Overflow on the other hand, always shows the same html - but when you click to vote for your own comment, the server-side code baulks at the idea and the response is basically 'bugger-off! you can't do that' having received a negative result from the server, the javascript on the page pops up the message, rather than updating the vote count.
I suggest you create a "hidden field" in your page that stores information about the upvote/downvote by the user. This is how you would use it:
If the user upvotes/downvotes, the javsvscript on the on the shows the upvote, sets the field to maybe true to indicate the vote status and would also send an AJAX request to the server where the server would record the vote status in the user profile database.
Then whenever the post is re-loaded for a particular user, the server sets the hidden field server-side to true or false, depending on the vote record that is stored in the database. The JS would, on load, check the hidden field and set the vote on the page accordingly (you might need an extra hidden field to indicate whether it is an up or down vote).
I have some questions about database design for user tracking (analytics)
My website will have 3 classes of user:
non user
user with incomplete regstration progress
registered user
For each of these classes I have different events I want to track. Ie: for a non-user I want to track if they reach registration. For registered I want to track if they make it to a certain page etc...
At the start of each visit I will record their landing page and referrer, then I want to track each of these funnels. So for the table design I am thinking:
A table with their session id (stored in a cookie) and user id if they ever log in=one row per user + one row per guest
A table for visit details- referrer, landing page, time etc... so If a user from table 1 visits 10 times they will have 10 entries in this table. Maybe have another table with a list of browsers?
3 tables for each of the user types with tracking flags. Each row will link to the visit in table 2. So there is a 1:1 relation between these 3 tables and table 2
Is this a logical design for tracking users? Does anyone have any links with good articles on user tracking?
Also, how reliable is the http-referrer header for tracking where users came from? Is there any other method I can use to see where people came from?
Also also, how scalable will this table design be?
Thanks, M
Not having done that before, I find your approach not bad - propably improvable, but ok. Just wanted to add that - depending on your requirements - you could be very lazy and let Google Analytics do that for you!
With different tracking rules, you can set up Google Analytics to track differently depending on user cookies, which you can set in the registration/login-process (or not for the non-user).
Downside of this idea is that you'd rely on javascript and you won't have seperate data for each user, just a total of the user-class.
Here's the GA Resource how to set up exceptions (see the lower description). Then create three different GA tracking codes and on each only accept user with either no-cookie, logged-in cookie or registration-process cookie.
Hope I could help, anyway good luck!
I'm doing a system with Codeigniter , this is my first system with CI, and i'm also novice to PHP too.
I'm doing this for a hospital, in this i have the following problem
junior doctor first check the 1st visit patients and then if he can't handle them he refer them to the senior doctor
from registration room some patients are send to the eye checking room to check their eyes and then they go to the junior doctor
like wise i have temporary data to be kept on the system, references from one room to another and so on...
i need to get this details to the main GUI of the each person; for example in the Senior doctors UI there will be a tab named 1St time patients, in that the patients that was referred by the junior doctor will be shown to him! so i need to refer to the patients that was sent to senior doctor from the junior doctor and show them in the senior doctor's UI.
so my problem is how can i keep this temporary data to be referenced by the system? keeping them in the tables is not appropriate as i think because at the end of the day these data is not stored any where, only the patient table and few other tables will be keeping the data.
guys how can i achieve this kind of thing? any method of achieving this? technology that is easier to master that will allow me to keep temporary data?please give me some references or help by code to over come this problem.
regards,
Rangana
If the data is truly temporary, and has to be used by only one user at a time you need to stick it in session.
An entry level tuorial is here:
http://www.w3schools.com/PHP/php_sessions.asp
However if data is accessed by different users, but is simply not needed on following days, or you are storing a significant amount of data, you should probably keep it in the DB.
The DB should be able to store lots of data, so on a smallish app there is not much reason to keep clearing it out, but you could also include a housekeeping function that clears data that is old or irrelevant.
However when working with medical data, it may be a good idea to hang on to everything.
everything will do with ajax (or something that always refresh the browser for number of time like meta refresh tag) that will notify senior / junior doctor if any patient referred to them.
you need to add a flag at database if the doctor already retrieve the notification so it will not notify the doctor twice.
for example your database table :
Name of patient | referral | referrer | flag_retrieved
you need to specify referral doctor at the session so it can retrieve the correct record and then notify the doctor
then your system should :
request to database if any doctor to
refer patient to them over time (you can use ajax or meta tag)
database will check if any record match and not yet retrieved
send request to browser, than notify the doctor if any referred patient.
if you need to clean up the database, you can use cron every end of day for deleting any record at the table.
How would I go about creating a system in which a user must click a link to validate something.
Let us say that on my website, each user has their own folders that are not shared. I want their to be an option for each user on their account page in which they can create a trust with another user to access each others folders. Perhaps by typing in the name of the user in which they want to create a trust with.
So in this case, user Jim types into his create trust input box "Tom", and so a trust needs to be established. I figure, A) There will need to be something that stores Jim's request, B. then assigns it as unverified, C. and finally then sends the vaerification link to Tom. Tom then recieves a message somehow? (not sure yet), perhaps with something to check and see if there are any verification links for Tom? and then Tom clicks the verification link sent to verify that he wants to also establish a trust with Jim. once Tom clicks it, A. tells whatever it is that stored Jim's request that Tom accepted, B. assigns validation as verified C. notifies Jim that Tom's request has been accepted or declined.,
HOWEVER there also needs to be a way to remove the validation storage (whatever it will be) if 24 hours runs up, or Tom declines Jim's request.
Can anyone please help me? :)
Sounds like a good job for a database. When user A requests a share, it is entered in the database as pending. When user B is notified, he can click a link who's script will check the database for the original request, verify that it is within 24 hours, and if so, the database record us updated as accepted or declined. If 24 hours is up, the record is updated as expired.
If you're using a database, this is simple. Just generate some sort of unique ID for each request, and store the request (with status "awaiting response" or similar) in the database, then send an email to Tom with a link to one of your php pages with the unique ID of the request in the querystring.
This way, you can save update the "time accepted" field for the request to whenever Tom clicked the link in his email, and change the status to "accepted".
It is trivial to logically determine if 24 hours have passed, and you can deny access to the trusted share after this time period.
You can define a table wich has the attributes:
-Unique ID
-ID1 (Of Person 1)
-ID2 (Of Person 2)
-Validated State (TRUE OR FALSE)
-Validation Hash MD5(Name(ID1)+RANDOM(100,99999),Name(ID2))
-Date and Time of requested relationship
-Other information pertinent to the system that needs this function...
Then you proceed like you said.
1)On 12/5/2009 at 5:25pm Tom (ID=15) asks Mark(ID=21) to join his network, then your script add the data to the table as follows:
-Unique ID: Assigned automatically by autoincrement
-ID1: 15
-ID2: 21
-Validated: False
-Validation Hash MD5: MD5("Tom".42574."Mark")=bedbcfc6e679be69ff3587f15213c83f
-Date and Time: 12/5/2009 at 5:25pm
-Other information pertinent to the system that needs this function...
Be sure that, before setting the data into database, check for duplicates.
After that, erase all fields wich Date And Time is older than 24 hours =D
2)If no duplicates, Send mail to Mark with a link back to your page, including md5 hash as a Get attribute of the url. Lets sat the page name is validate.php?h=bedbcfc6e679be69ff3587f15213c83f
3)Your script validate.php recieves a request with the get parameter h=bedbcfc6e679be69ff3587f15213c83f
Proceed to chek de db.
If hash exists, and date and time is not older than 24hours, set Validated to true, and send a mail to Tom's email with the info of his new friend.
If hash does not exists, inform it to Mark throught the validation page results.
Ok. Thats all. Hope its usefull to you in some way.