Computer Webmaster Gaming Console Graphics Forum

Welcome to the Computer Webmaster Gaming Console Graphics Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

MK PitStop Main Earn $25 Earn Money Posting Extras Members Blogs Image Hosting User Pages
Go Back   Computer Webmaster Gaming Console Graphics Forum > Webmaster Forum > Website Coding > PHP
Register FAQ/Rules Become A V.I.P. Member Search Today's Posts Mark Forums Read

PHP PHP for some can be one of the hardest website programming codes, so do you need help on your PHP script, if it is php4, php5 or lower this is the place for you for any PHP help.

Google
Closed Thread
 
LinkBack Thread Tools Display Modes
Old 07-01-2007, 5:08 PM   #1
Ian.H [dS]
 
Ian.H [dS]'s Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default I need to write a function to compare IP addresses

On Fri, 15 Aug 2003 01:29:26 -0400 in
<message-id:cd__a.739$3M4.380@lakeread04>
"Phil Powell" <soazine@erols.com> wrote:

> I have written a function that is supposed to compare the first three
> nodes of an IP address, but it is riddled with errors that I have
> given up trying to figure out.
>
> Is there an existing function out there that can do this? I want to
> compare"127.0.0.1" with "127.0.0.100" and come with as a "match", or
> "127.0.0.1" with "127.0.0.1", but no "match if "127.0.0.1" and
> "127.255.0.0".
>
> Here is what I have:



[ snip self-confessed non-working code ]


Phil,

Have a try with:


<?php
$ip1 = '127.0.0.1';
$ip2 = '127.0.0.100';
$ip3 = '127.255.0.0';


function ipcmp($ip_1, $ip_2) {
if (substr($ip_1, 0, strrpos($ip_1, '.')) ==
substr($ip_2, 0, strrpos($ip_2, '.'))
) {
return 'Match';
} else {
return 'No Match';
}
}

echo ipcmp($ip1, $ip2) . "<br />\n";
echo ipcmp($ip3, $ip2) . "<br />\n";
echo ipcmp($ip1, $ip3);
?>


HTH =)



Regards,

Ian

--
Ian.H [Design & Development]
digiServ Network - Web solutions
www.digiserv.net | irc.digiserv.net | forum.digiserv.net
Programming, Web design, development & hosting.
 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Advertisements
Old 07-01-2007, 5:08 PM   #2
Phil Powell
 
Phil Powell's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default I need to write a function to compare IP addresses

I plain give up. Your solution is so simple it's amazing and works, mine is
so complicated and fails.

Phil

"Ian.H [dS]" <ian@WINDOZEdigiserv.net> wrote in message
news:20030815070820.2dfa51f2.ian@WINDOZEdigiserv.n et...
> On Fri, 15 Aug 2003 01:29:26 -0400 in
> <message-id:cd__a.739$3M4.380@lakeread04>
> "Phil Powell" <soazine@erols.com> wrote:
>
> > I have written a function that is supposed to compare the first three
> > nodes of an IP address, but it is riddled with errors that I have
> > given up trying to figure out.
> >
> > Is there an existing function out there that can do this? I want to
> > compare"127.0.0.1" with "127.0.0.100" and come with as a "match", or
> > "127.0.0.1" with "127.0.0.1", but no "match if "127.0.0.1" and
> > "127.255.0.0".
> >
> > Here is what I have:

>
>
> [ snip self-confessed non-working code ]
>
>
> Phil,
>
> Have a try with:
>
>
> <?php
> $ip1 = '127.0.0.1';
> $ip2 = '127.0.0.100';
> $ip3 = '127.255.0.0';
>
>
> function ipcmp($ip_1, $ip_2) {
> if (substr($ip_1, 0, strrpos($ip_1, '.')) ==
> substr($ip_2, 0, strrpos($ip_2, '.'))
> ) {
> return 'Match';
> } else {
> return 'No Match';
> }
> }
>
> echo ipcmp($ip1, $ip2) . "<br />\n";
> echo ipcmp($ip3, $ip2) . "<br />\n";
> echo ipcmp($ip1, $ip3);
> ?>
>
>
> HTH =)
>
>
>
> Regards,
>
> Ian
>
> --
> Ian.H [Design & Development]
> digiServ Network - Web solutions
> www.digiserv.net | irc.digiserv.net | forum.digiserv.net
> Programming, Web design, development & hosting.



 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Old 07-01-2007, 5:09 PM   #3
Ian.H [dS]
 
Ian.H [dS]'s Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default I need to write a function to compare IP addresses

On Fri, 15 Aug 2003 04:30:48 -0400 in
<message-id:eT0%a.745$3M4.707@lakeread04>
"Phil Powell" <soazine@erols.com> wrote:

> I plain give up. Your solution is so simple it's amazing and works,
> mine is so complicated and fails.
>
> Phil



Don't give up Phil.. I been "messing around" with PHP for around 5 years
now.. it's great fun =)

IMO, you over-complicated your method by splitting the IP addresses into
separate numerical blocks. If you _really_ need to do some checking on
those values as INTs, then fair enough, if it's literally to compare the
first X chars, I just thought it'd be easier to work with as a complete
string.

Glad it works.. and FWIW.. I've posted some pretty "silly" questions
here before too.. sometimes, it's just a matter of not seeing the forest
for the trees =)



Regards,

Ian

--
Ian.H [Design & Development]
digiServ Network - Web solutions
www.digiserv.net | irc.digiserv.net | forum.digiserv.net
Programming, Web design, development & hosting.
 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Old 07-01-2007, 5:09 PM   #4
Martin Lucas-Smith
 
Martin Lucas-Smith's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default I need to write a function to compare IP addresses




> function ipcmp($ip_1, $ip_2) {
> if (substr($ip_1, 0, strrpos($ip_1, '.')) ==
> substr($ip_2, 0, strrpos($ip_2, '.'))
> ) {
> return 'Match';
> } else {
> return 'No Match';
> }
> }



Or a one-liner using the ternary operator, if you prefer, using Ian.H's
example:


function ipcmp($ip_1, $ip_2) {
return ((substr($ip_1, 0, strrpos($ip_1, '.')) == substr($ip_2, 0, strrpos($ip_2, '.'))) ? true : false);
}



Martin Lucas-Smith www.geog.cam.ac.uk/~mvl22
www.lucas-smith.co.uk

Senior Computing Technician (Web Technician)
Department of Geography, University of Cambridge (01223 3)33390

& Webmaster, SPRI
Scott Polar Research Institute, University of Cambridge



 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Old 07-01-2007, 5:09 PM   #5
Phil Powell
 
Phil Powell's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default I need to write a function to compare IP addresses

That's how I handled it, as a one-liner with the ternary operator!

Phil

"Martin Lucas-Smith" <mvl22@cam.ac.uk> wrote in message
newsine.SOL.4.44.0308151320160.23887-100000@green.csi.cam.ac.uk...
>
>
>
> > function ipcmp($ip_1, $ip_2) {
> > if (substr($ip_1, 0, strrpos($ip_1, '.')) ==
> > substr($ip_2, 0, strrpos($ip_2, '.'))
> > ) {
> > return 'Match';
> > } else {
> > return 'No Match';
> > }
> > }

>
>
> Or a one-liner using the ternary operator, if you prefer, using Ian.H's
> example:
>
>
> function ipcmp($ip_1, $ip_2) {
> return ((substr($ip_1, 0, strrpos($ip_1, '.')) == substr($ip_2, 0,

strrpos($ip_2, '.'))) ? true : false);
> }
>
>
>
> Martin Lucas-Smith www.geog.cam.ac.uk/~mvl22
> www.lucas-smith.co.uk
>
> Senior Computing Technician (Web Technician)
> Department of Geography, University of Cambridge (01223 3)33390
>
> & Webmaster, SPRI
> Scott Polar Research Institute, University of Cambridge
>
>
>



 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Old 07-01-2007, 5:32 PM   #6
Ray Bellis
 
Ray Bellis's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default I need to write a function to compare IP addresses


"Martin Lucas-Smith" <mvl22@cam.ac.uk> wrote:
>
> function ipcmp($ip_1, $ip_2) {
> return ((substr($ip_1, 0, strrpos($ip_1, '.')) ==
> substr($ip_2, 0, strrpos($ip_2, '.')))
> ? true : false);
> }


Why even bother with the ternary operator?

(boolean condition) ? true : false

is exactly equivalent to:

(boolean condition)

Ray




 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Featured Websites
Free Space
Free Space
Free Space Free Space
Closed Thread
Tags: , , ,




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
logging ip addresses behind proxies somaBoyMX PHP 1 07-01-2007 4:00 PM
IQue 3600 Addresses Rick GPS 2 06-25-2007 11:56 PM
Interpreting Hawaii addresses? Plug GPS 3 06-25-2007 11:48 PM
Garmin 176C locating addresses Normand Slowskan GPS 1 06-12-2007 5:13 PM
compare .. Piet Database 2 06-10-2007 12:22 AM


Featured Websites




All times are GMT +1. The time now is 12:18 AM.


Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.0.0
Cheap Computers
MK PitStop Copyright 2005 - 2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98