I am trying to write a PHP script that will count the amount of users that have visited the page within the last 10 minutes. This is my script right now:
function getOnlineUsers($database, $main_connection){
$database;
$visitor_id = session_id();
$timestamp = time();
$timeOut = $timestamp - 6000;
mysql_query("INSERT INTO online (m_time, ip) VALUES ('$timestamp', '$visitor_id')", $main_connection);
mysql_query("DELETE FROM online WHERE m_time < $timeOut");
$result = mysql_query("SELECT * FROM online");
mysql_fetch_assoc($result);
if(!$result){
$online_users = 1;
}else{
$online_users = mysql_num_rows($result);
}
return $online_users+1;
}
The problem is that nothing is being inserted into the database and the database remains empty and therefore the count is null. Can someone please assist me in this?
First, better use PDO or MySQLi. In your database, the 'm_time' column must be integer type, and pass the $timestamp value as number, not within quotes.
"INSERT INTO online (m_time, ip) VALUES ($timestamp, '$visitor_id')"
1) Change '$timestamp' to NOW(), mysql can't understand php's time() function ( assuming m_time is a datetime field) .
ie:
INSERT INTO online (m_time, ip)
VALUES ( NOW(), '$visitor_id')
2) Your delete suffers the same problem
3) You can use something a little more clever to get the users in the last 10 minutes, try something like:
select count(*) AS OnlineUserCount
from online
WHERE
m_time > DATE_SUB( NOW(), INTERVAL 10 MINUTE ) ;
Your code looks fine .. what i suggest you do is echo out the query when it runs and cope and paste it and run it directly from phpmyadmin and check if it gives you any error and if the row is inserted succesfully i suggest you check your connection file with the database.
And also try what's suggested by CoursesWeb
Related
As the title says.
From the database, in the resultset, I want those rows where the date and time (schedule column) are already passed.
In the database I have
On the PHP page I have:
$now = time();
$result = $mysqli->query("SELECT * FROM schedules WHERE $now >= UNIX_TIMESTAMP(schedule)");
However, if I test this on 2015-09-19 at 18:50:00, I get no results at all, while instead I should only get the row with ID 39.
As much as I know I am comparing two valid timestamps - the one generated by time() and the one converted from the database with UNIX_TIMESTAMP() - so what isn't working?
EDIT: I already tried to do
$result = $mysqli->query("SELECT * FROM schedules WHERE NOW() >= schedule");
But with no luck. I am getting the results with
while ($row = $result->fetch_assoc()) {
$id = $row['ID'];
echo $id;
}
Use now() to select rows where schedule is in the past
SELECT * FROM schedules WHERE now() > schedule
They are different because,
php time is based on the php timezone setting (inside php.ini).
database time is based on your system time (unless you set it).
I have MySQL table:
and I want to add next row (from script in page) with values:
ip: 178.40.12.36
time: 2014-01-22 14:08:04
browser: Google Chrome
browser_version: 32.0.1700.76
platform: windows
country: Slovakia
Question: How to determine in mysql query to insert only if last insert with same identificator (IP+browser+platform) was 30min ago ?
My current insert (pseudo code):
$exist = SELECT *
FROM table
WHERE ip = $ip
AND browser = $browser
AND platform = $platform
if(!$exist) {
INSERT INTO table ...
}
My Idea:
$exist = SELECT ...
...
AND time < $time - 30MIN
Note: How to write this in MySQL?
You may use this as indicator:
SELECT
COUNT(1)
FROM `t`
WHERE `ip` = '$ip'
AND `browser` = '$browser'
AND `platform` = '$platform'
AND `time`>NOW() - INTERVAL 30 MINUTE
-I've replaced time with NOW() for current time, but you may wish to count from your last time value.
It will select records what are newer than 30 minutes, thus, if it's positive, then you don't need to insert new row(s).
Yes, it's easy.
AND time > NOW() - INTERVAL 30 MINUTE
There are many choices like this for date arithmetic.
You could just filter the SELECT for the INSERT:
INSERT INTO `Table` ( ... )
SELECT $ip, $time, $browser, $browser_version, $platform, $country
FROM `Other_Table`
WHERE ip = $ip AND browser = $browser AND platform = $platform AND
time < $time - 30MIN
Now, clearly that syntax won't work exactly, but you get the idea. If the time isn't 30MIN or more ago then it will return 0 records to INSERT.
This will avoid the need of performing a COUNT or EXISTS first; it can be done in one statement.
i am having problem showing how many users are online using timestamp function. what i am doing is storing last active time in 'lastactivity' database column using time(); function and updating it whenever user refreshes the page. here's code for showing lastactivity :-
<?
$result = mysql_query("SELECT * FROM users");
while($array = mysql_fetch_array($result)){
$last = $array['lastactivity'];
echo $last;
}
?>
i am just confused what condition to use with 'if' if possible to seperate out those who are not active for more than 5 minutes.
MySQL can do it faster:
select * from users where timediff( now(), lastactivity ) > "00:05:00";
I need some more help with a script. I have pieced together the following from several examples but I'm falling short when it comes to comparing the results and the IP address and timestamp. As it stands, I'm trying to redirect a user to another page if they have accessed a certain area on my site for more then 10 minutes.
My MySQL table consists of the following columns:
ID, client_ip, submitted_time
Here's my current code:
<?php
$ipaddress = $_SERVER["REMOTE_ADDR"];
$link=mysqli_connect("localhost","root","password","visitors");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "SELECT count(`id`) AS 'count' FROM `visits` WHERE `client_ip` = '$ipaddress'
AND 'submitted_time' = CURRENT_TIMESTAMP - INTERVAL 10 MINUTE
LIMIT 1";
$result = mysql_query($link, $query);
if ($result['count'] > 0) {
echo "You have accessed this site in the last 10 minutes -- you are blocked!";}
else
{
echo "You have access";
exit;
}
?>
I should be getting the "You have accessed this site in the last 10 minutes echo..... But I'm getting You have access everytime. Any ideas on what I'm doing wrong based on my current code? Thanks in advance!
if you are checking whether they have accessed within the last 10 minutes you could use between
$query = "SELECT count(id) AS 'count' FROM visits WHERE client_ip = '$ipaddress'
AND 'submitted_time' BETWEEN CURRENT_TIMESTAMP - INTERVAL 10 MINUTE AND CURRENT_TIMESTAMP
although epends on the specifics of how you're trying to do it
Information about your schema & data type is insufficient. assuming submitted_time is DATETIME or TIMESTAMP,
you have two problem.
submitted_time is a column, so remove single quote, or use backtick.
use <= rather than =, user can revisit over 10+ minutes
LIMIT 1 is not required. COUNT(*) always return 1 record. but this is not problem..
below query would work well:
SELECT count(`id`) AS 'count'
FROM `visits`
WHERE `client_ip` = '$ipaddress'
AND `submitted_time` = CURRENT_TIMESTAMP - INTERVAL 10 MINUTE
I need to select data from a mysql database from the past 12 months based on the current date. I have the dates saved in the database as unix timestamps but my query does not seem to work.
$query="SELECT user_id, COUNT(first_name) AS member_count
FROM main_user
WHERE renew_date<'$time' AND renew_date>'$old_time' WHERE renew_date!=''";
Basically I need to count all instances of first_name where there is a renew_date timestamp.
You have an additional WHERE where you should use AND:
$query="SELECT user_id, COUNT(first_name) AS member_count
FROM main_user
WHERE renew_date<'$time' AND renew_date>'$old_time' AND renew_date!=''";
^^^
You have an error in your query, you have two WHERE clauses!
You can find this and other errors, when you test the return value from your query
$query = 'select ...';
$result = $mysqli->query($query);
if ($result === false) {
// error handling
echo $mysqli->error;
} else {
// query successful
// process result set
}
You put WHERE twice. You can use From_UNIXTIME function in mysql
WHERE FROM_UNIXTIME(renew_date)<NOW()
AND FROM_UNIXTIME(renew_date)> (NOW()-INTERVAL 1 year)
AND renew_date !=''