PHP - Deny folder access without direct link to file? - php

I am creating a file hosting website. I would not like people to access the directory
public_html/quick/files
unless they have a direct download link from uploading a file. How would I go about this?
For others: To rephrase, deny permission to index all the files.

Create a file named .htaccess in public_html folder with this content:
Options -Indexes

Creates a blank index.html file in the directories.
If thought necessary, create a redirect to an error page.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Your Page Title</title>
<meta http-equiv="REFRESH" content="0;url=<URL_ERROR_PAGE>"></HEAD>
<BODY>
Optional page text here.
</BODY>
</HTML>

Related

load an html page into/from a php page

i have a website that's mostly written in html, but i have to load it into a server that require the first page to be a index.php. How can i do that?
I have my index.html. is there a way to create a index.php that just load the index.html?
i tried
<html>
<head></head>
<body>
<?php include 'index.html'; ?>
</body>
</html>
but won't work.
i heard of get_file or to import header, but i'm clueless, i never used php. how should i do?
You can just rename the file to index.php. It doesn't require that any actual PHP code be in the file, the server's PHP engine will still process the file (pretty quickly too, since there's no server-side code to interpret) and show the HTML.
PHP
<?php
header( 'Location: http://www.redirect-location.com' );
exit();
?>
HTML
<meta http-equiv="refresh" content="1;url=http://www.redirect-location.com">
.htaccess ReWrite
RewriteEngine on
RewriteRule index\.html index.php [NC,R]
or
DirectoryIndex index.php index.html
In order for this to work you need to allow overrides for indexes in the apache config
AllowOverride indexes

PHP - Should I add a favicon.ico to avoid a 404 error?

I read that the browser is looking automatically for a favicon.ico - is that also the case in an Android WebView element? Should I include a favicon.ico to a PHP page like below, to avoid a 404 error in my logs?
<?
//PHP CODE...
?>
<!DOCTYPE html>
<head>
<title>Hello world</title>
</head>
<body>
<--
Content...
-->
</body>
</html>
This happens because (almost every) browser searches for a favicon by default. If the accessed page don't inform a valid URL for it, Apache uses the root directory.
You have two choices. You can create a website icon for each of your websites, or you tell Apache not to log that event as an error message.
1).
If you choose the second option, add the following code to each VirtualHost, or at least the ones which don’t have a favicon file:
Redirect 404 /favicon.ico
<Location /favicon.ico>
ErrorDocument 404 "No favicon"
</Location>
2).
There is no way to stop browsers requesting it. You can either create a favicon, or create a zero byte file called favicon.ico and place in the web root.
Yes, you can add an empty favicon.ico file. But of course the file itself, in the document root of your site, but not just a link in HTML code.
It'd be better to put favicon on the document root.
Just put dummy or blank favicon file into your page..sometimes it gives error if you keep blank path in favicon selector..

PHP block is not parsed in html

I am writing the following html code:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript">
function detectBrowser(){
if(navigator.appName === "Microsoft Internet Explorer"){
window.open("redirect.html", "_parent");
}
}
</script>
</head>
<body onload="detectBrowser()">
<div class="mainBody">
<?php
echo "test";
?>
</div>
</body>
</html>
But the php block doesn't display test for me. It looks like it is not parsed and I can see the php code in the webpage's source. Any one can tell the problem? Thank you
You can enable parsing of PHP in files with .html extensions in your httpd.conf file.
Look for a line like this and make sure .html is an option.
AddType application/x-httpd-php .php .php3 .phtml .html
You could probably add it to an .htaccess file in the docroot of you site as well, but I've not tried this personally.
You could change this in your .htaccess file. (if it doesn't exist just create the file (text only) in the same folder as your file)
AddType application/x-httpd-php .html
However this would make all static files also go through the PHP parser which is unnecessary.
I would suggest the best solution is to rename the file instead with extension .php. Then in your .htaccess file you could rewrite just this address, then you will be able to access it with the .html extension anyway.
RewriteEngine on
RewriteRule ^myfile.html$ myfile.php

How to autoload a site through a php file?

Is there a way to use a php file to automatically load a website.
I have a new version of my website, but I want to keep the current version live (with its current domain). I would just like to direct clients/customers to preview the new version but still be able to go to the current site.
For example: They would go to the current site by visiting www.currentsite.com. And to visit the new version they would visit www.currentsite.com/newversion
Both sites are subfolders within the root of my hosting account. How do I achieve this?
Thanks for any help on this question.
You probably want to take a look at .htaccess files:
Assuming your current website is located in /home/user/public_html/current and your new website is in /home/user/public_html/new, you could set up something like this:
RewriteEngine On
# Rewrite every uri that does not start with /newversion/ to the current website
RewriteCond %{REQUEST_URI} !^/newversion/?(.*)$
RewriteRule (.+) /current/$1 [NC,L]
# Rewrite everything else to /new
RewriteRule (.+) /new/$1 [NC,L]
Judging from your post and the fact that you are mentioning multiple folders in a hosting account, there is a pretty good chance you already have something like this setup for your current website.
Take a look in your root folder and see if there is a .htaccess file and start editing away.
If you're looking for a PHP-only solution, you can put an index.php or simmilar file at the root folder of the domain containing:
<?php
header("Location: http://www.currentsite.com/current");
?>
That would enable a redirection from http://www.currentsite.com/ to http://www.currentsite.com/current. The drawback is that you'd have to stick with "current" in your path, so I'd go for the more elegant .htaccess solution.
EDIT
Oh, and if you already have an .htaccess file in the root folder doing similar rewriting tasks, chances are this approach won't work as index.php won't ever be reached. You'd better check for that first.
Or create a simple index.html file in the "/newversion" directory with an iframe.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<body>
<iframe src="/" width="100%" height="100%"></iframe>
</body>
</html>

How to add a default splash screen without the use of .htaccess

I'm looking for a method to suspend access to a website without loosing the old files, by adding a file to the directory but not .htaccess, since the use of .htaccess has been disabled on the specific server. Are there other ways to do this by using php or another method?
rename your www-root directory and add another, empty one.
You have to rename your index.php, create a new one, and add:
<?PHP header("Location: http://disney.com"); // or something ?>
Also, you may want to check Apache's DirectoryIndex directive, the order of the "default page" is defined here. If your server have DirectoryIndex index.html index.php, the html extension is prior to the php, so You can create a file index.html with:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Jump page</title>
<meta http-equiv="REFRESH" content="0;url=http://disney.com"></HEAD>
<BODY>
Jumping
</BODY>
</HTML>
But if You want to "remove" all files recursively, You have to move the whole directory. Another option is to rewrite the url http://yourpage.com/hidden_directory/ to somewhere, but sadly You need .htaccess to do that, if You can't access the webserver's config file. If You can:
<Directory /path/to/dir/>
RewriteEngine On
RewriteRule ^/hidden_directory/$ http://disney.com [L}
</Directory>

Categories