Page 1 of 1
Update sql problem
Posted: Thu Aug 07, 2003 5:46 pm
by GK
hi i have this script
it gives no errors but doesn;t save the info to sql
any idea whats wrong?
Code: Select all
<?
include "config.php"
$ip = 'unknown';
if (getenv('HTTP_CLIENT_IP'))
{
$ip = getenv('HTTP_CLIENT_IP');
}
else if (getenv('HTTP_X_FORWARDED_FOR'))
{
$ip = getenv('HTTP_X_FORWARDED_FOR');
}
else if(getenv('REMOTE_ADDR'))
{
$ip = getenv('REMOTE_ADDR');
} else {
$ip = 'unknown';
}
$browser = $HTTP_USER_AGENT;
$refer = $HTTP_REFERER;
mysql_connect("$dbhost","$user","$pass");
mysql_select_db("$dbname");
$sql = "INSERT INTO count (ip,browser,refer) values ('$ip','$browser','$refer')";
$result = mysql_query($db);
?>
Posted: Thu Aug 07, 2003 6:08 pm
by Drachlen
replace
Code: Select all
$sql = "INSERT INTO count (ip,browser,refer) values ('$ip','$browser','$refer')";
$result = mysql_query($db);
with:
Code: Select all
mysql_query("INSERT INTO count (ip,browser,refer) values ('$ip','$browser','$refer')") or die(mysql_error());
That way, if it doesnt work, it will give you a die message, that helps lead to the problem.. But it should work..
Posted: Thu Aug 07, 2003 6:10 pm
by Kriek
View modifications with the addition of a function
Code: Select all
<?php
/*
kriek at jonkriek dot com
*/
include 'config.php';
function getIP() {
if (isset($_SERVER)) {
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
return $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
return $_SERVER['REMOTE_ADDR'];
}
} else {
if (isset($GLOBALS['HTTP_SERVER_VARS']['HTTP_X_FORWARDER_FOR'])) {
return $GLOBALS['HTTP_SERVER_VARS']['HTTP_X_FORWARDED_FOR'];
} else {
return $GLOBALS['HTTP_SERVER_VARS']['REMOTE_ADDR'];
}
}
}
$ip = getIP();
$browser = $_SERVER['HTTP_USER_AGENT'];
$refer = $_SERVER['HTTP_REFERER'];
$db = mysql_connect($dbhost, $user, $pass) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
$sql = "INSERT INTO count (ip,browser,refer) VALUES('".$ip."','".$browser."','".$refer."')";
$result = mysql_query($sql) or die(mysql_error());
mysql_close($db);
?>
Posted: Fri Aug 08, 2003 1:47 am
by GK
great that works
only refer doesn;t work
how can i test that?
i would like to get as much information about the visitor as possible
Posted: Fri Aug 08, 2003 9:37 am
by m3rajk
maybe no one referred them?
referrer is only set IF coming form someplace AND the person isn't behind a proxy that strips it OR has it enabled on their browser
Posted: Fri Aug 08, 2003 7:26 pm
by Kriek
No problem GK, glad I could be of assistance to you
GK wrote:Only refer doesn't work. How can i test that?
m3rajk is correct, HTTP_REFERER will return the URL of the referring page as a string, IF there IS a referring page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted, but you can however test HTTP_REFERER by placing a link on your site that is directed back to your site.
GK wrote:I would like to get as much information about the visitor as possible.
See
Predefined Variables associated with the $_SERVER superglobal array.