Most efficient way to backup user preferences on MySQL Database in PHP? - php

I am working on a website where user clicks on a add buttons to get information about that topic. Currently, when user clicks on any add button i store the data in a cookie. For example,
Let's say the Topics are A, B, C, D and so on.
I store them in a cookie like - A,B,X,P,Z. In other words as a comma separated list. What I want to do is backup the preferences on server in a database. This way if the user clears cookies sometime i still have the data backed up. Also, the user will be able to login on other devices with same preferences.
If I connect to the database on every user click, I will hit the concurrent maximum connections list probably. Is there some way to do it efficiently?
Is there some other option altogether? I am looking for something like feedly where user clicks on add button and the preferences are saved.

Related

track all user's activity using cookie with php

Here is the idea for keep track of a user in an online-shopping website:
1 - When a user comes in for the first time, I'll create a random hash and I'll send this hash through a cookie to the user and simultaneously I'll create a user with this hash in my Users table.
In that table I will store many users data like: (and I use mongodb by the way)
* User page visit
* User choose products (means user cart in my shopping cart)
* User last login
and ....
2- When the same user comes again (say a day later), he will send that cookie (hash id) and I'll search into my database for that id and retrieve any data I want (e.g shopping-cart info).
I think this works fine and is a good way track the user (Do you agree on this?)
Problem :
What if a user cleans his browser history?
How do sites like youtube save our data (favourites and .... ) and even if I clear my browser or use another ip they had my favorites ready for me without logging into my account?
youtube-like sites store each and every details about the user interactions. For showing your favorites, your likes and other things it saves the data on the server. Whenever the user logs in, the users data will be shown. If you want to implement this, then it is better to go for cloud computing to manage data efficiently.

php cookie or session for viewed details?

i have data from database and i need to count the views for every click,
i need to count only unique click for the current hour.
saving it in database is not in my options. and for the user to know that he/she already click that data
should i use cookie for every data that users clicked or session?
thanks.
I would store it in a session, there is no need to send it back and forth with every page request, that would only slow things down, especially if the site is big and the internet connection of your visitor slow.
However, note that you can only display this information to your visitor if you use sessions or cookies, there is nothing that you can actually do with it unless you store in in a database or a text file.

Check if a User Has Clicked on a Button

What I would like to do is essentially check whether or not a user has clicked a certain link on a page. The idea behind the functionality is that a user can click "Thanks!" on a post created on the website, but they can only submit one "Thanks!" per post.
I have no problem creating a simple click counter for the "Thanks!" button, and save the count to a database using PDO, but I am not sure how I can assign a click to the user who's session is active.Also, posts are constantly being added to the site, so I would need a system which would scale to more posts automatically.
Any guidance is appreciated, thanks!
Note: I know I have not put any example code, but I believe this is simple enough for a good programmer to grasp quickly.
EDIT
On further thought, the IP can not be used to distinguish users, as this website will be used by many people on the same IP.
Are the users logged in or anonymous? If they are logged in just save their user id in a session variable and upload it with their vote to a table in your database.
I do something similar using an Ajax request to a .php page that inserts a row with the session user ID and the vote.
If they are anonymous you can save their ip but that leads to problems with users who share the same ip address. Another (bad) option is to use cookies but users can obviously just clear their cookies and vote again.

How to understand closed window

I have created a chatroom.php and i made target="_blank" that directs the user to the appropriate room whenever a user clicks a room's link. Also, when a user clicks that link i stores the nickname of the user in database, to display the nicknames of the users in that room. However, when a user closes a room's window it still displays that user is online which is not wanted. I need a way that whenever a user closes a room such as chatroom?name=book, i will delete him/her from that room. I hope i made myself clear. It should work like logout.php for certain room. How can i do that?
Thanks
Have a look at the onBeforeClose event.
window.onbeforeclose = function(){
// make a call to PHP saying user is singing off.
};
Alternatively you can use a "idle" timeout (if user hasn't said anything in X minutes, auto-log them off.)
To do this,
You need to create a Automatic Task to check for the users which are still their in the CHAT ROOM using SESSION_Checking or ACTIVITY_Monitoring.
Once user closes window, The TASK will check and make necessary changes.
To implement in PHP using SESSION Checking:
Every time a user logs in you set $_SESSION['online'] = 1; - the session variable is set only for this user so you don't have to worry for conflicts. But if you want to display this info to all users you should probably save the last action time of your user to the DB. if($_SESSION['online'] == 1;) mysql_query('UPDATE user SET last_activity=NOW() WHERE id='.$user_id); or like that.
So by this way you can keep track of the ONLINE USERS in the CHAT APPLICATION.

Storing real time data

I am building a website in which the user can select what list items they see in their navigation menu, my idea is to store the menu items that the user selects in a cookie as this will stop the need for the user to be registered member on the website, is it possible to store realtime data in a cookie and how would I do this? For more information the navigation options are built from a mysql result, the then clicks a link and that link is added to a different list, if they click it again it is deleted, I need to add/remove these items from the cookie as the user add/removes it from there list.
i would use the cookie only to identify the user and do all of your menu option saving in MySql.
Grab the user id from the cookie and query the db for the menu_options and display them.
Either way, storing the data in a cookie or in the database, when the cookie expires, so does (effectively) the user. Plus people delete cookies all the time using cleaners like Adware and CCleaner. I do this about once a week. Cookie = Gone.
This is a bad idea.
The number of cookies a browser can store is not defined (however there is a hard limit for most browsers). RFC 2109 suggests at least 20 cookies per host and a min cookie size of 4k. Certainly the latter is adhered to by most browsers.
You're also going to have to replicate all the features of session management without the nicety of having server-side state. You do not want the kind of pain going down this route will cause you. Keep your session data server-side.
There is no requirement for a user to 'log-in' to have a session. You just need to assign them an automatic identity in a persistent cookie (the replace that if they ever do sign in). And map the session back to a more long term storage when the user changes the config.
C.

Categories