Jump to content

PHP- MYSQL ISSUE - IP RANGES


princeofpersia

Recommended Posts

Hi guys, im trying to create a functionality where I can direct my users to their own folder. for example, if my user is from uk, they will be redirected uk.mydomain.com

 

my current ip address is    87.194.123.198 and i have created a table called ip with four fields, please see attached image

 

in my php, I am getting connected to database but when in my mysql query there is something wrong. I have inserted only one ip range which my ip is in within it. But I get the error "Finding your location failed" (this is a or die statement in my query).

 

I am going to have my ip ranges without dots, I have no idea where the mistake it. my php code is below,

 

I have followed the ip2location tutorial but I'd like to use this experiment as learning curve.

 

I do appreciate your time and many thanks in advance,

 


<?


$mysql_server = "localhost"; 
$mysql_user_name = "princeofpersia"; 
$mysql_user_pass = "fgdjk4588@@!5"; 




$link = mysql_connect($mysql_server, $mysql_user_name, $mysql_user_pass) or die("Could not connect to MySQL database"); 


mysql_select_db("myipexp") or die("Could not select database"); 
$ip=$_SERVER["REMOTE_ADDR"];
$clearip= str_replace(".", "", $ip);
echo $clearip;
$list=mysql_query("SELECT * FROM ip WHERE $clearip<=to AND $clearip>=from") or die("Finding your location failed");

if (mysql_num_rows($list)==0) {

header("Location: country.php"); 

}

else

///////////////////////////------  Countries Redirection Starts Here -----//////////////////////////////////////////////////

while ($row=mysql_fetch_array($list)){

$countrycode=$row['code'];
$countryname=$row['country'];	

}

echo $countrycode;
echo $countryname;




?>




 

[attachment deleted by admin]

Link to comment
Share on other sites

Simply removing the decimal points is not the way to work with IP addresses. What happens when you have an address or 2.125.22.230? How would you even know what it really is without the decimals? Is it 21.252.22.30? Is it 212.52.223.0? There are functions available in MySQL such as INET_ATON() and INET_NTOA(), as well as the bitwise operators that make dealing with IP addresses much easier.

Link to comment
Share on other sites

Thanks, but thats not the issue, ;lets assume we working with normal digits and not IPs, what is wrong in the code that causes mysql query to fail?

 

This is just an experiment, and im not worry if im using ip or normall digits, the issue here is that why mysql query cant find my  ip (as a digit) in my db?

 

Thanks

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.