How to See If a User Is Online in a Website With PHP and MySQL Driven Databases

How to see if a user is online in a website with php and mysql driven databases?

You don't need the online/offline flag, you just need to save the last activitity time. When displaying the user status, if last activity time is less than now+15 minutes then user is online, offline otherwise.

How to know if the user is still online on the website or offline

You have three options:

  1. You can assume the user is offline if they don't load a new page within a specific period of time (e.g. 10 minutes). This would be done by keeping a "last page load time" for each user on the server.

  2. You can have a script on your pages that fires off an AJAX call every so often to tell the server "I'm still here" - this would use the same logic as #1, but would be somewhat more reliable.

  3. You can use a page-unload AJAX call to tell the server when the browser is unloading the page, and mark the user as offline until they load another page.

How to get list of all logged in users in Yii2?

The right way here is to work directly with a list of active sessions. By default sessions are stored in filesystem (each in own file) and it can be inconvenient to work with them. It will be much easier for you, if you'll use yii\web\DbSession storage instead of default yii\web\Session. Then you will be able to query all sessions right from database.

Here is samdark's recommendation: https://github.com/samdark/yii2-cookbook/issues/92

Could be solved by using DB sessions and writing additional info into the table:

'session' => [
'class' => 'yii\web\DbSession',
'writeCallback' => function ($session) {
return [
'user_id' => Yii::$app->user->id,
'last_write' => time(),
];
},
],

Then it's easy to work with it via SQL:

-- Get all users active in recent hour
SELECT user_id FROM session WHERE last_write - 3600 < :now;

P.S. Prior to use this config you should execute a migration to add session table to your db and add fields user_id and last_write.

Default migrations are under this path: vendor/yiisoft/yii2/web/migrations.

Or just execute something like this (should be adopted to your case):

CREATE TABLE session
(
id CHAR(40) NOT NULL PRIMARY KEY,
expire INTEGER,
data BLOB,
user_id INTEGER,
last_write TIMESTAMP
)

To learn more about sessions in Yii2 visit this section of Yii2 guide.

How would I implement a simple site search with php and mySQL?

Everyone is suggesting MySQL fulltext search, however you should be aware of a HUGE caveat. The Fulltext search engine is only available for the MyISAM engine (not InnoDB, which is the most commonly used engine due to its referential integrity and ACID compliance).

So you have a few options:

1. The simplest approach is outlined by Particle Tree. You can actaully get ranked searches off of pure SQL (no fulltext, no nothing). The SQL query below will search a table and rank results based off the number of occurrences of a string in the search fields:

SELECT
SUM(((LENGTH(p.body) - LENGTH(REPLACE(p.body, 'term', '')))/4) +
((LENGTH(p.body) - LENGTH(REPLACE(p.body, 'search', '')))/6))
AS Occurrences
FROM
posts AS p
GROUP BY
p.id
ORDER BY
Occurrences DESC

edited their example to provide a bit more clarity

Variations on the above SQL query, adding WHERE statements (WHERE p.body LIKE '%whatever%you%want'), etc. will probably get you exactly what you need.

2. You can alter your database schema to support full text. Often what is done to keep the InnoDB referential integrity, ACID compliance, and speed without having to install plugins like Sphinx Fulltext Search Engine for MySQL is to split the quote data into it's own table. Basically you would have a table Quotes that is an InnoDB table that, rather than having your TEXT field "data" you have a reference "quote_data_id" which points to the ID on a Quote_Data table which is a MyISAM table. You can do your fulltext on the MyISAM table, join the IDs returned with your InnoDB tables and voila you have your results.

3. Install Sphinx. Good luck with this one.

Given what you described, I would HIGHLY recommend you take the 1st approach I presented since you have a simple database driven site. The 1st solution is simple, gets the job done quickly. Lucene will be a bitch to setup especially if you want to integrate it with the database as Lucene is designed mainly to index files not databases. Google custom site search just makes your site lose tons of reputation (makes you look amateurish and hacked), and MySQL fulltext will most likely cause you to alter your database schema.

Building a database website in PHP and MYSQL

In my opinion, take up php basics from the ground up, learn your variables and what they do, if/else statements, loops n functions etc etc..and leanr how/why they operate.

once you have a handle on the basics, then do something like was suggested above like lynda.com(ive used em theyre great) and other tuts online/books etc.

To do what you want to do, not only will you need this (the basic concepts i put up above), but youll need to learn SQL/MySql as well which is another simple but easy to messup language especially at first depending on what/how/how much you want to extract from the DB, mixing it with php and printing on the page, seting up databases/tables etc etc...

ALSO, in order to organize and display your content, youll need to understand how to extract said data hence, learning the basic basics.

as far as W3Schools - for basics theyre fine.just my .02
If you do your homework, And can tackle what you need in an orderly fashion, in a few days you should be up n running.

Good luck.



Related Topics



Leave a reply



Submit