Post by Joe1234 » Tue Nov 12, 2024 3:35 pm

I'm trying to extract ip address from an htaccess file. I can get the address when it's the full IP, but I'm having issues picking up the addresses when it's two or three octets.
It's looking for any line that starts with "deny from ".
I've tried the following with no luck.

Code: Select all

		$existingContent = file_get_contents(htaccess file);
		preg_match_all('/deny from \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/', $existingContent, $matches);
		//preg_match_all('/^deny from (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/', $existingContent, $matches);
		//preg_match_all('/^deny from (\d{1,3}(?:\.\d{1,3}){1,3})$/', $existingContent, $matches);
		//preg_match_all('/deny from \d[\d.]*$/', $existingContent, $matches);
		$htaccessLines = $matches[0];
How can I fix this? Thanks.

v3.0.4.0 php 8.1
I'm here for a reason, if your response is contact a/the developer, just don't reply.


Active Member

Posts

Joined
Sat Jan 01, 2022 5:47 am

Post by by mona » Tue Nov 12, 2024 4:33 pm

For general questions that are not related to Opencart
https://stackoverflow.com/

DISCLAIMER:
You should not modify core files .. if you would like to donate a cup of coffee I will write it in a modification for you.


https://www.youtube.com/watch?v=zXIxDoCRc84


User avatar
Expert Member

Posts

Joined
Mon Jun 10, 2019 9:31 am

Post by nonnedelectari » Tue Nov 12, 2024 4:56 pm

Joe1234 wrote:
Tue Nov 12, 2024 3:35 pm
I'm trying to extract ip address from an htaccess file. I can get the address when it's the full IP, but I'm having issues picking up the addresses when it's two or three octets.
It's looking for any line that starts with "deny from ".
I've tried the following with no luck.

Code: Select all

		$existingContent = file_get_contents(htaccess file);
		preg_match_all('/deny from \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/', $existingContent, $matches);
		//preg_match_all('/^deny from (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/', $existingContent, $matches);
		//preg_match_all('/^deny from (\d{1,3}(?:\.\d{1,3}){1,3})$/', $existingContent, $matches);
		//preg_match_all('/deny from \d[\d.]*$/', $existingContent, $matches);
		$htaccessLines = $matches[0];
How can I fix this? Thanks.
why not run the preg_match_all four times, 1 octet, 2 octets, 3 octets and 4 octets in reverse order and merge the results.

You could also try this

Code: Select all

preg_match_all('/deny from (\d{1,3}\.(?:\d{1,3}\.)?(?:\d{1,3}\.)?(?:\d{1,3})?(?:\/\d{1,2})?)/i', $existingContent, $matches);
That should get either:

Code: Select all

Deny from 123.
Deny from 123.123.
Deny from 334.442.422.
Deny from 343.234.334.444
Deny from 123.334.0.0/16
giving:

Code: Select all

array (
  0 => 
  array (
    0 => 'deny from 123.',
    1 => 'deny from 123.123.',
    2 => 'deny from 334.442.422.',
    3 => 'deny from 343.234.334.444',
    4 => 'deny from 123.334.0.0/16',
  ),
  1 => 
  array (
    0 => '123.',
    1 => '123.123.',
    2 => '334.442.422.',
    3 => '343.234.334.444',
    4 => '123.334.0.0/16',
  ),
)

Active Member

Posts

Joined
Thu Mar 04, 2021 6:34 pm

Post by Joe1234 » Tue Nov 12, 2024 10:42 pm

by mona wrote:
Tue Nov 12, 2024 4:33 pm
For general questions that are not related to Opencart
https://stackoverflow.com/
I figure since it's for an extension I'm making for OC it would be appropriate to post it here since OC is all php code. No?

@nonnedelectari, that seems to work perfectly. Thanks.

v3.0.4.0 php 8.1
I'm here for a reason, if your response is contact a/the developer, just don't reply.


Active Member

Posts

Joined
Sat Jan 01, 2022 5:47 am

Post by nonnedelectari » Wed Nov 13, 2024 12:43 pm

Joe1234 wrote:
Tue Nov 12, 2024 10:42 pm
by mona wrote:
Tue Nov 12, 2024 4:33 pm
For general questions that are not related to Opencart
https://stackoverflow.com/
I figure since it's for an extension I'm making for OC it would be appropriate to post it here since OC is all php code. No?

@nonnedelectari, that seems to work perfectly. Thanks.
You can always experiment and test these expressions using:
regex101.com and/or
www.functions-online.com/preg_match_all.html

Active Member

Posts

Joined
Thu Mar 04, 2021 6:34 pm

Post by khnaz35 » Thu Nov 14, 2024 1:06 am

Joe1234 wrote:
Tue Nov 12, 2024 3:35 pm
I'm trying to extract ip address from an htaccess file.
Why htaccess file? How you are adding it there at first place?

Got an urgent question that’s keeping you up at night? There might just be a magical inbox ready to help: khnaz35@gmail.com
Enjoy nature ;) :) :-*


User avatar
Active Member
Online

Posts

Joined
Mon Aug 27, 2018 11:30 pm
Location - Malaysia
Who is online

Users browsing this forum: Semrush [Bot] and 10 guests