Jump to content

IP address recording.


EsOne

Recommended Posts

Okay. I understand. The while loop will keep going through $query to continue to get results.

 

Any idea why it is working without a while loop, but not with one?

 

And BTW guys. I really do appreciate all the time you have spent helping me. If I didn't have this kind of help, I would probably be less likely to continue to learn.

 

Lynda.com has helped me get started with the basics though. It wasn't cheap to learn. Next month I will be starting web development classes in my college, but PHP will not be explored until my second semester. I am 24 years old and feel like I should be learning something like this. Web development has always been something I have wanted to learn. I also am going to dabble in VB.NET in college as well, so I can have a small knowledge of OOP.

Link to comment
Share on other sites

It could be because you already have a field name $results or $row, but without seeing the full code its hard to say. in all reality if it worked like that it should be working the other way too. OOP is present in php as well. You can make classes and create properties, methods and variables for those classes (or object). If you are ever going to do any programming in the business world you are going to need to know OOP pretty well. Here is an example of OOP in php:

 

class person{
  public $name;
}

$aperson = new person();
$aperson->name = 'nick';

echo $aperson->name;

 

that is just a class that has a variable $name. then i create the class with a variable $aperson. Then i assign the variable $name in the class to nick then i just echo it to the screen. So OOP is everywhere.

Link to comment
Share on other sites

It could be because you already have a field name $results or $row, but without seeing the full code its hard to say. in all reality if it worked like that it should be working the other way too. OOP is present in php as well. You can make classes and create properties, methods and variables for those classes (or object). If you are ever going to do any programming in the business world you are going to need to know OOP pretty well. Here is an example of OOP in php:

 

class person{
  public $name;
}

$aperson = new person();
$aperson->name = 'nick';

echo $aperson->name;

 

that is just a class that has a variable $name. then i create the class with a variable $aperson. Then i assign the variable $name in the class to nick then i just echo it to the screen. So OOP is everywhere.

 

I checked for pre-exhisting $row variables. None present, but just to make sure, I changed the $row variable to $habo, since there is no way I already have a variable named habo. Same results.

 

Here is the complete code of the page.

 

It includes the code to ban certain IPs from banned.txt. The code I am working now for having it display results is at the bottom.

 

<?php

$connection= mysql_connect("******","******","******");

if(!$connection)
{
die("Error" . mysql_error());
}

$dbselect= mysql_select_db("greg1233",$connection);

$IP =$_SERVER['REMOTE_ADDR']; //Or whatever refined IP
$Requests =mysql_real_escape_string(print_r($_REQUEST,true)); //Grab server request vars
$Date =date('Y-m-d'); //The date, you can use other info..

mysql_query("INSERT INTO ip_table (IP, requests, date) VALUES('".$IP."','".$Requests."', '".$Date."' ) ") or die(mysql_error());



if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
elseif (isset($_SERVER['HTTP_VIA'])) {
    $ip = $_SERVER['HTTP_VIA'];
}
elseif (isset($_SERVER['REMOTE_ADDR'])) {
    $ip = $_SERVER['REMOTE_ADDR'];
}
else {
    $ip = "Banned";
}
$banned = file("banned.txt", "r+");
$nbanned = count($banned);
function ban($ip, $banned, $nbanned){
    for ($i = 0 ; $i < $nbanned ; $i++) {
        
        // Use this if you want to use IP patterns with regular expressions:
        // if (eregi($ip, $banned[$i])) {
        // We have to strip the end-of-line characters, to test for equality: 
        if ($ip ==  rtrim($banned[$i])) {
            echo "You have been banned from this site, if you feel this is in error ";
            echo "please send email to you@yoursite.com ";
            die();
        }
    }
}
ban($ip, $banned, $nbanned);



$query=mysql_query("SELECT * FROM ip_table ORDER BY ID DESC");
while ($habo = mysql_fetch_assoc($query)) {
    echo $habo[0];
    echo $habo[1];
    echo $habo[2];
}

?>

Link to comment
Share on other sites

AWESOME! after I changed the variable to $habo, and changed assoc to array again, it worked.

 

Now. This may be a dumb question. When it displays, it comes in a single row. How do I make them show horizontally?

 

I.E

 

xx.xxx.xxx.xx *date*

xx.xxx.xxx.xx *date*

xx.xxx.xxx.xx *date*

xx.xxx.xxx.xx *date*

xx.xxx.xxx.xx *date*

 

 

Also, previously, I was recommended to make a "Requests" section in my table. What does the information in here hold?

Link to comment
Share on other sites

Got that to work properly.

 

Now, I need to add Time to the equation. (Time of visit)

 

I edited the following code with the time:

 

$dbselect= mysql_select_db("greg1233",$connection);

$IP =$_SERVER['REMOTE_ADDR']; //Or whatever refined IP
$Requests =mysql_real_escape_string(print_r($_REQUEST,true)); //Grab server request vars
$Date =date('Y-m-d'); //The date, you can use other info..
$Time =time('h.m.s');

mysql_query("INSERT INTO ip_table (IP, requests, date, time) VALUES('".$IP."','".$Requests."', '".$Date."', '".$Time."' ) ") or die(mysql_error());

 

It does register something in the MYSQL, but it is WAY inaccurate, showing the time as 839:59:59

Link to comment
Share on other sites

just change the $Time variable to $Time = time(); that will give you a timestamp (something like 098767762348). You will need to store this into an int field (usually i set it to 20 just to be safe that the whole timestamp will be set there). Then when you need to show it you will do $date = date('m/d/Y',$Time);.

Link to comment
Share on other sites

I figured it out! YAY! That makes me happy that I was able to debug it by myself!

 

All I did was change it to $Date =date('D dS M,Y h:i a');, and it shows as:

xx.xxx.xxx.xx Sat Dec 12th,2009 05:26 pm

 

Oh, and checking the user agent via getenv("HTTP_USER_AGENT") can aid you in removing bots, and if there is no user agent, than it is either a bot or untrusted. I forgot to think of this.. This should help you much more than IP filtering, it's vague.

 

May be a fun tool to put in your database as well as the others, since you can compare IPs and what browser they're using, much easier to sift them out and add them to a 'blacklist' (with preg_replace or whatnot) if you need.

Link to comment
Share on other sites

I figured it out! YAY! That makes me happy that I was able to debug it by myself!

 

All I did was change it to $Date =date('D dS M,Y h:i a');, and it shows as:

xx.xxx.xxx.xx Sat Dec 12th,2009 05:26 pm

 

Oh, and checking the user agent via getenv("HTTP_USER_AGENT") can aid you in removing bots, and if there is no user agent, than it is either a bot or untrusted. I forgot to think of this.. This should help you much more than IP filtering, it's vague.

 

May be a fun tool to put in your database as well as the others, since you can compare IPs and what browser they're using, much easier to sift them out and add them to a 'blacklist' (with preg_replace or whatnot) if you need.

 

This HTTP_USER_AGENT you referred to. What exactly does it do, and where would it be used?

Link to comment
Share on other sites

This HTTP_USER_AGENT you referred to. What exactly does it do, and where would it be used?

 

It's the user agent of the browser (Note it is sent by the client, and remains as an environment variable on the server http://php.net/manual/en/function.getenv.php ) for the site to be able to know the browser's type (I.E./Firefox/Safari etc.) and would come out as something such as:

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 

 

Note most bots do not , or do not care to define a user agent, And most e-mail harvesting bots are named. You may want to match it against all the common browsers to further sanitize your pages..

 

$agent = getenv("HTTP_USER_AGENT");
if (preg_match("/MSIE/i", $agent)) {
} elseif(preg_match("/FIREFOX/i", $agent)) {
} elseif(preg_match("/MOZILLA/i", $agent)) {
} else {
        die('Uh oh, Bot?')
}

 

Bleh, messy example but easy to do, preg_match isn't as hard as it looks,  you may want to add Chrome and a few others as well if you wish to use this method, Or you can simply just check if it's empty, an easier way.

 

Link to comment
Share on other sites

This HTTP_USER_AGENT you referred to. What exactly does it do, and where would it be used?

 

It's the user agent of the browser (Note it is sent by the client, and remains as an environment variable on the server http://php.net/manual/en/function.getenv.php ) for the site to be able to know the browser's type (I.E./Firefox/Safari etc.) and would come out as something such as:

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 

 

Note most bots do not , or do not care to define a user agent, And most e-mail harvesting bots are named. You may want to match it against all the common browsers to further sanitize your pages..

 

$agent = getenv("HTTP_USER_AGENT");
if (preg_match("/MSIE/i", $agent)) {
} elseif(preg_match("/FIREFOX/i", $agent)) {
} elseif(preg_match("/MOZILLA/i", $agent)) {
} else {
        die('Uh oh, Bot?')
}

 

Bleh, messy example but easy to do, preg_match isn't as hard as it looks,  you may want to add Chrome and a few others as well if you wish to use this method, Or you can simply just check if it's empty, an easier way.

 

So broken down it would be:

 

$agent = getenv("HTTP_USER_AGENT"); //setting the agent into the variable $agent
if (preg_match("/MSIE/i", $agent)) {  // If the term MSIE is in there, dump it to $agent (?)
} elseif(preg_match("/FIREFOX/i", $agent)) {  //Same as above just with FF
} elseif(preg_match("/MOZILLA/i", $agent)) { /* Would I be able to add Safari by doing "/Safari/i"? Just wondering. I don't want to block someone because they are using a weird browser. */
} else {
        die('Uh oh, Bot?') // Does this die command stop it from producing the rest of the PHP on the //screen?
}

Link to comment
Share on other sites

Die does as you'd expect it, It kills the output buffer and prevents any further code being run, Displaying only the message. preg_match has two parameters, pattern and match, so it's just matching if the phrase 'MSIE' exists in '$agent'.

 

And yeah, You can add as many browsers as you want (you can do an array if you're willing to instead of if's, just an example code), and the /i means insensitive, so it can be 'SaFarI' and match "Safari" or "safari".. I use this on a script of mine, just pulled out a bit of it.

Link to comment
Share on other sites

Die does as you'd expect it, It kills the output buffer and prevents any further code being run, Displaying only the message. preg_match has two parameters, pattern and match, so it's just matching if the phrase 'MSIE' exists in '$agent'.

 

And yeah, You can add as many browsers as you want (you can do an array if you're willing to instead of if's, just an example code), and the /i means insensitive, so it can be 'SaFarI' and match "Safari" or "safari".. I use this on a script of mine, just pulled out a bit of it.

Would I be able to create a table in MYSQL that stores the whole USER_AGENT text, then echo that out so it comes in the format you mentioned before (Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 )? Kind of like Wordpresses tracker.
Link to comment
Share on other sites

I did that by myself too! That makes me happy! LOL. You don't understand how happy I am getting. I think I am getting on fiances nerves because I keep YAYing out loud. LOL.

 

I did it by:

$IP =$_SERVER['REMOTE_ADDR']; //Or whatever refined IP
$Requests =mysql_real_escape_string(print_r($_REQUEST,true)); //Grab server request vars
$Date =date('D M dS Y h:i a'); //The date, you can use other info..
$agent = getenv("HTTP_USER_AGENT"); //Or whatever refined IP //Used the getenv code to collect the USER AGENT and assign it to $agent




mysql_query("INSERT INTO ip_table (IP, requests, date, time, agents) VALUES('".$IP."','".$Requests."', '".$Date."', '".$Time."', '".$agent."' ) ") or die(mysql_error()); // Added agents to the INSERT section, so it knows to insert the value of $agents (at the end of VALUES) into it.

 

I understand the principle, but what exactly does "getenv" mean?

Link to comment
Share on other sites

I did that by myself too! That makes me happy! LOL. You don't understand how happy I am getting. I think I am getting on fiances nerves because I keep YAYing out loud. LOL.

 

I did it by:

$IP =$_SERVER['REMOTE_ADDR']; //Or whatever refined IP
$Requests =mysql_real_escape_string(print_r($_REQUEST,true)); //Grab server request vars
$Date =date('D M dS Y h:i a'); //The date, you can use other info..
$agent = getenv("HTTP_USER_AGENT"); //Or whatever refined IP //Used the getenv code to collect the USER AGENT and assign it to $agent




mysql_query("INSERT INTO ip_table (IP, requests, date, time, agents) VALUES('".$IP."','".$Requests."', '".$Date."', '".$Time."', '".$agent."' ) ") or die(mysql_error()); // Added agents to the INSERT section, so it knows to insert the value of $agents (at the end of VALUES) into it.

 

I understand the principle, but what exactly does "getenv" mean?

 

It gets the environmental variable of the Apache server, rather than going through PHP. $_SERVER is a Superglobal, and it'd be aliased to $_SERVER['HTTP_USER_AGENT']; For example. getenv

 

Yeah! It's so neat creating your own scripts and hand coding everything to perfection, What made me love doing PHP.

 

Link to comment
Share on other sites

Were you able to get my PM oni?

I have never sent one before and it was kind of legnthy. When i went to sent messages it did not show anything was sent. I hope I do not have to rewrite it T_T

 

Some forums don't automatically copy them into sent, it's annoying. But yeah, anything I can help you with I'll try, just don't know if I know the specifics of what you want.

Link to comment
Share on other sites

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.