Check if a User Has Clicked on a Button - php

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.

Related

How to know if user in session has changed?

I'm making a page for submitting some info about user and after that data is inserted in database.
How can I know if current user has changed?
I'm using CodeIgniter and I can get users id but it's the always active one. How do I know if session has changed after page load?
Right now other user could log in another tab and submit on first tab would post session id from latest user.
Use tokens therefore sessions will be stored in the users browser not in tabs. Check the PHP documentation or YouTube videos they have good tutorials on how to do this effectively.

Is there such a thing as a unique browser $_SESSION id?

I am working on add to favorites add-on (where posts will be added to favorites by signed in users), but then I saw that craigslist lets anyone add favorites without them being signed in. How do they do that? Is there a unique $_SESSION id for a browser? Sorry if it's a silly question.
As an example: http://orlando.craigslist.org/search/jjj (Click on any star to add to favorites. May not be available in IE 11).
They are just using a session, you should be able to check this by looking what cookies they are storing on your computer.
Everything in $_SESSION persist only for the current session. If the users closes the browser and reopen it a new session is generated. So $_SESSION is not the right place for your answer.
There is $_ENV where you can get the clients ip address and general browser data from. In most cases this is enough to identify a single user. If you keep track of these data you can prevent users from giving multiple votes.

How to know if user is logged with his session id

I need to know if there is an user logged in my website. For this purpose, I have only his session ID.
I got this id using: session->getId();
¿Is it possible?
Thanks in advance.
add a custom field to the session when the user logs in and then just check that field
Given that the web is essentially stateless, it is hard to know for sure if a particular user is logged onto a website.
One way to accomplish your goal is to keep a running log of all the users logged in, and the last visit time. Then you could query that log and if the users last visit time was less than 5 minutes ago, you could then say the user is logged onto your site. This will only tell you if the user is logged into your site, not if your site is the active tab in their browser.
Another way to get more "real-time" information as to the the active users of your site is to use something like SignalR which will allow you to do push to the browser. I've used SignalR in the past to send out system status messages to the browser and have it automatically update the page. The great thing about SignalR is it maintains an in-memory list of all the clients connected.

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.

How can I make remember voting with cookies easier than this?

It is the most easiest to describe my problem with a working example: even if you are not logged in, YouTube remembers what you have watched, and next time gives you suggestions based on previous watched movies.
My site is similar in a way: the users can vote on articles without logging in, and the site remembers votes with cookies. I have figured out a working method, but there has to be an easier way - also now the DB usage is anything but optimized.
For every visitor there is a check if he has the cookies. If yes I query his votes. If not I create a dummy user, and send him out the cookies. Now I store this users "last_visit" timestamp. After this everything is the same for both users. My problem is that my DB is filling up with dummy users, so I made my cookies expire in 3 months and my site regularly check which users didn't visit my site in the last 3 months, and deletes them from the DB.
I know I overcomplicated this, but my vote system is using AJAX, and I couldn't find a method to send out a cookie (and create the dummy user) only if a vote happens and not every time a simple visitor browses my site - without vote.
Also a note: I insist on using cookies - I know it would be easier to store IP-s when a vote happens, but there are schools, businesses using the same IP, and I like to allow their users to use my site.
What did I miss here? How can this be optimized?
if they do not hold a permanent account, why store anything related to them in the database at all? just record their prior votes in the cookie. you would also store averall votes in the db, but anonymously, and not relate these to "users" at all.

Categories