Reading cookie and using jQuery to display data - javascript

Here's the idea.
I'm coding a chat bar, I have everything functioning. But my issue is keeping everything continuous whilst browsing pages across the site. By this I mean, chat boxes staying open. Like Facebook, as on Facebook, every time you open up a chat box and then navigate to a different page, it stays open, you don't have to go back into your online friends list and load of the chat box.
What have I done so far?
One method I have thought of using is using cookies to store the ID of the conversation in a cookie and put whether it is open or closed. So if a conversation has an ID of 64 a cookie is made called chat_64. I am using this jQuery cookie documentation: https://github.com/carhartl/jquery-cookie
I was thinking that it would be done by putting them into an array, and then using opening a chat box for each cookie.
I have come to ask for suggestions on how this could be done?

Don't store that kind of information in a cookie, use sessions in your server. It's a big risk for privacy.
Store session data or use a database, with a call to a php(for example) script that fetches the current chats from a database, an additional feature is that if the user logs off and then logs in again, the chats could stay the same.
the table could be something like this:
chat_id | user_id | chatting_with_id | timestamp
When the user closes the chat window you might just set a time limit for the user to reopen the window and if he/she didn't, send a delete request for that row.

Related

How can I keep the session open on my site using Java HTTPSession even by closing and reopening the browser? (NOT PHP)

I would like to know how I could do to keep the session open on my site even by closing and reopening the browser. Right now I'm studying everything that has to do with Java Servlets, JSPs, etc ...and I should create a project that aims to create an e-commerce site.I will explain myself better with regards to "keeping the session open on my site also by closing and reopening the browser": I would like to do what Amazon does more or less. Upon registration except for username, password, name, surname ... taken from the form in the database and at the time of login I take this data and i access the site. The problem is that if I close the browser I should log in again and I don't want this.As I understand it, I also need to implement a session to implement the cart where I should save the products I want to buy.So, in summary, how can I make a connection that lasts as long as I want until I press the Logout button?
This is for a university project that consists of the creation of an e-commerce site.
Have you tried using cookies? Maybe you can create a code that reads if there is a cookie with the user data, if it exists you can create a new session with that data.

How to set up free and paid versions of website?

I am currently redesigning a website and looking for a solution on how to add a paid version of the site.
For example, say I have a <select> drop-down box with 20 elements inside. However, I want 15 of these 20 elements to be disabled unless the user has a paid account. At this time, that is the extent of what I need to differentiate between free/paid versions.
I'm planning on adding the ability to register an account and log in, as well as some type of payment processor (recommendations are appreciated for this! - currently looking at using Django/Python). I just don't know how to best go about managing two different levels of the website, and allowing those additional options to paid members.
I'm working with calculators that are pure Javascript. Using Bootstrap for the page design. As far as anything else goes, I'm open.
To do that, you'd have to add a field with a default value of NULL to your database, let's call it "subscription", into your users table. Then, everytime a user login to your website, fetch the subcription value and write it into a session variable. The last thing you have to do to ensure free members aren't allowed to perform any actions the subscribed members can is checking the content of the session variable while :
building your html, or you could check it on the client side with javascript right after the premium element has loaded (this is for user experience only since a client can remove any html attribute whnever they want)
and
while recieving the data of the premium element on the server side, accept it if the user is premium, reject it if they're free (again, just checking the session variable should do the job).
So as you can see, it's much more about preventing free users to gain access at the paid members options than allowing paid members to browse a completly new version of the website designed specifically for them.
I will try to give you a blunt idea maybe this might help you.
So lets say a user has paid for your service you can flag a token in yours app's backend if a particular user is paid or not. So whenever the user logs in your app next time you can get the status of the logged user.
Once you get the status of the logged user you can enable or disable ui elements.
I hope this made some sense.

How to prevent a function from firing off when the user refreshes a page?

I'm making a game, in which a user logs in, clicks a "Start Game" button, and a random token (and team JSON) is generated after a user clicks that button and navigates to the game page. The functionality works in that our Firebase database is updated each time the game page is loaded with a brand new token/team object, but what I want to prevent is the function from firing off if the user keeps refreshes the page while still logged in.
What is the best way to track logged-in sessions? Would trying to track cookies be a better approach?
If you are already using Firebase, then you can take a look at the Firebase Auth APIs, which handle sign-in sessions very well.
If you are just storing game sessions, and nothing secure or valuable, then cookies will be an okay option, just remember that they can be edited by the user.
If you want to keep a page from calling a function on refresh, a good way to do this would be with cookies as well. You can create a cookie when the user first visits the page, then it checks for the cookie when the page loads, and if it doesn't contain the cookie, then you can call the function. You can also give a way for the user to delete the cookie if they want to start a new game.
have you tried
event.preventDefault()
https://api.jquery.com/event.preventdefault/

Allow same user multiple login on different number of devices and how to deal with sessions?

I'm building a chat application with some php, mysql, nodejs, socketio.
I want to allow users to be logged in from nomatterwhat number devices. Like eg. Facebook where it doesn't matter which screen you're looking at. They all reflect the latest changes.
Tested it right now and it works that way. If i open the notifidations the number dissappears on the same page on different devices.
My guess -and started building with that iedea in mind – was that a room is created for each user when a user logs in the first time and any following login from the same user is also added to that room so the changes can easily be broadcasted to the room.
All browsers are aware off the multiple logins because i send a soclet-message to the room setting a variable - multilogin- so that it knows that there are more.
Also needed to see when a user clicks on the logout-buttons it send that information along.
If it is the only one logged in it should kill the session set the user offline.
Actions performed on one device, broadcasts it to the other members in the room etc.
Seems logical?
It works so far but i'm uncertain about the session part of this setup.
The logic in php -inherited from the origanal build of the chat – checks the user logging in to see if the user is already logged in and if so, it destroys the existing session first and then sends a loggout command -which i prevent now when the multilogin parameter is send along- to the other browser.
My question now is, what could be a logical approach towards the sessions?
I was playing with some ideas in my head and then i thought, let's look on the internet about the subject.
An idea could be that only the first loggin in gets the session created and is shared by sending the id to the others so they can be identified as being the same user.
Would that be an approach that one would suggest?
Found a simple solution by letting each device with same user that logs in getting a new sessionid which is send to the others (socket message to room) so their sessionid becomes the same to authenticate themselves to the server. Session id is save enough for that purpose.

Auto log out when url change in browser tab

I need to build a feature like most of the banks use. Where..
if user has log in to bank account in a browser tab & again he/she change the url of browser & move to some other site.. and again come to bank's page by clicking browser's BACK button.. then bank automatically log out user from there site.
I think may be by java script we can do this.. but, can not able to understand how to do this. I'm using PHP for my server side script. Is this, possible by PHP to do this..
Regards
Suresh
This not a java/PHP question but depends on the exact behaviour you want to implement.
The only way to track the user "live" is through javascript. So if you want to know when the user leaves the page, you can bind yourself to an event listener and then do an ajax call or something like this that invalidates the session on the serverside. Keep in mind that users may be browsing your site with JS disabled, so you need a fallback on the serverside.
I would recommend you to implement session storage on the serverside with a storage mechanism (either the built-in PHP session store or some external storage like Couchbase or Redis, Memached,...) and set the logout time to a sane default (lower if it is something like a banking application).
If you have the basics in place, use JavaScript to enrich the user experience, for example by showing a "countdown" when the user will be logged out and sending session refresh ajax calls to the server to renew the session every time the user has an interaction with the website and such.
For more detailed information I'd need more requirements from your side!

Categories