Same file on multiple sites [duplicate] - php

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.

Related

What it mean when we perfix with dots for require function to indicate filename in php [duplicate]

This question already has answers here:
What is double dot(..) and single dot(.) in Linux?
(4 answers)
Closed 5 years ago.
I always use require('dbc.php'); to include file but what is the difference when I prefix 2 dots ../ as below, is there is any extra security.
require('../dbc.php');
require('../lib/bootstrap.php');
require_once '../../../conf/config.php';
If you do
../../
You've gone back two directory
../
You've gone back one directory
This basically going out the current directory the file u are working on is in. It depends on the location of the db file relative to the file that needs it. It has nothing to do with security.
The . gives you the ability to set the path of the included files relatively to the path of the original file that run (the file that included them). The ./ indicates the current directory. So if including a file like such:
require('./config.php')
You are telling PHP to look in the current directory for "config.php". Which is the same as
require('config.php')
The ../ indicates the directory above or "parent directory"
require('../dbc.php');
This is telling PHP to go one directory up and look for "dbc.php".
These commands can be chained like so:
require('../lib/bootstrap.php');
require_once '../../../conf/config.php';
The dots simply are used to traverse the directory structure. What is double dot(..) and single dot(.) in Linux?, though you should avoid using relative paths and use absolute paths. Absolute vs. relative paths.
Security:
In its self, it introduces no security benefits, except if you get it wrong your app won't work at all!
It does add some protection against code disclosure if PHP fails to parse. This applies ONLY if you store your main code outside of the webroot, though I have never encountered or seen this issue spontaneously happen, though it possibly could. Storing script files outside web root.

Referring to the root directory

Before I start, this question may have been asked before, but I either don't know what to type to find it specifically and this is a slightly more specific case.
I'm developing a website with many dynamic ties, some of which is at the beginning of every php file there is the line require("global.php"); which does what the name states, among others such as the css file and whatever else is found on the root level. Problem is however, upon entering down into another directory, the link to this file is broken.
Traditionally what I've done (which is an absolutely stupid beginner workaround) is create a variable just before the require called $up which contained the string '../' which recurred respectively depending on how many directories deep the file is from the root. So the line would then appear require($up."global.php");.
I've realised how stupid this is and have tried to find ways around this. One of which is using the variable $_SERVER['DOCUMENT_ROOT'] to print out the root directory but the reason why this won't work 100% is because a) I'm developing the website on a local machine where the 'root' of the website in development is located in a subdirectory on the server and b) it returns the entire path based upon the location on the drive starting from D:\ and working its location that way, which I don't particularly like to work with when it comes to developing websites and would rather remain within the hosted directories of the web server if possible.
So, based on what I've explained, is there an easy way to get the root location of a website (regardless if the project is in a subridectory or the real root of the web server) within a short string that is extremely easy to append to the begining of every file reference in every php file regardless of it's directory level?
Thanks! <3
Suppose you have the following directory:
/srv/website/index.php
/srv/website/lib/functions.php
You could then do this:
define("MY_ROOT", $_SERVER['DOCUMENT_ROOT']);
And use it in every (included) file relatively:
require (MY_ROOT . "lib/functions.php" );

PHP special symlink usage [duplicate]

This question already has answers here:
How to get the relative directory no matter from where it's included in PHP?
(5 answers)
Closed 8 years ago.
I have the following test structure:
/www/index.php
<?php
require_once(dirname(__FILE__).'/linked/linked.php');
/www/linked/ which is a symlink to /symlinkedfolder/
/symlinkedfolder/linked.php
<?php
echo __FILE__;
The output for this script is:
/symlinkedfolder/linked.php
Is there any way/technique with PHP or Apache or Linux which would make symlink behave not symlink instead like a normal filesystem folder/file?
I need that my example give back the following output:
/www/linked/linked.php
(But in real it would be still a symlinked file which originally located in its original folder)
UPDATE #1
We are working with version control system and we would like to keep the checked out folder in a global folder and we would like to symlink each folders to its proper path in the actual platform(Joomla or WordPress etc...). It would allow us to only update and commit from one folder, but still refresh every platform with a single update. (This could work until we not use FILE or DIR or any related things what symlink can mix up.)
It’s a pain. As the official PHP documentation explains:
The full path and filename of the file. If used inside an include, the
name of the included file is returned. Since PHP 4.0.2, FILE
always contains an absolute path with symlinks resolved whereas in
older versions it contained relative path under some circumstances.
Which is a pain. This is why I have decided it’s best to set a base path explicitly as I explain here. So in your case you would set:
$BASE_PATH = '/www/';
And then your require_once would be like this:
require($BASE_PATH . '/linked/linked.php');
This question & answer is similar to yours and recommends using $_SERVER['SCRIPT_NAME'] but in my experience, that setting can change radically between server to server for odd reasons. Which is why I have defaulted to the $BASE_PATH method when I code. You set it once, forget it & no worries.

can server document root be relied upon

I wrote the PHP code for my website almost 9 years ago. I have a config file called common.php. Each page in my site requires this page to access the site's constants, classes and variables.
I develop on a local machine and then upload files to a live site. The code I have used on my pages to call common.php is:
$main = ($_SERVER['HTTP_HOST'] == 'localhost')? "E:\-=Web=-\-=Sites=-\mysite\main\common.php" :"/home/mysite/public_html/main/common.php";
require_once($main);
This has worked fine and has never given me any issues.
However, I figure I could instead just use:
require_once($_SERVER["DOCUMENT_ROOT"]."/main/common.php")
This would make my code more compact and it also means I do not have to have my development site files always in E://
So my question is, is it good practice to use $_SERVER["DOCUMENT_ROOT"]?
Is this what most developers would do if wishing to call the config file on each page?
The most common and reliable way is to define a constant that contains the path to your code either in your index.php file, or a config.php file which is included from the same directory or a known relative path and reference that:
if (!defined('APP_DIR')) define('APP_DIR', __DIR__); // set the app directory to that of the currently executing file
Your require line for other files then becomes this
require_once(APP_DIR . '/main/common.php');
Now, you can guarantee that this constant will contain what you expect it to. Assuming you've set it right, that is. ;)

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