Update sql problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
GK
Forum Commoner
Posts: 51
Joined: Sat Jun 07, 2003 2:58 pm
Contact:

Update sql problem

Post 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); 
?>
Drachlen
Forum Contributor
Posts: 153
Joined: Fri Apr 25, 2003 1:16 am

Post 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..
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post 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);
?>
GK
Forum Commoner
Posts: 51
Joined: Sat Jun 07, 2003 2:58 pm
Contact:

Post 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
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post 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
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post 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.
Post Reply