How to handle users joining a game room - php

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 ;)

Related

Storing/Tracking user activity on a website [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I was wondering what would be the best way to go about storing/tracking user activity on a website. I am thinking of user activity along the lines of which webpage a user visited,how many times has she visited,when(i.e timestamp) of his/her vists.
I am currently thinking of creating multiple tables in my database for each webpage on my website, each of which would track an aspect of user activity on my site. E.g 1 table containing timestamp and userid of each user who visited that webpage, another table storing their actions done on the webpage(e.g liked something)
Would this be a good way of tracking user activity or are there other, better methods of doing so?
PS: Im using PHP and MYSQL for the website, and am doing this as a means of improving my coding so i won't be using google analytics or anything like it.
Yep, store everything in a database. Have a table for user visits, one for user interactions etc.
To start with have a table page_visits that contains an ID for the user (This is your first problem to solve - how to uniquely identify a visitor. Identify them using their IP and or User agent? Create a cookie that refers to a unique ID? There are different methods depending on your goal - none are 100% fool-proof) and the page ID or URL they visited along with a timestamp.
Add a table to track interactions. How you capture the event is up to you, do you fire an AJAX call using Javascript or jQuery to log an event like a click? This table could contain fields such as user_id, timestamp, action_performed, source_page_id.
The way you decide to implement is entirely up to you but a database would probably be the way to go.
try creating tables in your database to store the necessary information.
Off the top of my head I could think to store their IP address
get hold of some GEO information and you can use that IP address to obtain and store the country they are in.
You can store the webpage they were on before coming to your website.
How many times they have came to your website and the pages they have visited and how many times on each page.
IP address can be obtained using
$_SERVER['REMOTE_ADDR']
and
$_SERVER['HTTP_X_FORWARDED_FOR']
geo information to obtain their country can be obtained from many websites...Usually you have to pay for it if you want up to date info...some people off free older info which is still quite useful.
Here is a good provider of GEO information that you pay for but it is very cheap
http://dev.maxmind.com/geoip/geoip2/web-services/
to do the amount of visits just grab their IP address when they arrive, search your database for that IP and if it exists then increment the number of visits by 1. If not create a new visitor. Do the same incrementation on each individual page visit too.
use this to obtain the URL they were on before coming to your site
$_SERVER['HTTP_REFERER']
So a table like this:
userId | userIP | userCountry | visits | webpage1Visits | webpage2Visits | webpage3Visits
Assuming you dont have thousands of pages this could work for a dozen or so pages.
There a tonne of other stuff you can store like average time on site etc etc but this is the basic stuff.
attempt it and when you come across problems along the way ask more questions and people will help :)
you can use flag for each webpage and save the incremented flag value if he clicks on the webpage to the respective users login database to track which pages (s)he is clicking (this is only for the webpages who has the user database).
Without going into the whole GDPR debate, although you may want to take that into consideration before going further, here's how I'd proceed.
I'd have a separate table for storing user activity, although I may want to look into using something else to store this data in. Perhaps mongoDB and have all the details for each session stored into a json file. but for the purpose of this excersise you could just use MySQL.
I'd suggest adding entries based on sessions so the user activity table would look something along the lines of:
1. usserId
2. location or ip
3. referrer
4. timestamp
5. sessionToken
The second thing I'd do is have another table for just the session table and have all the details stored there with timestamps.
1. sessionToken
2. url
3. timestamp
Now you can query the last x users that visited your site from the user activity table and when you select one of them in the interface you can see all the details of his visit. This is probably the less taxing method of viewing this information as far as your MySQL queries go.
This is a juvenile look at a very complex process that over the years A LOT of companies started using and it's called Real User Monitoring.

PHP/MySQL and State

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.

User tracking table design and method

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!

adding the number of referrals and referral id's into the MySQL DB using PHP

I have setup a referral system, whereby a user enters their email address and can then refer a friend. The friend is given a unique url followed by a code, ie: example.com/FHF73H
The DB is setup as such;
email_id email code ts
1 test1#test.com 98VIB15 2011-03-17 01:52:22
2 test2#test.com 412395D 2011-03-17 01:58:00
3 test3#test.com 6521298 2011-03-17 02:00:51
4 test4#test.com 3VJ7AB5 2011-03-17 02:01:02
I have htaccess setup so RewriteRule ^([a-z0-9]+)$ /index.php?_url=$1 [NC,L,QSA] and I can then use $_GET['_url']; to get the referrer code.
For each user in the DB, I want to track:
a) A count of how many people have visited their referral link
b) A count of how many users have then gone on and subscribed (by entering their email address and submitting)
c) A comma-seperated column of the email_id's of the users who performed b).
Essentially 3 columns are required.
Where must each of the above (a, b, and c) be placed in the code to function the way I'd like? I'm also unsure how to code all that up in PHP and what certain-type of column properties are required in the MySQL database.
Any help would be very much appreciated!
Thank you!
A)
There are 3 ways you can count how many times a referral link has been clicked.
Every click made, always
Everytime the URL is called, you update the value in your database. This means one person can continually click and it will all be stored. Intentionally or not.
Every independent click
You'll store a cookie on the user, and only increase the count when this cookie is not present. This will prevent people from accidentally increasing the count if they click the link two times. However, they are still able to clear their cookies to increase count.
Every unique click
Here you choose to store what IP the click came from, and only increase count when the IP is not already present in the database.
B)
You'll have to save a cookie (regardless of above method). In this cookie you'll save whatever ID this referral had in your database.
In extreme cases, you might want to save to a database instead of a cookie to prevent people from clearing their cookies as to fake not being referred by anyone or the user doesn't accept cookies at all.
There is, of course, the possiblity to not save anything at all, and have the user land directly on the registration page. But the referrers ID will be lost if the users leaves or changes page.
C)
I wouldn't use CSV on a RDBMS in this case, instead I would probably have a new table with user_id (the person who registered) and a referral_id (ID of the referral). CSV or serialized data can be needed sometimess, though.
Summary
The user clicks on a referral link, you save a cookie of the ID of the referral on the users browser and increase the click count of this ID in your database.
When the user submits the registration form, you check for a cookie, if the cookie is found, you increase the referral count based on what referral ID was inside this cookie,

where to keep temporary data?

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.

Categories