How to use include_once in php with subdomains

Genesis5150
Genesis5150 used Ask the Experts™
on
Hello folks I have a file in http://mydomain.com/user/classes/check.class.php and would like it to be used in https://subdomain.mydomain.com using include_once How can I achieve this?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Fixer of Problems
Most Valuable Expert 2014
Commented:
Just use the root-relative version of the path and filename.
include_once "/user/classes/check.class.php";

Open in new window

Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
The include function doesn't know about domains or sub-domains. If your sub-domain is hosted on different server say then include won't work (unless the PHP setting allow_url_include is enabled - which it shouldn't be).

So, assuming they are on the same server then as Dave pointed out using a local path to the file will allow you to include that file.
Folder structure
Websites
      Main Domain
            Classes
                  check.class.php
      Sub Domain
            index.php

Open in new window

Then in index.php the relative path
include_once(../Main Domain/Classes/check.class.php');

Open in new window

Or the absolute path
include_once('/Websites/Main Domain/Classes/check.class.php');

Open in new window

Most Valuable Expert 2011
Top Expert 2016

Commented:
Sidebar note:  You may also want to know about this function:
http://php.net/manual/en/function.getcwd.php

And if you want to write your code in a way that allows for cross-platform functionality, consider using the context-aware predefined constant: DIRECTORY_SEPARATOR
Most Valuable Expert 2011
Top Expert 2016

Commented:
I don't know about the soon-to-be-extinct list.  My use of PHP goes back to 1999, so I encountered PHP on Windows 98, 2000, XP, ME, etc.  I have encountered the '/' problem in the past, when building a church web site circa 2000, and that was when I started using the constant.  Never looked back.
Most Valuable Expert 2011
Top Expert 2016

Commented:
I can't remember the exact timeline.  I changed to DIRECTORY_SEPARATOR long ago because the '/' did not work as desired on Windows hosting.   I can't really say when Microsoft made a change because DIRECTORY_SEPARATOR has worked perfectly from day one, and I never had any reason to change back.  IIRC, this was in the days of PHP4, but it's been a long time.

This page has some copyright information that seems to point to 2004, since PHP 4.0.6.  So I guess Microsoft changed behaviors some time after that.
https://pear.php.net/reference/PHP_Compat-latest/PHP_Compat/_PHP_Compat-1.6.0a3---Compat---Constant---DIRECTORY_SEPARATOR.php.html

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial