Any PHP/SQL gurus here?

Home Archive Serious Business Any PHP/SQL gurus here?
O-Trap's avatar

O-Trap

Chief Shenanigans Officer

14,994 posts
Dec 5, 2014 9:44 PM
I'm trying to set up a script (Where's Ender when you need him?) that checks a visitor's IP address again a database. If it's not already in the database, the script adds it. If it is already in there, it does something else.

I had this sometime before, but I don't remember how the database was set up. Here's what I had:

[PHP]
<?php
$username = "mysql_username";
$password = "password";
$hostname = "localhost";
$ip = $_SERVER['REMOTE_ADDR'];
$short = ip2long($ip);


$dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
$dbname = mysql_select_db(mysql_database, $dbhandle) or die("Cannot find the database");

$result = mysql_query("SELECT NULL FROM table WHERE column='$short'");

if(mysql_num_rows($result)) {

echo "Baha! You've been here before!";

} else {

mysql_query("INSERT INTO table (column) VALUES (" . $short . ")");
echo "$ip is now $short";
mysql_close($dbhandle);

}

?>


[/PHP]

Anyone know how the table should be setup?
Dec 5, 2014 9:44pm
ernest_t_bass's avatar

ernest_t_bass

12th Son of the Lama

24,984 posts
Dec 5, 2014 11:51 PM
I don't have any experience whatsoever.

Hope this helps.
Dec 5, 2014 11:51pm
J

Jawbreaker

Senior Member

520 posts
Dec 6, 2014 12:32 AM
I haven't done much PHP/MySQL in the past year but this should work:

table_ip_address_log
ip_address_log_id, int(10), NO NULL, Primary Key, Auto Increment
ip_address_log_datetime, datetime
ip_address, varchar(46)

I would take a look and see if you want to use ip2long since it only deals with IPv4 and doesn't handle IPv6 (I think).
Dec 6, 2014 12:32am
O-Trap's avatar

O-Trap

Chief Shenanigans Officer

14,994 posts
Dec 6, 2014 12:59 AM
Damn. Had the SQL column set to INT(11). Changed it to VARCHAR(46) and it's working well now.

Actually thought to look because of your post, Jawbreaker. Much appreciated. Reps!
Dec 6, 2014 12:59am