Storing visitor to sql

How can I make a script that stores visitor IP & Location & Time to database?

I really tried to make this a few times but didn’t work for out well :frowning:

Could only get visitor to text file work.

And if can I would like another script that shows results of the visitors

Very appreciated if anyone can help!

I tried making a script, kept getting syntax error, rage quited deleted it xD

Thats why I’m asking if someone could help me here.

Just create variables for the IP, location and Time. Then insert those to a table. How did you attemp it?

Well, you know to get the IP you use: $_SERVER[‘REMOTE_ADDR’];. Assign it to a variable:

$ip = $_SERVER['REMOTE_ADDR'];

then insert it into your db:


INSERT INTO daTable (IP)
VALUES ($ip)

I know those 2 but how would I set it up in a script, could you make the base?
Then I try to work out from it to add time, location etc.

No I cannot at this moment. That’s why I asked for what you attempted.

<?php

//mysql connect here I guess or I could include a file
$ip = $_SERVER[‘REMOTE_ADDR’];
//$time = // no clue

$sql_insert=“INSERT INTO visitor (id, ip, time, host, location)VALUES (‘noclue’, ‘$ip’, ‘noclue’, ‘noclue’, ‘$time’)”;
?>
<?php
echo ‘Am I doing great?’;
?>

You need to include some words in your reply…

What do you mean?

You just posted code. Do you get an error? Do you just want me to say it looks ok? I don’t know what sort of reply you wanted in #7.

Ok I got this now :

The only problem is, it doesn’t store the right time, agent, ref.

I want ref to say what browser he used, and time he went on it.
If can as 18:34 for example.
And host, to store host.

<?php
$ip = $_SERVER[‘REMOTE_ADDR’]; ( Working fully )
$tm=time(); ( This gives me a rare thing )
$ref=@$HTTP_REFERER; ( Stores nothing )
$agent=@$HTTP_USER_AGENT; ( Stores nothing )
$host = no clue what to do here.

$GETsql = “INSERT INTO visitor (tm, ref, agent, ip, host)VALUES (‘$tm’, ‘$ref’, ‘$agent’, ‘$ip’, ‘Under Construction’)”;
$test=mysql_query($GETsql);
?>

<?php

$user_ip = $_SERVER['REMOTE_ADDR'];
$user_referer = $_SERVER['HTTP_REFERER'];
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$user_host = gethostbyaddr(@$_SERVER['REMOTE_ADDR']);

$sql="
    INSERT INTO visitor
        (
              tm
            , ref
            , agent
            , ip
            , host
        )
    VALUES
        (
              NOW()
            , '$user_referer'
            , '$user_agent'
            , '$user_ip'
            , '$user_host'
        )
";

$test = mysql_query($sql);

?>

Most of the info is found in the $_SERVER array, the method of getting the hostname I had to look up. The use of NOW() in the query will store a “timestamp” according to the time on MySQL server, provided that the tm field is set to use the datetime data type.

Can I give you a kiss?

omg thanks:D
gonna try it now :smiley:

How would I output this for example I want a page that displays it like
Time :
Host :
Browser :
IP :

And then a whole list of these

Lets not forget about escaping that ‘user supplied’ info. :slight_smile:


<?php
function get_server_var($var, $default){
  return empty($_SERVER[$var]) ? $default : $_SERVER[$var] ;
}

function get_ip_address($default = null){
  return get_server_var('REMOTE_ADDR', $default);
}

function get_referer($default = null){
  return get_server_var('HTTP_REFERER', $default);
}

function get_user_agent($default = null){
  return get_server_var('HTTP_USER_AGENT', $default);
}

$sql = sprintf(
  "INSERT INTO visitor (tm, ip, ref, ua) VALUES (NOW(), '%s', '%s', '%s');",
  mysql_real_escape_string(get_ip_address()),
  mysql_real_escape_string(get_referer()),
  mysql_real_escape_string(get_user_agent())
);

mysql_query($sql);

What do you with that AnthonySterling, didn’t understand xD
To complexed.

The last thing I would need to complete the script is country, could anyone help me do this?

So it also adds its country, for example if someone from england goes on it stores as England and Netherlands as Netherlands etc.

You need to understand the basic first, lets see if we can get you ‘walking’ properly before we show you how to run. :smiley:

Have you read the manual yet? This page shows you how to extract data, and [URL=“http://www.php.net/manual/en/function.mysql-insert-id.php”]this page also has an example ‘insert’.

Try some of the simpler stuff first. :wink:

Anthony.

You would just write a query that grabs that info and spit it out.


while($row =  mysql_fetch_assoc($result)){
echo 'Time: '. $row['time'].'<br />';
echo 'Host: '. $row['host'].'<br />';
echo 'Browser: '. $row['browser'].'<br />';
echo 'IP: '.$row['IP'].'<br /><br />';
}

I have done everything now, thanks everyone for help.
I just would need the country added then I’m done.

So far :

This is currently my display script :


<table class="booter">

    <tr>

      <td class="head" width="150"><strong>Time</strong></div></td>
      <td class="head" width="100"><strong>IP Address</strong></div></td>
	  <td class="head" width="500"><strong>Browser</strong></div></td>
	  <td class="head" width="200"><strong>Host</strong></div></td>

      </tr>

    <?php
$cquery1="SELECT * FROM visitor WHERE status='up'";
$cresult1=mysql_query($cquery1);

$query="SELECT * FROM visitor";
$result=mysql_query($query);

while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
?>

    <tr>
      <td class="cell"><? echo date('d M Y H:i:s', $row['tm']);?></td>
	  <td class="cell"><? echo $row['ip'];?></td>
	  <td class="cell"><? echo $row['agent'];?></td>
	  <td class="cell"><? echo $row['host'];?></td>
	  
      </tr>

	<?

	}

	?>

  </table>

And this is my store script :


<?php
$user_ip = $_SERVER['REMOTE_ADDR'];
$user_referer = $_SERVER['HTTP_REFERER'];
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$user_host = gethostbyaddr(@$_SERVER['REMOTE_ADDR']);
$tm=time();

$GETsql = "INSERT INTO visitor (tm, ref, agent, ip, host)VALUES ('$tm', '$user_referer', '$user_agent', '$user_ip', '$user_host')";
$test=mysql_query($GETsql);
?>

You’ll find this stops working completely if the referrer or user-agent contains a single comma. Additionally, as you’re not escaping the data when displaying it, someone could quite easily hijack your site by providing a custom referrer and user-agent.

You’re doing this wrong, if you would like to how to do it right and/or safer just let us know.