I'm using Laravel 3. How do I have a notice that can tell me how many users are currently logged in.
I wish I could explain this better but I am new to Laravel and I should upgrade to 5.1 but that is not a option at the moment.
Any help is appreciated.
Step 1: upgrade to Laravel 5.x
Step 2: add a migration to add the column active_at as a date to the users table
Step 3: add a new HTTP middleware called ActiveUser
Step 4: in the middleware handle method:
if ($user = $request->user()) {
$user->active_at = new \DateTime();
$user->save();
}
Step 5: feed your baby dragon
Step 6: you can now get users by activity using the active_at column
I think that is not a built in feature in Laravel. You could add a column to your user table, something like a flag if the user is logged in or not.
Simply set that column to true, after authenticating of the user and to false when the user logs out again.
Where you need the number of logged in users, simply count the rows in the user table where that flag is set to true.
As far as I know there is no easy way to achieve this. What you can do is show how many users have logged in in the last X minutes using the last logged in column in the users column. I admit, this isn't exactly what you're looking for but it might be the next best thing.
There is no way to accomplish this with laravel or php without using a third party tool like html5 websockets ( ratchet, socket.io etc ) or javascript with ajax.
it is pretty easy and more reliable with websockets. you can create your server to handle connection/disconnection and keep record of sockets connected and emit a message to all connected sockets count of actively connected sockets. This will be real time and less transaction since websockets will only trigger when there is an activity ( someone logins/logouts ) but requires higher access level of control over server/machine in order to install node.js, socket.io and open a tcp port for communication.
second option is javascript with ajax. keep a column in users data table as last_online and update this data each time user browses any page in your site. Like put a code at the top/bottom of your each page and update this columns value with current time. secondly determine how much avarage time a visitor roughly spends on your each page. then create a ajax script where you show your onlines count and trigger it every x amount of time that you determined ( i.e every 30 secs ) on the other side of your code check how many people were online since your last check ( current time - x time )
for instance
$query = "select count(id) total_online_last30 from users where last_check>='" . date( "Y-m-d H:i:s" , strtotime( date() . " +30 seconds") ) . "'"
then print it out.
Related
i am working on project ( php laravel + stripe payment ) which i need to let the user pay for product by two step :
the first one will be triggered by the user to pay half amount of
product's value
and the second one will be paying the remaining amount at specific
time automatically without of being triggered by user
the question is : what's the best solution to apply the second step ( paying automatically in specific time ) ? using stripe subscription or laravel cron job or there is better sollution ?
any ideas will be highly appreciated
If you don't repeat the second step, I would recommend just creating another Payment (normally using PaymentIntent) at the specific time. You can use a cron job to find previously created PaymentMethod, then create a PaymentIntent and try to confirm it immediately like Charge the Saved Payment Method step here
i have created a booking system which synchronizes with Google Calendar every 5 minutes and also truncates and old data and fetches new one from calendar, this process takes about 2-3 seconds.
What i want to do is, when the event to fetch data from Google Calendar fires, i want to disable access to the route of the booking system for these 2-3 seconds then enable it again when the event ends, i want to do this because it truncates the old data and fetches new one, so if a person is looking at the booking system this 2-3 seconds all the book schedule times will be free and i don't want this to happen.
Is this kind of thing possible?
Thanks and regards!
Well, my suggestion is to create a new field in your users table something like : is_google_sync_active and route middleware.
Then just before you will start sync. you change that field to true and in your new middleware you may check whether your sync. is active or not, then when your sync. ends you just change the value of field again to false. So, every time when user tries to pass to your sync route he will be passing your middleware which wil handle all job.
In addition, you can redirect user to page which shows status or progress if the sync. is processing.
I have a basic laravel project which have login, logout and some others basic public pages.
I would like to count all session for the current time(now time). Any session that should be count login user or visit any public pages.
From this project, I would like to know how many session is running?
What I understand from your question is, you want a feature in your application where you can count number of user that are logged in to the application.
If that's the case then as stated above you can hack a way around by adding a bool column in users table or whatever table you are using to store users, and whenever any user logs in, you change that column value to 1 and when the user logs out you change the column value to 0.
This way you can count the users by using that column where the column value is true or 1.
Hope this solves your problem.
I need to create to send a message to all new users registered on my website. I created a table called messages that admins can store (insert) the messages from the admin panel to this table and these messages are simply shown to all users with a foreach.
I don't know if this the best way to do something like that!
Anyway, the problem is that when any new user register and open his dashboard he just found the old messages
This is the table :
image
And this is the simple code for foreach
$msgForAll = Message::latest()->get();
I'm not sure how to display new messages to the users.
Again the way I made this idea is wrong, I know that ):
You can use eloquent's where spefically the whereDate queries in your case to get certain rows past or before date.
As an example relating to what you want to do, it can be along the lines of this:
// Given that you are using Auth to get the user's data...
// Get messages that were created after the user's creation date
$messages = Message::whereDate('created_at','>',Auth::User()->created_at)->get();
I am using comet chat with Code Igniter 2.0.
Problem-
i have a listing page where all the users are listed and i need to show their comet chat status ("Online" or "Offline") next to their name.
For this purpose we have a DB field ('chat_status') in our users table, we usually set this field to '0' or '1' when user logged in we set this field to '1' and when user logged out we set this field to '0'. I just want to use comet chat call backs that will return all the online user_ids in array so i will change their 'chat_status' to '1' in our users table.
I couldn't find any comet chat function or query that will return all the online users in one shot.
Can you please assist me to solve this stuff.
Thanks in advance
There is a function in integration file [in my case its in its integration.php]
function getFriendsList() show all the friends on-line but if you want to show all the user insted of friends fellow the instruction given in link
You just have to edit the 3 sql queries in integration.php function by making changes to the username field .
You can write a subquery and get the status of the user that will be very easy .