PHP/MySQL Control Page Access with IP Address

PHP script to compare IP address with Ip addresses stored in MySQL file and redirect.

Obviously works fine when retrieved value and mysql store values are both 86.131.178.77, for example.

How do we match a retrieved value of 86.131.178.77 with a stored value of 86.131.178?
In a simpler script without mysql preg_match does it.
air4castsAsked:
Who is Participating?
 
Cornelia YoderConnect With a Mentor ArtistCommented:
No, because you can compensate in your query.  If you store the xxx.xxx.xxx only, then use

SELECT * FROM consultcomp WHERE SUBSTRING_INDEX(CONCAT(cons_ipad,'.'),'.',3) = SUBSTRING_INDEX(" . $ip . ",'.',3)";

This will effectively add an extra . at the end of the ip address as it's being compared.  If there are the full four parts to the ip address, it won't matter because you are looking at the third . only.

There is another possibility.  You could use

SELECT * FROM consultcomp WHERE cons_ipad like '$ip%'

This will find anything that matches $ip up to as long as $ip is.   It won't match a longer $ip with a shorter cons_ipad.
0
 
Cornelia YoderArtistCommented:
SELECT * FROM Table WHERE SUBSTRING_INDEX(IPAddress,'.',3) = '86.131.178'

0
 
hieloCommented:
>>86.131.178.77 with a stored value of 86.131.178?
It looks like you are interested in matching subnets. Since the IPs are delimited by a period, you can just
do:
$dbquad = explode(".",$dbip);
$retrievedquad = explode(".",$retrievedip);

Then compare the individual quad values or "clear" the last quad and join them again for comparison:
$dbquad[3] = 0;
$retrievedquad[3]=0;
$dbquad = implode(".",$dbquad)
$retrievedquad = implode(".",$retrievedquad);
if($retrievedquad == $dbquad)
{
 echo("matching subnet");
}
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
air4castsAuthor Commented:
Thank you for your speedy reply. Understand it but would welcome a moment further; need to incrporate into the simplest of recordsets where $ip is 'REMOTE_ADDR'. Recordset is:

mysql_select_db($database_conn_333mysql, $conn_333mysql);
$query_rst_consult = "SELECT * FROM consultcomp WHERE cons_ipad = '$ip'";
$rst_consult = mysql_query($query_rst_consult, $conn_333mysql) or die(mysql_error());
$row_rst_consult = mysql_fetch_assoc($rst_consult);
$totalRows_rst_consult = mysql_num_rows($rst_consult);
$zzz=$totalRows_rst_consult;
$idred=$row_rst_consult['cons_redirid'];
mysql_free_result($rst_consult);
if($zzz==1){

Could you help further?
0
 
hieloCommented:
"SELECT * FROM consultcomp WHERE SUBSTRING_INDEX(cons_ipad,'.',3) = SUBSTRING_INDEX(" . $ip . ",'.',3)";
0
 
air4castsAuthor Commented:
A complication arises when people are entering ip addresses in different formats sometimes omitting the last three digits, sometimes omitting only one. Are we obliged to ask them to conform to a template?
0
 
Cornelia YoderArtistCommented:
SELECT * FROM consultcomp WHERE
SUBSTRING(cons_ipad FROM 1 FOR LEAST(LENGTH(cons_ipad),LENGTH('$ip')))
=
SUBSTRING('$ip' FROM 1 FOR LEAST(LENGTH(cons_ipad),LENGTH('$ip')))

will compare the two strings to the length of the shorter one.
0
All Courses

From novice to tech pro — start learning today.