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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.