How do I prevent public access to php include files? [duplicate] - php

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
APACHE, prevent users from accessing include files?
How do I prevent public access to php include files?
Let's say that I have a header.php, footer.php, a folder with multiple include files and I don't want users to access them directly by typing the filenames in the address bar. What is the best way to prevent this?
Thank you for your help

Move them outside of the web root.
Eg my web root is htdocs
.
├── htdocs
└── php
And php is outside of it
I include files like this
<?php
include($_SERVER["DOCUMENT_ROOT"] . "/../php/prepend.php");
include($_SERVER["DOCUMENT_ROOT"] . "/../php/common.inc");

Related

Azure PHP hosting, Include files from a parent directory [duplicate]

This question already has answers here:
PHP include file. Showing error file not found
(3 answers)
Php include not working? function not being included
(6 answers)
PHP include not loading file
(1 answer)
Closed 3 days ago.
My php Web app is not reading files correctly on azure. How do I fix this ? The web app correctly works in my computer.
The file directory
mainFolder->common_files (common files have all the files used by all the web pages)
mainFolder->pages (pages will have all the .php files except index.php)
mainFolder-> index.php
The index.php files works correctly. However, the .php files inside the "pages" folder does not go to the common_files folder.
I used the following code to include those files from common_files folder:
(The .php files inside "pages" folder use this code)
<head>
<?php require('../common_files/headfiles.php') ?>
</head>
The webpage should load files from o:
https://glidingcub.azurewebsites.net/common_files/
But try to load from:
https://glidingcub.azurewebsites.net/pages/common_files/
How do I fix this ?

How to correctly call a require_once using PHP [duplicate]

This question already has answers here:
PHP display current server path
(5 answers)
PHP absolute path to root
(8 answers)
How do you know the correct path to use in a PHP require_once() statement
(9 answers)
Closed 3 years ago.
I am absolutely terrible with using 'require', 'include' and 'require_once' to locate my files and include them inside other .php files and would like some assistance.
The class where it's located:
htdocs (Main)
- wp-content
-- mu-plugins
--- extensions
---- _process
----- pp
------ classes
------- Control.php (Class)
Now I need to require_once the class 'Control.php' in the following structure:
htdocs (Main)
- wp-content
-- mu-plugins
--- extensions
---- _process
----- oc
------ run.php (Need to call inside this file)
How would I be able to achieve this? I've tried everything and can't figure out how to call files and structures.
I've tried this and it didn't work inside run.php:
require_once (__DIR__.'/../pp/classes/Control.php');
Personally, when I have to make the include or require of the case, I create a constant and use the global variable SERVER.
Example.
define( "PATH", $_SERVER['DOCUMENT_ROOT']);
require PATH . "/wp-content/mu-plugins/extensions/_process/pp/classes/Control.php"
In this way, using the SERVER variable, your path will always start from the main root of the site.
I hope it helps you.
Having said that, being in Wordpress, why don't you use the wp-load.php file?
By including that file in your project, you automatically have everything that is part of wordpress available to you. Think about it, it might come in handy later in the other files.
Enjoy :)

PHP refer to certain location in Filesystem (../ or ./) [duplicate]

This question already has answers here:
What does the dot-slash do to PHP include calls?
(7 answers)
Closed 7 years ago.
What's the effective difference between:
include_once("../backend/example.php");
and
include_once("./backend/example.php");
My problem is that on my development-environment (XAMPP-Server) i had to use "./". But when i tried to upload my progress to the production-server, i had to change the paths to "../".
Thanks for your answer!
./ refers to the current working directory (It's basically redundant, but it reiterates that you're starting in the current directory, and not at the root / folder, or in PHP's case it may try other folders to find the file.). To see which directory that is, you can use the getcwd() function. ../ basically instructs php to go back to the parent folder and then into backend/example.php.
A few examples:
Let's say this is your root, and your cwd: /var/www/mywebsite/
../backend/example.php would refer to: /var/www/backend/example.php
./backend/example.php (And also just backend/example.php) would refer to: /var/www/mywebsite/backend/example.php
./= Actual folder
../= one folder back
you can define website path in config
like this
$path = 'http://yourwebsite.com'
Than you can do like this :)
include($path"/backend/example.php");

Same file on multiple sites [duplicate]

This question already has an answer here:
What is the best way to execute the same php script on different server?
(1 answer)
Closed 9 years ago.
Let's say I have 2 domain names 123.com and abc.com all on the same 1and1 server.
123.com is in a folder called 123 and abc.com is in a folder named abc.
How would I include a file stored in 123 into a page on the abc.com site.
I used to do this with PHP
<?php
$code = file_get_contents("http://www.123.com/file.html");
eval('?>' . $code);
?>
but it was stopped with PHP5 to prevent abuse.
Basically, I want to be able to edit one file of html and have it change on multiple sites.
I thought that
<?php include($_SERVER["DOCUMENT_ROOT"]/123/file.html); ?>
would do it, but it's not going to my server root, just the domain's root.
Is it possible to use php to get info from sibling folders on a server?
Thanks
Richard
Yes, you want the require() or include() function (or require_once()). See http://nl3.php.net/require .
This is also much faster btw, because there is no http connection being setup, but only the internal file system is being queried.
-- EDIT
oh wait, I see you've already found the include function. I think you should use either absolute paths, or use the '..' in the path:
include('/full/path/to/the/directory/file.html');
include($_SERVER['DOCUMENT_ROOT'] .'../123/file.html');
I don't know 1and1 and what they allow you to do... What I would suggest is have a 3rd folder at the same place as your dmain folders. Call it common. Then in each of your domains folders, where it better suits your needs, create a directory symlink to the common folder we just created...
Basically, it will do this: (absolute blind approximation of actual server)
1and1/your_account/123/common -> 1and1/your_account/common <- 1and1/your_account/abc/common
Also you can use symlink so you can open file from both directories.

wordpress root dir [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Wordpress root directory
I need to write a file in the root directory on a wordpress installation. This need to be in the same place as the wp-config.php file.
But i need a way to call the root url.
Is there a wp_root_dir() like function
Do you want root url or root dir?
Given that you're putting this file into the root of your wordpress I assume its not a plugin or something like that.
Here is how to get the root url
<?php echo get_bloginfo('url'); ?>
Otherwise if you want the path (to include() for instance) its perfectly reasonable to just do:
../../../file-in-root.php -> as many ../s as you need to go back directories
I do this all the time. If you think your directory structure may change a lot, you can just put the whole path to the file e.g.
/home/w/o/wordpress/web/public_html/file-in-root.php
If you're creating a plugin and you don't know the path to the file then obviously it would be different, but there are wordpress functions for that. However as you're using root I guess this is just for your own server/website configuration and this will be fine.
If you mean on the client side (http://www.myreallyawesomewebsitethatuseseordpress.com/blog/wordpress/), you can use get_bloginfo('url'). This will return the url which wordpress is installed.
If you mean on the server side, you can use the ABSPATH variable.
Usage:
Client side:
<?php
echo "You are using this site. Wordpress is on" . get_bloginfo('url');
?>
Server side:
<?php
echo "On the server, Wordpress is installed on" . ABSPATH;
?>

Categories