• 0

PHP to bypass .htaccess


Question

Hey guys, I've got a question...

I'm currently working on a site for a client that's done in PHP and MySQL. There is a support area that they need that will include files and documentation. Here's the deal:

Their files need to be protected. The support area will have a login prompt so that only customers can get into it. The thing is, I can easily protect the links to the files and documentation, obviously. What I want to do is protect the actual files that will be held in a different directory. They need to be protected with .htaccess so that no links can just be handed out to other people. I need to make it so that if the customer has already logged into the support area through the PHP script, they don't see any htaccess login prompt when they go to download the files.

So, is there any way that I can have PHP bypass the htaccess login?

Please help me ASAP... I need to finish this job up soon.

Thanks everybody!

Link to comment
https://www.neowin.net/forum/topic/280438-php-to-bypass-htaccess/
Share on other sites

19 answers to this question

Recommended Posts

  • 0

I have an idea, no clue if it would work, I'm not that great with any web stuff.

Why not just use a php file that just opens the file in the protected directory, but only if they are authorized. That way, you wouldn't even need to put anyone in the .htaccess file?

  • 0

The thing is, if you have a directory protected with .htaccess, then any links that send somebody to a file inside that directory will pull up the login prompt.

I don't want to use .htaccess to authenticate people, only to keep them out if they're not already logged in through my PHP script.

So, you're saying exactly what I want to do, but .htaccess will still cause a problem :/

I'm looking for something in PHP that will suppress the .htaccess login window.

  • 0

Ahhh.... true, I could do that.

How would I go about sending the user a file that's not in a public directory? Keep in mind, I believe some of these files are over 100MB.

Edited by fubarshibby
  • 0

Look, what you can do is redirect any queries to any file or directory in your folder to be redirected to your PHP-authentication file. From there you may access any file and output it as you like. (You even may link the user to the actual file, and have php run first, then look for the requested file and output it transparently for the user).

Now you need to know what to write into the .htaccess... I don't know much about apache syntax, but i think this may work:

RewriteEngine on
RewriteRule abs/patch/to/the/files/(.*) abs/path/to/the/script/yourScript.php?request=$1

I'm not completely sure this will work well... test around and take a look at this

Well you see it passes the name of the file to the 'request' get-parameter of your php scipt (which might be located anywhere else, not necesarily the same folder, this way you'd avoid that they pointed to your script inside the files folder ;)).

Well, try it and play around with the Apache commands.

  • 0

How would I output it to the user though? Would readfile() be feasible? Or would the htaccess file allow me to do a header() command and change the location without redirecting to the same script?

Thanks for all the help so far.

  • 0

Oh, that's easy.

header('Content-Disposition: attachment; filename="'.$filename."');
readfile('path/to/the/files/'.$filename);

You may have made some cheks to see if the file exists before, for security's sake. Maybe there's some urldecode() to do to the passed parameter, but I doubt that.

Edit: I recall there was a way to load anything BEFORE the actual file was loaded, this would be great to authenticate first and, depending on the results, send 403 headers or allow the file to be sent, but I can't seem to find it atm.

Edited by KeyStorm
  • 0

I can't see why, tbh. Maybe the process time limit may cut the data flow, but the best way to know... is to test it ;)

You may create a random crap sring and echo it to the browser as attachment. In the worst case there is a command in htaccess to change the time limit anyway for current call, so you may use that.

  • 0

Problem :(

The server that my client is hosted on doesn't seem to allow the RewriteEngine... I've used it before for a different site, so I know I'm not doing anything wrong. I also found out through the hosting provider's support pages that you can't even do your own .htaccess protection; you have to email them to get it protected.

Well, their current site's files aren't protected in any way, I just figured it would be better if I did this for them. I guess I just won't give direct links; I'll simply use the header command. I don't think their clients are going to be trying to gain access without permission because AFAIK my client hasn't had any problems yet.

  • 0

Now I feel really stupid: I just realized that they're on an IIS server and that's why there's no .htaccess :blush:

The funny thing is, I knew this, but I'd never really thought about it, you know?

Well I found out a way to get through it though; they have some software called iisProtect on there, which is what I wanted to get away from... But I figured out a way for it to work nicely with my PHP pages. So it's all good now.

Thanks for you help!

  • 0

Just so you know if there running iis6 instead of .htaccess use web.xml :) it does the same thing but on iis. Atm its not as rich as apache, but it has all the major and most used function capability. (iis7 says it will be just as rich as .htaccess)

I was about to say, if it was on apache php can control the authentication box, I cant remeber the code *damn* anyway cant be done on iis atm.

  • 0

why not use php to protect the info, have a form input and validate a password

<?php
$password = $_POST['password'];
$valid_pw = array('valid', 'passwords', 'in', 'this', 'array');  // you could do this with MySQL, or with just one password
if (in_array($password, $valid_ps)) {
 // the code for valid users here
}
else {
echo "Not authorized";
}
?>

  • 0
  j79zlr said:
why not use php to protect the info, have a form input and validate a password

<?php
$password = $_POST['password'];
$valid_pw = array('valid', 'passwords', 'in', 'this', 'array');  // you could do this with MySQL, or with just one password
if (in_array($password, $valid_ps)) {
 // the code for valid users here
}
else {
echo "Not authorized";
}
?>

585414562[/snapback]

I/They don't want the directories available to the public. Currently they're protected, using IISProtect software that they've got.

I just decided to go ahead and keep it, since it works just like .htaccess except it's got an entire administration area to have users, groups, access levels, etc... I just wanted to see if there was a way to do it myself.

  • 0

Nah, I've got it all covered. Besides, this way my client won't have to learn a new way of adding/deleting users and groups and everything. They'll be able to be comfortable with what they've got.

In any case, they're not paying me enough to have me try and create a whole new way to secure that area of the site. ;) Especially since it'll be fine the way it is.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Nvidia and Foxconn planning to deploy humanoid robots within months by Paul Hill Never mind intellectual work, Foxconn and Nvidia are now in talks to deploy humanoid robots at Foxconn’s new Houston factory to carry out physical work, Reuters has reported. The two companies want to have the robots operational by the first quarter of 2026. This is a big development for both companies; it marks the first time Nvidia products will be made with robot assistance, and the first time Foxconn has used robots in an AI server factory on a production line. While the development is certainly interesting, and potentially bad for factory workers, we do not know many of the details such as which robots are being planned for use at the factory, what they’ll look like, or how many will be deployed. While Foxconn has trialed robots made by Chinese firm UBTech, the report states that Foxconn is developing its own robots with Nvidia. The news was told to Reuters via unnamed sources who are not allowed to discuss the matter, so they did so under anonymity. They said that the robots will start work early next year and will contribute to the production of Nvidia’s GB300 AI servers. Foxconn has been training the robots to pick and place objects, insert cables, and do assembly work, but it’s not clear exactly what their role will be on the factory floor and whether jobs will be impacted. One of the sources that spoke to Reuters said that the Houston factory was the best place to trial the robots because it is more spacious, giving robots more room to move about. Last month, a Foxconn subsidiary, which is in charge of the group’s AI server business, said there were two robots being developed which are expected to be showcased in November. One of the robots will have legs, while the other will use a wheeled autonomous mobile robot base. Predictably, the latter version will cost less money than the one with legs, but pricing is unknown at this point. Reuters noted that these two businesses are not the only ones working on robots. It also said that Mercedes-Benz and BMW have been testing robots on production lines and that Tesla is developing its own robots too. China is also getting heavily invested in the sector. Jensen Huang, head of Nvidia, believes that they will be in wide use in manufacturing facilities within five years.
    • That's great and all, but humans have a finite range of vision and a rather slow reaction time. I live in a rural area too and the roads are painted with dear carcasses most of the year. It's not because people choose to hit them. A human simply can't react quick enough when they come firing out of the woods perpendicular to your vision. Those issues can and are being solved with machine vision. The industrial world has been doing this for many years already and cars are finally jumping on the bandwagon using the same technology. It's a known fact that driving is the most dangerous thing (most) people do every day, and that's solely because humans are driving. It's one thing to use technology to be lazy, but it's another to use it to be safe and save lives.
    • Microsoft doesn't make AMD drivers. The "AMD" driver on Windows update comes from AMD, its AMD decision to put it there.
    • While the Windows key shortcut is fine, I would argue that it's not as efficient as just using your mouse. It's a one-handed task vs two-handed. And with the keyboard, you trust that you don't hit a wrong key or that Windows can actually find what you're looking for. For me, Win 10 search struggled to find some things I had in the start menu, but Win 11 has seemingly fixed that. The main issue is that Microsoft did such a crappy job on the start menu since Win 8. The use of space is horrible and needs much more functionality. Most of all, it needs to be customizable. Having to rely on third-party apps to make it useful is really embarrassing.
  • Recent Achievements

    • First Post
      emptyother earned a badge
      First Post
    • Week One Done
      Crunchy6 earned a badge
      Week One Done
    • One Month Later
      KynanSEIT earned a badge
      One Month Later
    • One Month Later
      gowtham07 earned a badge
      One Month Later
    • Collaborator
      lethalman went up a rank
      Collaborator
  • Popular Contributors

    1. 1
      +primortal
      675
    2. 2
      ATLien_0
      275
    3. 3
      Michael Scrip
      218
    4. 4
      +FloatingFatMan
      165
    5. 5
      Steven P.
      162
  • Tell a friend

    Love Neowin? Tell a friend!