princeofpersia Posted June 30, 2011 Share Posted June 30, 2011 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] Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted June 30, 2011 Share Posted June 30, 2011 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. Quote Link to comment Share on other sites More sharing options...
princeofpersia Posted June 30, 2011 Author Share Posted June 30, 2011 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 Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted June 30, 2011 Share Posted June 30, 2011 That may not be the issue right now but it soon will be a major issue if you don't change it. As for the issue right now, you're using MySQL reserved words as field names. Quote Link to comment Share on other sites More sharing options...
princeofpersia Posted June 30, 2011 Author Share Posted June 30, 2011 thanks mate, when im done with this test, I would definitly follow your advise for my final dev. Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.