is there any way to use domain file in subdomain to? - php

i have a site which has a subdomain, for example domian.com and forum.domain.com.
in my domain.com i have inc folder which contain my initialize.php and config.php my classes and functions in it. i just include initialize.php in each php page and other pages included in initialize.php like config.php
in config.php i define
define("BASE_URL","/");
define("ROOT_PATH",$_SERVER['DOCUMENT_ROOT']."/")
now when I include initialize in subdomain it dosen't work because ROOT_PATH assumed as subdomain. in this situation what sholud I do? how can I use files that are in domian.com root in subdomain?
should I copy all off them in subdomain?

It can be done with a simple trick like following,
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/../inc/header.php';
I recommend you to read the following tutorial
https://developer.hyvor.com/php/including-files-in-root-with-subdomain.php

Related

Getting to main directory from subdomain folder via $_Server['document_root']

So my folder is called
leaguenotes and inside that folder i have a folder administration which is my subdomain folder so
leaguenotes
-index.php
-js
-files.js
-administration
-index.php
-otherfiles.php
And from administration index I'm trying to include in this example files.js with <?php echo '<script src="'.dirname($_SERVER['DOCUMENT_ROOT']).'/js/jquery.min.js"></script>'; ?>
$_SERVER['DOCUMENT_ROOT'] = /var/www/html/leaguenotes/administration which is the subdomain folder so I'm using dirname to get to parent folder /var/www/html/leaguenotes and add /js/jquery.min.js and still the file is not found
if you access your page, take a look at the html source-code, i think you will see the source produced from your php code is not the correct one. because a real subdomain is a different domain then your domain itself. It is treated by your browser as a different domain.

How to include a php file into two others which are in different directories ? (not the other way around!)

I'm having a problem with the folowing:
I have a file called config.php, which I include in two other pages: login.php and index.php. The pages are organized as follows:
index.php
Config/config.php
Page/Users/Login/login.php
lib/smarty.php
My problem, is that, within config.php, I have another include to a different file: smarty.php.
But because index.php and login.php are not on the same level, the smarty file only works for one of them:
Example : if I have, in config.php:
include('lib/smarty.php')
then it only works for index.php
if I have include('../../../lib/smarty.php')
then it only works for login.php
Is there a way for me to use the same config.php for both login.php and index.php and have smarty.php work for both?
I hope I was clear, and thank you in advance
NOTE: The problem is not how to include files from different folders into another, it is to include one file into two different pages in different directories and with different paths.
Use dirname(__FILE__), or if available, __DIR__ (PHP >= 5.3). They resolve to the full path of the directory the current file is in. It is better than explicitly using the current absolute path to the file because it allows you to move the files in the file system (as long as the files stay in the same location, relative to each other).
You'll have to change Config/config.php to include lib/smarty.php like so:
include(dirname(__FILE__) . '/../lib/smarty.php');
While you're at it, you should probably change your other include/require to use a similar construct.
Use the absolute path in all of your includes.
like:
include '/var/www/html/Config/config.php';

PHP include file from root when using sub domain

I have a website with a subdomain (my.domain.com)
my main website uses
include 'directory/page.php';
and on the subdomain i want to be able to include the same file, i have tried:
include realpath(dirname($_SERVER["SCRIPT_FILENAME"]) . '/../..').'/directory/page.php';
but i get an error saying the file cannot be found.
My directory structure looks like:
www.domain.com - /home/username/public_html
my.domain.com - /home/username/public_html/my
how can i include a file in the public_html directory in the my. subdomain?
include dirname(__DIR__).'/directory/page.php
Refer this to learn more
Simply,
include_once $_SERVER['DOCUMENT_ROOT'] . '/../directory/page.php';

PHP Require In different folders

Okay so on my website I have a scripts folder which includes a php file which connects to mysql server so If I move a database then It will change it on all the files which are connect to the database.
I also have another folder called templates. In that folder there is a top for the header and the footer. In the header template I wrote:
require("../scripts/connect.php");
And I have another folder called, category. And that folder includes the header and the header includes connect. But then it displays and error that there is no such files.
Please help. Thank you
A good practice is to include a main config in all running php files, usually called config.php :)
in this config file create a constant called SITE_ROOT or something similar that point to the exact folder like this
define("SITE_ROOT", "/var/www/mysite");
Then on any include, include_once, require, require_once use it like this:
require(SITE_ROOT."/scripts/connect.php");
This should solve any relative path drama
You shouldn't use relative paths with the include/require, but use a constant defining the ROOT_PATH of your website.
Example:
In all the files calling needing includes:
define(ROOT_PATH, '../');
include ROOT_PATH . '/scripts/connect.php';
And in /scripts/connect.php (and all the other files that will be included somewhere), all the includes should use ROOT_PATH (without defining it).

Relative path doesnt work after include?

I have made a config file that includes te database information (connection, and more).
The config file is called by index.php. So I have this:
index.php->calls->config.php->calls->db.php
This works, but sometimes the config.php is not called by the index.php but by for example header.php. The problem is that the relative path in the config.php to the db.php doesn't work.
How can I solve that? Should I use absolute paths?
Thanks!
If all the files are located in the same directory, you can include it like this:
include __DIR__."/config.php";
Otherwise create a constant in your index.php page before all includes that define your application path, and use that to include your files, E.G:
in index.php:
define('APP', __DIR__);
include APP.'/config/config.php';
in config.php:
include APP.'/lib/db.php';

Categories