Undefined variables $PHP_SELF help!

i am modifying a script that catches how many users are online in that corrent web page.

Well I am getting this three errors the script i am using is below the error messages can anyone tell me how can i defined this variables, I am using PHP 5 in case these variables has changed or didn’t exist in former PHP versions

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

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

Notice: Undefined variable: PHP_SELF in C:\wamp\www
yhungry\includes\header.php on line 74
1 Hungry online //

PHP Script:


<?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 the values
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
('$timestamp','$REMOTE_ADDR','$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 user online\
");
} else {
print("$user users online\
");
}
?>
 

Thank you in advance…

Gah…Sitepoint it putting it all lower case :frowning:

$_server['remote_addr']
$_server['php_self']

i think u r missing whole part of tutorials …i have not seen tutorials but may be that tutorial might have said to initilize those two CONSTANT somewhere in passage form which u missed…
REMOTE_ADDR constant itself doesnt have any special meaning unless used with some SUPER varibales like $_SERVER
so me be
DEFINE (‘REMOTE_ADDR’, $_server[‘remote_addr’]);
DEFINE (‘PHP_SELF’, $_server[‘php_self’]);
will help

$PHP_SELF doesnt many any sense, unless you do the following:

foreach($_SERVER as $key => $val){
$$key = $val;
}

Now you can use $PHP_SELF as an alias of $_SERVER[‘PHP_SELF’].

Thanks

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 > "; 
}

guys I fixed it already thank you!!

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

One more thing

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

header.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\
"); 
} else { 
print("$user users online\
"); 
} 
?>


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
yhungry\includes\header.php on line 53

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

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

Notice: Undefined variable: _server in C:\wamp\www
yhungry\includes\header.php on line 65
Useronline Insert Failed >
Notice: Undefined variable: _server in C:\wamp\www
yhungry\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




//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\
");
} 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…

ok i am not sure why r u hard coding all things in header.php and again making file called connection
better make connection file and then may be include connection in header
may be connection like this


DEFINE ('DB_USER', 'username');
DEFINE ('DB_PASSWORD', 'password');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'dbname');


if ($dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD)) { // Make the connnection.

	if (!mysql_select_db (DB_NAME)) { // If it can't select the database.
	
		// Handle the error.
error_handler (mysql_errno(), 'Could not select the database: ' . mysql_error());
//just echo message	
				exit();
		
	} // End of mysql_select_db IF.
	
} else { // If it couldn't connect to MySQL.

error_handler (mysql_errno(), 'Could not connect to the database: ' . mysql_error());
	exit();
	
} // End of $dbc IF.



I am just working with header.php

When i open header.php alone which is below it will still display the same errors…

Uch…

Your connection set up you have done in your last post is included here but still it display

Useronline Insert Failed > Useronline Delete Failed > Useronline Select Error >
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www
yhungry\includes\header.php on line 104
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/webpageprueba.css" rel="stylesheet" media="all" />
<title>store.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="right">
<?php
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', 'feros');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'db_name');


if ($dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD)) { // Make the connnection.

	if (!mysql_select_db (DB_NAME)) { // If it can't select the database.
	
		// Handle the error.
error_handler (mysql_errno(), 'Could not select the database: ' . mysql_error());
//just echo message	
				exit();
		
	} // End of mysql_select_db IF.
	
} else { // If it couldn't connect to MySQL.

error_handler (mysql_errno(), 'Could not connect to the database: ' . mysql_error());
	exit();
	
} // End of $dbc IF.





//fill in some basic info

$timeoutseconds = 300;

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

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

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


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

//grab the results
$result = mysql_query("SELECT DISTINCT ip FROM useronline WHERE file='{$_SERVER['PHP_SELF']}'" . mysql_error());
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\
");
} else {
print("$user users online");
}
?>


</div>

</body>
</html>

by the way this is the link to the tutorial

http://www.spoono.com/php/tutorials/tutorial.php?id=16

the sql database as in the tutorial is as below

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)
);

but in mysql some how I don konw how to put
KEY ip (ip)
KEY file (file)

but the actual database dump I have in phpmyadmin is as:

CREATE TABLE IF NOT EXISTS useronline (
timestamp int(15) NOT NULL,
IP varchar(40) NOT NULL,
file varchar(100) NOT NULL,
PRIMARY KEY (timestamp)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

How can I re-do the database so it contain:

KEY ip (ip)
KEY file (file)

That could be one of the issues.
[/quote]

ok i have found that tutorial use much old function
rather try this
http://www.phpeasystep.com/phptu/9.html

i have tested this once and i worked at that time and may be work now as well