air4casts
asked on
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.
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.
SELECT * FROM Table WHERE SUBSTRING_INDEX(IPAddress, '.',3) = '86.131.178'
>>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");
}
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");
}
ASKER
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_333my sql, $conn_333mysql);
$query_rst_consult = "SELECT * FROM consultcomp WHERE cons_ipad = '$ip'";
$rst_consult = mysql_query($query_rst_con sult, $conn_333mysql) or die(mysql_error());
$row_rst_consult = mysql_fetch_assoc($rst_con sult);
$totalRows_rst_consult = mysql_num_rows($rst_consul t);
$zzz=$totalRows_rst_consul t;
$idred=$row_rst_consult['c ons_rediri d'];
mysql_free_result($rst_con sult);
if($zzz==1){
Could you help further?
mysql_select_db($database_
$query_rst_consult = "SELECT * FROM consultcomp WHERE cons_ipad = '$ip'";
$rst_consult = mysql_query($query_rst_con
$row_rst_consult = mysql_fetch_assoc($rst_con
$totalRows_rst_consult = mysql_num_rows($rst_consul
$zzz=$totalRows_rst_consul
$idred=$row_rst_consult['c
mysql_free_result($rst_con
if($zzz==1){
Could you help further?
"SELECT * FROM consultcomp WHERE SUBSTRING_INDEX(cons_ipad, '.',3) = SUBSTRING_INDEX(" . $ip . ",'.',3)";
ASKER
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SELECT * FROM consultcomp WHERE
SUBSTRING(cons_ipad FROM 1 FOR LEAST(LENGTH(cons_ipad),LE NGTH('$ip' )))
=
SUBSTRING('$ip' FROM 1 FOR LEAST(LENGTH(cons_ipad),LE NGTH('$ip' )))
will compare the two strings to the length of the shorter one.
SUBSTRING(cons_ipad FROM 1 FOR LEAST(LENGTH(cons_ipad),LE
=
SUBSTRING('$ip' FROM 1 FOR LEAST(LENGTH(cons_ipad),LE
will compare the two strings to the length of the shorter one.