Jump to content

How a counter of users ? such as ---> (231 Viewing)


Recommended Posts

ashton321

 

I have the following error after I have applied the last link to the useronline counter you have gave me can you help me on this please

 

 

Notice: Undefined variable: REMOTE_ADDR in C:\wamp\www\nyhungry\includes\header.php on line 50

 

Notice: Undefined variable: PHP_SELF in C:\wamp\www\nyhungry\includes\header.php on line 50

Useronline Insert Failed > Useronline Delete Failed >

Notice: Undefined variable: PHP_SELF in C:\wamp\www\nyhungry\includes\header.php on line 62

Useronline Select Error >

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\nyhungry\includes\header.php on line 68

users online //

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link type="text/css" href="stylesheets/restaurantwebpageprueba.css" rel="stylesheet" media="all" />
<title>www.Nyhungry.com</title>
<script type="text/javascript" src="scripts/prototype.js"></script>
<script type="text/javascript" src="scripts/rating.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
(
function()
{
  var default_image = $('td.largethumb img').attr('src');
  $('table.smallthumbs a').mouseover(function() { $('td.largethumb img').attr('src', $('img', this).attr('src')); });
});
</script>
</head>


<body>

<?php require_once("includes/connection.php");?>

<div id="right">
<?php 
$timestamp = time(); 
$timeout = $timestamp-$connection;

$insert = mysql_db_query($connection, "INSERT INTO useronline VALUES
('$timestamp','$REMOTE_ADDR','$PHP_SELF')"); 
if(!($insert)) { 
print "Useronline Insert Failed > "; 
} 

//delete values when they leave
$delete = mysql_db_query($connection, "DELETE FROM useronline WHERE timestamp<$timeout"); 
if(!($delete)) { 
print "Useronline Delete Failed > "; 
} 

//grab the results
$result = mysql_db_query($connection, "SELECT DISTINCT ip FROM useronline WHERE file='$PHP_SELF'"); 
if(!($result)) { 
print "Useronline Select Error > "; 
} 

//number of rows = the number of people online
$user = mysql_num_rows($result); 


//spit out the results
mysql_close(); 
if($user == 1) { 
print("$user user online\n"); 
} else { 
print("$user users online\n"); 
} 


?>

 

Connection.php

 

<?php require("constant.php");
$connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS,DB_TIME);
if(!$connection){
die("Database connection failed:" . mysql_error());
}
mysql_set_charset('utf8',$connection);
$db_select = mysql_select_db(DB_NAME, $connection);
if(!$db_select){
die("Database selection failed: " . mysql_error());
}

function GetCartId()
{
// This function will generate an encrypted string and
// will set it as a cookie using set_cookie. This will
// also be used as the cookieId field in the cart table

if(isset($_COOKIE["cartId"]))
{
return $_COOKIE["cartId"];
}
else
{
// There is no cookie set. We will set the cookie
// and return the value of the users session ID

session_start();
setcookie("cartId", session_id(), time() + ((3600 * 24) * 30));
return session_id();
}
}

?>
?>

constant.php

 

<?php

// Database Constants
defined('DB_SERVER') ? null : define("DB_SERVER", "localhost");
defined('DB_USER')   ? null : define("DB_USER", "root");
defined('DB_PASS')   ? null : define("DB_PASS", "treful");
defined('DB_NAME')   ? null : define("DB_NAME", "lala");
defined('DB_TIME')   ? null : define("DB_TIME",  300 );

?>

 

 

I have done the connection inside the script since I have a connection.php file and I just require once. But I don'w know if I should go ahead and  make the connection for this particular file.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link type="text/css" href="stylesheets/restaurantwebpageprueba.css" rel="stylesheet" media="all" />
<title>www.Nyhungry.com</title>
<script type="text/javascript" src="scripts/prototype.js"></script>
<script type="text/javascript" src="scripts/rating.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
(
function()
{
  var default_image = $('td.largethumb img').attr('src');
  $('table.smallthumbs a').mouseover(function() { $('td.largethumb img').attr('src', $('img', this).attr('src')); });
});
</script>
</head>


<body>



<div id="sidebar1">
<ul id="mainNav">
      <li id="restaurants"><a href="restaurants/index.php" title="All restaurants inside nyhungry" class="first">Home</a></li>
                <li id="takeouts"><a href="../../Take Outs/Take Outs frontpage.html" title="Information about all take Outs">Hungry for Music!</a></li>
              <li id="pizzerias"><a href="../../Pizzeria/Pizzeria frontpage.html" title="All the pizzerias inside nyhungry">Contact us</a></li>
                 <!--<li id="fishmarkets"><a href="../../Fish Markets/Fish Market frontpage.html" title="All the fishmarkets inside nyhungry">Fish Markets</a></li>-->
  </ul>
<!-- end #sidebar1 --></div>
<!--<table id="restaurantview">

<a href=""><td width="40" id="contenido1">tropiezo></td></a>
<a href=""><td width="40" id="contenido2">colozar1></td></a>
<a href=""><td width="40" id="contenido3">colozar2></td></a>
<a href=""><td width="40" id="contenido4">colozar3></td></a> 
<a href=""><td width="40" id="contenido4">colozar4></td></a>
<a href=""><td width="40" id="contenido4">colozar3</td></a>
</table>-->

<div id="right">
<?php 
//fill in some basic info
$server = "localhost"; 
$db_user = "root"; 
$db_pass = "felopw"; 
$database = "quero"; 
$timeoutseconds = 300; 

//get the time
$timestamp = time(); 
$timeout = $timestamp-$timeoutseconds; 

//connect to database
mysql_connect($server, $db_user, $db_pass); 

//insert the values
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
('$timestamp',$REMOTE_ADDR['REMOTE_ADDR'],$_SERVER['PHP_SELF']"); 
if(!($insert)) { 
print "Useronline Insert Failed > "; 
} 

//delete values when they leave
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout"); 
if(!($delete)) { 
print "Useronline Delete Failed > "; 
} 

//grab the results
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='$PHP_SELF'"); 
if(!($result)) { 
print "Useronline Select Error > "; 
} 

//number of rows = the number of people online
$user = mysql_num_rows($result); 


//spit out the results
mysql_close(); 
if($user == 1) { 
print("$user Hungry online\n"); 
} else { 
print("$user users online"); 
} 
?>


</div>

In this version I have done the connection inside the header.php file instead of require_once the connection.

first, make the MySql database:-

 

<?php

mysql_connect('localhost','root','');

mysql_select_db(online);

mysql_query("CREATE TABLE `useronline` ( `timestamp` int(15) NOT NULL default '0', `ip` varchar(40) NOT NULL default '', `file` varchar(100) NOT NULL default '', PRIMARY KEY (`timestamp`), KEY `ip` (`ip`), KEY `file` (`file`) ) TYPE=MyISAM;");

?>

Run this script from the browser!!!

 

 

 

second, save this as a file:-

 

<?php

// Configuration

$dbhost = "localhost";

$dbuser = "root"; // MySQL Username

$dbpass = ""; // MySQL Password

$dbname = "online"; // Database Name

$timeoutseconds = 1200; // length of session, 20 minutes is the standard

     

$timestamp=time();

$timeout=$timestamp-$timeoutseconds;

$ip = substr($REMOTE_ADDR, 0, strrpos($REMOTE_ADDR,"."));

 

// Connect to MySQL Database

@mysql_connect($dbhost,$dbuser,$dbpass);

@mysql_select_db($dbname) or die("No database, Chris!");

 

// Add this user to database

$loopcap = 0;

while ($loopcap<3 && @mysql_query("insert into useronline values('$timestamp','$ip','$PHP_SELF')"))

{ // in case of collision

$timestamp = $timestamp+$ip{0}; $loopcap++;

}

 

// Delete users that have been online for more then "$timeoutseconds" seconds

@mysql_query("delete from useronline where timestamp<$timeout");

 

// Select users online

$result = @mysql_query("select distinct ip from useronline");

$user = @mysql_num_rows($result);

 

mysql_free_result($result);

@mysql_close();

 

// Show all users online

if ($user==1) {echo '<b> There is<br />';

echo $user.'<br />visitor viewing the site.</B>';

} else {

echo '<b>There are<br />', $user.'<br />visitors viewing the site.</B>';

}

?>

 

third, link to the file via your web page:-

 

<?php

if

(file_exists('../cgi-bin/data/mpage/1.php'))

          {

                                    include'../cgi-bin/data/mpage/1.php';

                                    }else{ echo "?"; }

                                    ?>

 

NOTICE i put it in the cgi-bin for safety

 

Howzat!!

The first script the script below should be saved in a different file chouffer?

 

or that's the actual creation of the database doesn't look like that's why I come with that question.

<?php
mysql_connect('localhost','root','');
mysql_select_db(online);
mysql_query("CREATE TABLE `useronline` ( `timestamp` int(15) NOT NULL default '0', `ip` varchar(40) NOT NULL default '', `file` varchar(100) NOT NULL default '', PRIMARY KEY (`timestamp`), KEY `ip` (`ip`), KEY `file` (`file`) ) TYPE=MyISAM;");
?>

 

 

I have the variables defined as you guys has given me and I have place into the code, But there is a parse error in line 65

 

DEFINE ('REMOTE_ADDR', $_server['remote_addr']);
DEFINE ('PHP_SELF', $_server['php_self']);

//get the time
$timestamp = time(); 
$timeout = $timestamp-$timeoutseconds; 

//connect to database
mysql_connect($server, $db_user, $db_pass); 

//insert the values
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
('$timestamp','$_server['remote_addr'],'$_server['php_self']'"); //Line 65 where parse error accurs Help!!!
if(!($insert)) { 
print "Useronline Insert Failed > "; 
}

Why do you put curly braces here?

 

Thank you maq

 

Sorry, the second terminating curly brace is in the wrong place.  It should be:

 

('$timestamp','{$_server['remote_addr']}','{$_server['php_self']}'");

 

The reason is in my previous post.

Now there is a parse error at the query

 

i have tried to put in all differnet ways i know but nothing

 

thank you for the former advice it has stop the parse error message from displaying

 

 

Parse error: parse error, expecting `'}'' in C:\wamp\www\nyhungry\includes\header.php on line 77

 

//grab the results
$result = mysql_db_query($database, 
"SELECT DISTINCT ip FROM useronline WHERE file=$_server ['php_self']"); //line 77 
if(!($result)) { 
print "Useronline Select Error > "; 
}

 

 

Below are the ways I have tried but none of them work....

 

 

FROM useronline WHERE file='$_server ['php_self']' "); 

 

FROM useronline WHERE file='{$_server ['php_self']}'"); 

 

FROM useronline WHERE file='{$_server ['php_self']'} "); 

 

Any other suggestion !

Firstly, that method has been deprecated as of PHP 5.3 and shouldn't be used or relied upon.  Second, I recommend reading a basic tutorial on SELECT statements, so you can properly rewrite your code:

 

http://www.w3schools.com/PHP/php_mysql_select.asp

I fixed it already thank you!!

 

 

file='{$_server['php_self']}'"

 

have the header.php script included in index.php. Header.php contain the whole script we have been working here which is

 

header.php

 

PHP]

//fill in some basic info
$server = "localhost"; 
$db_user = "username"; 
$db_pass = "password"; 
$database = "users"; 
$timeoutseconds = 300; 

//get the time
$timestamp = time(); 
$timeout = $timestamp-$timeoutseconds; 

//connect to database
mysql_connect($server, $db_user, $db_pass); 

$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
('$timestamp','{$_server['remote_addr']}','{$_server['php_self']}'"); 
if(!($insert)) { 
print "Useronline Insert Failed > "; 
} 

//delete values when they leave
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout"); 
if(!($delete)) { 
print "Useronline Delete Failed > "; 
} 

//grab the results
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='{$_server['php_self']}'"); 
if(!($result)) { 
print "Useronline Select Error > "; 
} 

//number of rows = the number of people online
$user = mysql_num_rows($result); 


//spit out the results
mysql_close(); 
if($user == 1) { 
print("$user user online\n"); 
} else { 
print("$user users online\n"); 
} 
?>

 

We have fixed the header.php but when i open index.php which header is included it display the following error messages. that's after all the fixed we have done it keeps throwing message. The script apparently is working, becuase below the messabes it says as the quote below

 

0 users online

 

This is index.php

<?php include("includes/header.php");?>

//Rest of the code... 

 

When index.php is open which header.php is included in is spit the following error messages

 

Notice: Undefined variable: _server in C:\wamp\www\nyhungry\includes\header.php on line 53

 

Notice: Undefined variable: _server in C:\wamp\www\nyhungry\includes\header.php on line 54

 

Notice: Undefined variable: _server in C:\wamp\www\nyhungry\includes\header.php on line 65

 

Notice: Undefined variable: _server in C:\wamp\www\nyhungry\includes\header.php on line 65

Useronline Insert Failed >

Notice: Undefined variable: _server in C:\wamp\www\nyhungry\includes\header.php on line 77

0 users online

?>

 

 

But i as put in the quote above

 

0 user online

 

below all the 5 notices 0 user online is displaying as an indication that the script is working but why all this warning and notices still displaying?

Thank you I got that the Global Variables can not be lower cases thank you.. Need to learn more about parsing do you know of a good tutorial about parsing...

 

Because the curly braces did the job here..

 

One more thing is not inserting why?

 

 

Useronline Insert Failed > 0 users online

?>

 

the whole code above is working now with all the fixes we have done...

 

the final draft come to this

 

PHP]

//fill in some basic info
$server = "localhost"; 
$db_user = "username"; 
$db_pass = "password"; 
$database = "users"; 
$timeoutseconds = 300; 

DEFINE ('REMOTE_ADDR', $_SERVER['REMOTE_ADDR']);
DEFINE ('PHP_SELF', $_SERVER['PHP_SELF']);

//get the time
$timestamp = time(); 
$timeout = $timestamp-$timeoutseconds; 

//connect to database
mysql_connect($server, $db_user, $db_pass); 

//insert the values
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
('$timestamp','{$_SERVER['REMOTE_ADDR']}','{$_SERVER['PHP_SELF']}'"); 
if(!($insert)) { 
print "Useronline Insert Failed > "; 
} 

//delete values when they leave
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout"); 
if(!($delete)) { 
print "Useronline Delete Failed > "; 
} 

//grab the results
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='{$_SERVER['PHP_SELF']}'"); 
if(!($result)) { 
print "Useronline Select Error > "; 
} 

//number of rows = the number of people online
$user = mysql_num_rows($result); 


//spit out the results
mysql_close(); 
if($user == 1) { 
print("$user Hungry online\n"); 
} else { 
print("$user users online"); 
} 
?>

 

 

This is the insertion point:

 

//insert the values
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
('$timestamp','{$_SERVER['REMOTE_ADDR']}','{$_SERVER['PHP_SELF']}'"); 
if(!($insert)) { 
print "Useronline Insert Failed > "; 

 

here at the Insert point if mysql_db_query is not true then it will print "Useronline Insert Failed...

 

 

Now one of the things I have is that I open an connection in header.php

 

//fill in some basic info
$server = "localhost"; 
$db_user = "username"; 
$db_pass = "password"; 
$database = "users"; 
$timeoutseconds = 300;

 

thenk I include header.php - with a connection to the localhost already-  I include it in index.php

 

Index.php has a connection to the same database as well so now we have two connection I know it is unnecessary having two connection but because of my lack of ability to adapt the tutorial connection to the one i have already i haven't done it.

 

$timeoutseconds = 300; variable and it's numeric value has terrified me to try..

 

Well I have tried doing this.

 

Index.php

 

<?php include("includes/header.php");?>
<?php require_once("includes/connection.php");?>

 

Remember header.php has a connection  pluse the connection.php required in index.php makes it two

 

The connection.php looks like this.

 

<?php require("constant.php");
$connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
if(!$connection){
die("Database connection failed:" . mysql_error());
}
mysql_set_charset('utf8',$connection);
$db_select = mysql_select_db(DB_NAME, $connection);
if(!$db_select){
die("Database selection failed: " . mysql_error());
}

 

 

Costant.php is

 

<?php

// Database Constants
defined('DB_SERVER') ? null : define("DB_SERVER", "localhost");
defined('DB_USER')   ? null : define("DB_USER", "username");
defined('DB_PASS')   ? null : define("DB_PASS", "password");
defined('DB_NAME')   ? null : define("DB_NAME", "ohr");

?>

 

 

Still the connection is failing I don't know what could be the cause...

replace your current $insert with this:

 

$insert = mysql_query("INSERT INTO useronline VALUES ('$timestamp','{$_SERVER['REMOTE_ADDR']}','{$_SERVER['PHP_SELF']}'") or die (mysql_error());

 

as Maq said, mysql_db_query() has been issued 'deprecated' as of v5.3, and removed as of v6, so just stick with mysql_query() instead.

 

in your useronline table, what are the fields within .. please post them here.

Now I see that this script was made a long time for former php versions

 

 

I have put mysql_query instead of the older function... but Now it is spiting other errors and notices

 

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\nyhungry\includes\header.php on line 65

Useronline Insert Failed >

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\nyhungry\includes\header.php on line 71

Useronline Delete Failed >

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\nyhungry\includes\header.php on line 77

Useronline Select Error >

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\nyhungry\includes\header.php on line 83

users online

?>

 

Below is the Table dump.

 

CREATE TABLE useronline (

  timestamp int(15) DEFAULT '0' NOT NULL,

  ip varchar(40) NOT NULL,

  file varchar(100) NOT NULL,

  PRIMARY KEY (timestamp),

  KEY ip (ip),

  KEY file (file)

);

since this script was build for an older version of php is the if statement here needed?

 

if(!($delete)) { 
print "Useronline Delete Failed > "; 
} 

 

after puting mysql_error fuction here

 

$result = mysql_query( "SELECT DISTINCT ip FROM useronline WHERE file='{$_SERVER['PHP_SELF']}'") or die (mysql_error());

I have taken out the or die (mysql_erro))); function now after that it says that mysql_query function has slupplied argument is not a valid mysql link resource line 65, it means that mysql is not compatible with the way we have code the query at line 65

 

Line 65 below!..

 

$insert = mysql_query($database, "INSERT INTO useronline VALUES
('$timestamp','{$_SERVER['REMOTE_ADDR']}','{$_SERVER['PHP_SELF']}'"); 

 

The $database variable right? then the argument would be after the INSERT INTÖ right?

 

what is wrong then?

 

maybe the ['REMOTE_ADDR']} should be lower case?

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\nyhungry\includes\header.php on line 65

Useronline Insert Failed >

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\nyhungry\includes\header.php on line 72

Useronline Delete Failed >

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\nyhungry\includes\header.php on line 78

Useronline Select Error >

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\nyhungry\includes\header.php on line 84

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.