i am having problem of see who's online and don't have any idea how to see who's online.what i am doing is updating mysql database using time(); whenever user log in or refreshes the page.
Then, lastactivity gets filled with epochtime for eg : 1387806657 or 1287373415.
After that i select the lastactivity from database and use this condition but nothing happens.
<?
$result = mysql_query("SELECT lastactivity FROM users WHERE lastactivity!=0");
while($rahul = mysql_fetch_assoc($result)){
$last = $rahul['lastactivity'];
echo $last;
}
?>
</br>
<?
if ($result < time()-3600)
{
$hello = mysql_num_rows($result);
echo $hello;
}
else
{
echo "ERROR!";
}
?>
You could decide that any acticity in a 30 second interval from now means "online"
$early = time()-30;
$res = mysql_query("SELECT username,lastactivity FROM users WHERE lastactivity > $early") or die( mysql_error() );
while( $row = mysql_fetch_array($res) )
{
echo $row['username'].' is online.<br>';
}
How about going to the source and parsing or watching your Apache log to see which pages are being loaded?
Related
The php-code below finds and displays an external url as a link from a database. Sometimes there is a phonenumber instead of an url in that row. How can I manage to echo out the phonenumber without making it to a link. I guess it will work with if and else but I'm not finding out how to display it the right way. My code below:
<?php
$ticketurl = $row ['show_external_url'];
$query = mysqli_query( $connection,"SELECT *, DATE_FORMAT(`show_date`, '%W (%d/%m/%y)') as dateFormatted
FROM nhto_gigpress_shows
WHERE `show_date` >= CURDATE()
AND WEEKDAY(nhto_gigpress_shows.show_date) >= 5
ORDER By show_date" );
$row = mysqli_fetch_array( $query ); {
echo "<a href='".$ticketurl."'>BOOK</a>";
}
?>
Try to check whether the column has any data or not and use condition
inside the while loop:
if(empty($row['url'])){
echo $row['phone'];
}
else{
echo 'BOOK';
}
The code below a member and guest counter . The code runs perfectly however when I add a simple column (online) to the table active_members the entire code stops working (apart from the active_guests as this a separate table)
I have looked tirelessly trying to catch anything that may be causing this I have included the table screenshot below . I would to had add new column (online)that is a varchar to the table members_online and then query in the if SESSION below for that new column .
Why does the code stop working , Can somebody possible see the cause or solution?
As soon as i remove the online column is code runs perfect again .
$sqlt2 query below is the line I want to index the new online column .
include('..\db.php');
$con = mysqli_connect($dbsrvname, $dbusername, $dbpassword, $dbname);
//declare variables
$guest_timeout = time() -1 * 60;
$member_timeout = time() -1 * 60;
$guest_ip = $_SERVER['REMOTE_ADDR'];
$time = time();
// if the session is set PUTS THE USER IN MEMBERS TABLE
if(isset($_SESSION['CurrentUser'])){
//if user is logged in
$sqlt = mysqli_query($con,"DELETE FROM active_guests WHERE guest_ip='".$guest_ip."'");
**//I WANT TO ADD TO THE ONLINE COLUMN AND REPLACE A SIMPLE STRING INTO ONLINE**
$sqlt2 = mysqli_query($con,"REPLACE INTO active_members VALUES ('".$_SESSION['CurrentUser']."','".$time."')");
$name = $_SESSION['CurrentUser'] ;
}else{
//if user not in a session PUTS THE USER IN GUESTS TABLE
$sqlt3 = mysqli_query($con,"REPLACE INTO active_guests (guest_ip,time_visited)VALUES ('".$guest_ip."','".$time."')");
}
//execute querys
$sqlt4 = mysqli_query($con,"DELETE FROM active_guest WHERE time_visited < ".$guest_timeout);
$sqlt5 = mysqli_query($con,"DELETE FROM active_members WHERE time_visited < ".$member_timeout);
$sqlt6 = mysqli_query($con,"SELECT guest_ip FROM active_guests");
$sqlt7 = mysqli_query($con,"SELECT username FROM active_members");
$online_guests = mysqli_num_rows($sqlt6);
if(isset($_SESSION['CurrentUser'])){
$sqlt7 = mysqli_query($con,"SELECT username FROM active_members");
if($sqlt7->num_rows){
while($row = $sqlt7->fetch_object())
{
echo '<pre>',$row->username,'</pre>' ;
}
//$result->free();
}
}else{
echo "Login to see members-online list";
}
$online_members = mysqli_num_rows($sqlt7);
?>
// display results
<div class="container">
<p>_________________________</p>
<p>online Guests : <?php echo $online_guests ; ?></p>
<p>online Members : <?php echo $online_members ; ?></p>
</div>
Set column "online"s "Default/Expression" to null, hopefully it'll will work!
There are many questions asked about this problem but i suppose none of them could help me in my case. I will show you my login form , my main page, showing active users , and am little confused about starting lastTimeUpdate function to it.
Here is my main contetnt form, the problem is that there is a list of active users if user is active his name is displayed if not - not displayed, but if user exits browser, session destroys, but doesnt log out:
<?php
include 'config.php';
//include 'logout.php';
$query = "SELECT * FROM userinfo WHERE `ifactive` = 1";
if(isset($_SESSION['uname'])){
$result = mysql_query($query);
echo "<div id=maincontenttopleft>
<p>Active users</p>";
echo "<div id=acuser><table id=activeusers>";
while($row = mysql_fetch_array($result)){
echo "</td><td>" . $row['name'] . "</td></tr>";
}
echo "</table></div>";
mysql_close();
}
//if (session_destroy()) {
//mysql_query("SELECT * FROM `userinfo` WHERE `ifactive` = 1 AND `uname` !=
'$_SESSION[uname]'");
//mysql_query("UPDATE `userinfo` SET `ifactive` = 0 WHERE `uname` !=
'$_SESSION[uname]'") or die(mysql_error());
//}
if (!isset($_SESSION['uname'])) {
$_SESSION['uname'] = time();
} else if (time() - $_SESSION['uname'] > 10) {
// session started more than 30 minutes ago
//mysql_query("SELECT * FROM `userinfo` WHERE `ifactive` = 1 AND `uname` =
'$_SESSION[uname]'");
//mysql_query("UPDATE `userinfo` SET `ifactive` = 0 WHERE `uname` =
'$_SESSION[uname]'") or die(mysql_error());
session_regenerate_id(true); // change session ID for the current session an
invalidate old session ID
$_SESSION['uname'] = time(); // update creation time
}
?>
I want to check if there is a destroyed session, to take the name of this session end according to the name to set my boolean (ifactive) to zero. Can you help me about the logic of it.
You'll then need to save the session_id of every user in the database.
And then inside a cron for instance, have a script checking the existence of the file associated to each session.
Here is how you find your sessions folder if you don't want to mess in your config files :
<?php
echo session_save_path ();
?>
Your cron script could then save the results in a txt file which would be easier for your website script to read (or better yet, save it in memory so it's faster - checkout redis).
so here is my code and I will tell you the problem after that:
<script language='javascript' type='text/javascript'>
setInterval( function() {
$('#responsechat<?php echo $otherchatuser ?>').load('echogetconversation.php?username=<?php echo $username; ?>&otherchatuser=<?php echo $otherchatuser; ?>&numberofmessages=<?php echo $numberofmessages; ?>');
<?php
$subtractlogintime8 = time() - 600;
$data8 = mysql_query("SELECT * FROM loggedin WHERE username='$otherchatuser' and time > '$subtractlogintime'");
$numrows8 = mysql_num_rows($data8);
?>
if (<?php echo $numrows8; ?> == 1 )
{
document.getElementById("checkloggedin<?php echo $otherchatuser; ?>").innerHTML = '<img src="loggedin.png">';
document.getElementById("checkloggedin<?php echo $otherchatuser; ?>").style.marginLeft = '5px';
}
else
{
document.getElementById("checkloggedin<?php echo $otherchatuser; ?>").innerHTML = '';
}
}, 4000);
</script>
This is my code, which gets users who are logged in and also does other things. The problem that I am having occurs on this line:
$data8 = mysql_query("SELECT * FROM loggedin WHERE username='$otherchatuser' and time > '$subtractlogintime'");
The query successfully gets users from the database, but seems to be ignoring the "and time > '$subtractlogintime'");" part of the query. I have no idea why this is occurring and anyone who could possibly tell me what I have forgotten would be extremely appreciated. Thanks.
TIME is a reserved keyword in MySQL because of the TIME datatype. If it's not crashing, you would probably better off backticking your "TIME" keyword to refer to the field time.
$data8 = mysql_query("SELECT * FROM loggedin WHERE username='$otherchatuser' and `time` > '$subtractlogintime'");
Also note that, this code COULD be prone to SQL injection but we can't be sure without seeing the full code...
i get this error line on this php file . can someone locate where is the error ?
-------------You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1----------
i have this page for votes to users but if i vote in one user this vote goes to all users . how can i make this code when voting this vote goes only to its user .
--
// Connects to your Database
mysql_connect("localhost", "dbusername", "dbpassword") or die(mysql_error());
mysql_select_db("mydatabase") or die(mysql_error());
//We only run this code if the user has just clicked a voting link
if ( $mode=="vote")
{
//If the user has already voted on the particular thing, we do not allow them to vote again
//$cookie = "Mysite$id";
if(isset($_COOKIE[$cookie]))
{
Echo "Sorry You have already ranked that site <p>";
}
//Otherwise, we set a cooking telling us they have now voted
else
{
$month = 2592000 + time();
setcookie('Mysite'.$id, 'Voted', $month);
//Then we update the voting information by adding 1 to the total votes and adding their vote (1,2,3,etc) to the total rating
mysql_query ("UPDATE userads SET total = total+$voted, votes = votes+1 WHERE id = $id");
}
}
if ( $mode2=="vote")
{
//If the user has already voted on the particular thing, we do not allow them to vote again
//$cookie = "Mysite$id";
if(isset($_COOKIE[$cookie]))
{
Echo "Sorry You have already ranked that site <p>";
}
//Otherwise, we set a cooking telling us they have now voted
else
{
$month = 2592000 + time();
setcookie('Mysite'.$id, 'Voted', $month);
//Then we update the voting information by adding 1 to the total votes and adding their vote (1,2,3,etc) to the total rating
mysql_query ("UPDATE userads SET total = total+$voted, nvotes = nvotes+1 WHERE id = $id");
}
}
//Puts SQL Data into an array
$data = mysql_query("SELECT * FROM userads WHERE id = $id ") or die(mysql_error());
//Now we loop through all the data
while($ratings = mysql_fetch_array( $data ))
?>
<link href="style.css" type="text/css" rel="stylesheet" />
{
<?php
echo '<div id="voting_14" class="voting voting_template_votess-up-down">';
echo "<strong class='positive_votes'>";
$current = $ratings[votes];
echo "<span>+" . round($current,0) . "</span>";
echo " <input class='vote_positive' type='submit'>";
echo '</strong>';
echo "<strong class='negative_votes'>";
$current2 = $ratings[nvotes];
echo " <input class='vote_negative' type='submit'>";
echo "<span>-". round($current2,0) ."</span>";
echo '</strong>';
echo '</div>';
}
---the end
i have sql table userads with : id , name , username , total, votes , nvotes.
Correct your code to following,
setcookie('Mysite'.$id, 'Voted', $month); // ERROR 1
and
while($ratings = mysql_fetch_array( $data ))
{ // ERROR 2
?>
I copy pasted your code in a file and ran:
php -l your_script.php
Yields:
Parse error: syntax error, unexpected '}' in your_script.php on line 78
So, that last bracket } at the very end is causing a parse error. Either that or you didn't post the matching if/while/etc. in your post and the problem is elsewhere.
You have a syntax error. Probably your configuration doesn't display errors and you get a blank screen.
You are missing quotes here:
setcookie(Mysite.$id, Voted, $month);
It should be:
setcookie('Mysite'.$id, 'Voted', $month);
Apparently something is wrong with your query. You can check your SQL query with a simple:
$sql = "SELECT * FROM userads WHERE id = $id ";
echo $sql;
My first guess is, that $id is not set properly.