473,800 Members | 2,227 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Parsing .htaccess file

Hi all!

I want to parse the contents of an .htaccess file from within PHP. The
contents of the .htaccess file looks like this:

<Files foobar.tar.gz>
AuthType Basic
AuthUserFile /foobar
AuthName "Foobar"
<Limit GET>
Require user FOOBAR
</Limit>
</Files>

And so on. When I read in that file with the following code:

$fd = fopen(".htacces s", 'rb');
while (!feof($fd)) {
$line = fgets($fd, 1024);
echo "|".$line."|\n" ;
}
fclose($fd);

Then I get empty lines for those lines containing <Files ...>,
<Limit ...> and so on. How can I get at those lines as well?

What I need to know is basically the information "which user can access
which file?"

--
Stefan Bellon
Feb 10 '06 #1
4 2813
Stefan Bellon wrote:
Hi all!

I want to parse the contents of an .htaccess file from within PHP. The
contents of the .htaccess file looks like this:

<Files foobar.tar.gz>
AuthType Basic
AuthUserFile /foobar
AuthName "Foobar"
<Limit GET>
Require user FOOBAR
</Limit>
</Files>

And so on. When I read in that file with the following code:

$fd = fopen(".htacces s", 'rb');
while (!feof($fd)) {
$line = fgets($fd, 1024);
echo "|".$line."|\n" ;
}
fclose($fd);

Then I get empty lines for those lines containing <Files ...>,
<Limit ...> and so on. How can I get at those lines as well?

What I need to know is basically the information "which user can access
which file?"

Hi,

Maybe you are in htmlcontext?

What does this produce?

$myLines = file('path/to/.htaccess');
foreach($myLine s as $oneLine){
echo htmlentities($o neLine)."<br>";
}

Does that show correctly?

Regards,
Erwin Moller
Feb 10 '06 #2
Erwin Moller wrote:
$myLines = file('path/to/.htaccess');
foreach($myLine s as $oneLine){
echo htmlentities($o neLine)."<br>";
}

Does that show correctly?


This does print the content correctly, yes, thanks a lot. However, if I
want to parse (and replace) the content using ereg_replace I get funny
results as the bracket < seems to be represented as &lt; internally.
So, when trying to get at the filename, I need to do something like

ereg_replace("& lt;Files (.*)&gt;", "\\1", htmlentities($l ine))

Wow. Is this the easiest way of filtering out the information? All I
want to do is build a list of "who may access which file according to
the information inside the .htaccess file".

--
Stefan Bellon
Feb 10 '06 #3
Stefan Bellon wrote:
What I need to know is basically the information "which user can
access which file?"


In the meantime I've come up with a solution. Just in case somebody
else needs something similar, here is my PHP code:

<?php

## Read in the .htgroups file and build array of groups of array of
## users.
$tmp=file('.htg roups');
$groups=array() ;
foreach($tmp as $line)
{
$exploded=explo de(": ", $line);
$groups[$exploded[0]] = explode(" ", $exploded[1]);
}

## Read in the .htaccess file looking for group accesses for files.
$tmp=file('.hta ccess');
$file="";
$files=array();
foreach($tmp as $line)
{
## htmlentities necessary in order to get at HTML-like tags at all,
## but then they're escaped, so &lt; and &gt; are necessary.
if (ereg("&lt;File s .*&gt;", htmlentities($l ine)))
{
## We have a file section starting, so remember the file name.
$file=trim(ereg _replace("&lt;F iles (.*)&gt;", "\\1",
htmlentities($l ine)));
}
elseif (($file != "") &&
($file != "index.php" ) &&
(ereg("AuthName \".*\"", $line)))
{
## Get the authentication name of the file.
$files[$file]=array
('Name' => ereg_replace("A uthName \"(.*)\"", "\\1", $line),
'access' => 0);
}
elseif (($file != "") &&
($file != "index.php" ) &&
(ereg("Require user .*", $line)))
{
## We have a user permission.
$user=trim(ereg _replace("Requi re user (.*)", "\\1", $line));

if ($user == $_SERVER['REMOTE_USER'])
{
$files[$file]['access']=1;
}
}
elseif (($file != "") &&
($file != "index.php" ) &&
(ereg("Require group .*", $line)))
{
## We have a group permission, so get the group.
$group=trim(ere g_replace("Requ ire group (.*)", "\\1", $line));

## Look in the users of the group whether REMOTE_USER is listed.
foreach($groups[$group] as $user)
{
if ($user == $_SERVER['REMOTE_USER'])
{
$files[$file]['access']=1;
break;
}
}
}
}

## Do whatever HTML stuff you want here, and later on:

## Now loop through the file permission data and built HTML code.
foreach($files as $file => $data)
{
if (($data['access'] == 1) && (file_exists($f ile)))
{
## Do something with $file and/or data['Name']
}
}

## Rest of HTML stuff.

?>

--
Stefan Bellon
Feb 12 '06 #4
Stefan Bellon wrote:
Erwin Moller wrote:
$myLines = file('path/to/.htaccess');
foreach($myLine s as $oneLine){
echo htmlentities($o neLine)."<br>";
}

Does that show correctly?


This does print the content correctly, yes, thanks a lot. However, if I
want to parse (and replace) the content using ereg_replace I get funny
results as the bracket < seems to be represented as &lt; internally.
So, when trying to get at the filename, I need to do something like

ereg_replace("& lt;Files (.*)&gt;", "\\1", htmlentities($l ine))

Wow. Is this the easiest way of filtering out the information? All I
want to do is build a list of "who may access which file according to
the information inside the .htaccess file".


Hi,

No, it is not the easiest way, but it shows the output right to the browser.
If you use < and > in your output, your webbrowser will interpret that as a
tag.
adding htmlentities replaces the <> (and other problematic characters) by
their HTMLequivalents .

You can do what you were trying to do, but I cannot help you with the
regexp, because I suck at regexpr.
So my response was just a warning to use htmlentities whenever you are
feeding information to a browser, because of the unexpected result you get
when you do not.
Of course: Looking in the source (view source) of the page could help too.

Regards,
Erwin
Feb 17 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
7699
by: yawnmoth | last post by:
i'm trying to write a php script that will password protect some random directory by creating a .htaccess file, and a password file to accompany the .htaccess file, and it isn't working... specifically, i can't enter the newly created directory with a username of test, and a password of test, which is what i am trying to do. also, i'm testing this script out on a remote server, and... after this script runs, i can't delete any of the...
8
50627
by: Joshua Beall | last post by:
Hi All, How do I disable magic quotes via .htaccess? I put the following file in my webroot, but it does not disable magic_quotes_gpc (according to phpinfo(), both the local and master value are still "on") <IfModule mod_php4.c> php_value upload_max_filesize 8M
4
2991
by: Ivo | last post by:
Greetings newsgroup, I am moving some php scripts to a new host. While getting to know the server, I ran into a strange problem. If I add a .htaccess file, or more specifically: a .htaccess with content, to a folder, requesting (html and php) files from that folder results in internal server error messages. If I upload an empty .htaccess file, all is fine. Then phpinfo() tells me I am using Apache/1.3.27.
7
7545
by: John | last post by:
Hello. I want to get this blasted .htaccess file sorted out, so I can have sessions without register_globals being on. I have looked everywhere for info on this and I mean everywhere including the php.net manual. In the manual it said to include something like the following:
0
3721
by: Stoco | last post by:
I am trying to create a generic interface that will manage various ..htaccess protected directories. In the ideal world, the .htaccess would trigger a cgi script that would take login information and store it to a file, or block access all together. For example, anytime someone logs in, the .htaccess file should direct the user name and IP address to be stored in a log file through this cgi. A cron could be scheduled to determine if the...
0
2003
by: Jack Hambabo | last post by:
Hi, I'm searching for a php script that can find out whether the current user (I know _SERVER will give me the name for non-cgi php) has the right to view a specific file. My dream is that I just need a short php script that just asks the webserver what the rights are. That seems a reasonable wish to me. If this is however not possible than I'd like a script that can
0
11290
by: asherwolf | last post by:
Hi, I'm trying to do something I think is pretty neat, but I've just about pulled my hair out by the behavior of my server. I'm hosting on GoDaddy, using a subdomain (www.mywebpage.com maps to /mywebpage), running Apache 1.3.33, and PHP 4.3.11. The goal is to use PHP to install an .htaccess file in a folder to forbid PHP files from being accessed from outside the server. I have a working .htaccess file in one folder, and I have been...
0
2138
by: deco12 | last post by:
Hi, I am new to web design and Apache, but have managed to password protect my site by putting a .htaccess file in the sites root directory. I have set it up so that a number of members have access to the site. Now, within the site I have a link to a "document" folder which I only want 2 or 3 members to have access to. I have created a second .htaccess & .ht passwd file, with the path to the new folder and a different password. This was...
29
2154
by: lenbell | last post by:
It's old stupid and lazy here again I have been wanting to keep using my WYSIWYG (What You See Is What You Get - for my fellow stupids) html editor. But I was told that you HAD to rename your files to .PHP so they would be parsed correctly. Oh contraire if you are hosted by Apache and have some access to the .htaccess file mechanism In my case through the "cPanel" and then "Apache Handlers"
0
10505
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10275
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10253
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10033
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7576
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.