Issues with php include in sub domain - php

I just uploaded my site but am having issues with sub domain and php include.
First I have a directory /main/ and all it content are okay in localhost and also when I uploaded online until when I decided to forward www.main.example to main directory.
Now if I browse it this way www.main.example files that I include will not work, and also my css, but I manage to add full site name to my css and js like www.example.com/css/page.css and is working. So can someone help me with php path or any htaccess code that can help me fix this?
I tried this to but not working
include_once('../_inc/functions.php');
include_once(__DIR__ . '/../_inc/userlocation.php');

Related

Cant link images on subdomain

Can someone explain why linking local image files doesnt work under subdomain.
I have a website:
http://example.com/site/index.php
Icons and images are under icons directory.
http://example.com/site/icons/icon.png
I include them in site like this:
<img src="icons/icon.png">
This works fine when accessing site from first url.
However, when i use subdomain:
http://site.example.com
Icons doesnt display anymore.
I notice that even in browser, it doesnt exist.
http://site.example.com/icons/icon.png
Happy new year!
You need to check where your subdomain is pointing to. It seems from your description that the folder which it is pointing to contains an index.php file that is identical to the index.php file in the site folder, which gives you the feeling that the subdomain is actually pointing to the site folder, but it's not.
You need to change the subdomain in the control panel to point to the value /site. Notice the forward slash.

Website will work in subdirectory but not root directory

I have a PHP site that I have been testing in a subdirectory /dev and everything is working great. I moved all the files over to my root directory and now it doesn't work! I have a config.php file that is handling the code so that I can easily change directories (or so I thought). Working page is here non-working page is here. It looks like all the files are being found but it is not being styled correctly, any troubleshooting tips or anything you guys see? I have been looking at it for days and can't find anything! Please help!
Here is my config.php code
<?php
// these two constants are used to create root-relative web addresses
// and absolute server paths throughout all the code
define("BASE_URL","/");
define("ROOT_PATH",$_SERVER["DOCUMENT_ROOT"] . "/dev/");
$site_root = $_SERVER["DOCUMENT_ROOT"] . "/dev/";
And...
<?php
// these two constants are used to create root-relative web addresses
// and absolute server paths throughout all the code
define("BASE_URL","/");
define("ROOT_PATH",$_SERVER["DOCUMENT_ROOT"] . "/");
$site_root = $_SERVER["DOCUMENT_ROOT"] . "/";
Your main issue is that you have /css/styles.css which differs from /dev/css/styles.css. In order to do a clean move and to make sure you don't have 2 different versions while you think they're same:
Delete everything from your root folder except the /dev/ folder.
Copy everything from your /dev/ folder into your root folder.
It's a good idea to learn about Apache's mod_rewrite, and then - use a framework. Isn't maintaining 10's or 100's index.php files a pain?
EDIT 1: In order to disable GoDaddy's caching mechanisms, follow this guide:
Log in to your Account Manager.
Click Web Hosting.
Next to the hosting account you want to use, click Launch.
From the Tools section, click Website Accelerator.
Click Settings.
Click the slider next to Developer Mode so it displays On.
Click Close.

Set-up include path for menu navigation so page links know where the site root is located in order to not break links

I have not been successful getting my remote server environment to behave like my testing server environment -nor visa versa- in regard to my navigations "include code" that I've included on my various pages (these pages reside inside other folders in the site).
My Menu shows up fine in the pages calling the include to this page this way...
<?php include("../includes/navigation.php"); ?>
But my MAMP testing server seems to want me to also add the root folder in addition to my provided link in each of the menu items, so I altered the link by adding my site root folder, like this...
<a href="/mysite/courses/list.php"
and navigation menu seemed to work, going inside one folder and back out into another (then I realized it was no good when I tested it on my remote server).
I got bad/broken links.
So I changed to something like this for the remote server...
<a href="/courses/list.php"
Links seem to work on the remote, but I needed to fix it so that the code I go with works on both the testing and remote servers...
So, I'm trying to ~~understand/control/and fix~~ my include_once code to provide proper menu navigation for all my various pages whether I am on my testing server or on my remote server and wether I am in one folder or another. Is this possible? And am I approaching this the right way?
(Currently, I have my root folder with an index page, then inside that, I have 3 folders with misc pages inside them, one of these folders (includes) holds my navigation.php page)
I have been searching for how to get my pages (located in various folders) to recognize where they were, in relation to the sites root folder. As a result of searching I concluded to insert this code into the list.php page in my "courses folder"...
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/includes/navigation.php";
include_once($path);
?>
on one of my pages and then tested it in my testing server (MAMP) - but now the menu does not even show up...I also tried...
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "../includes/navigation.php";
include_once($path);
?>
So I am seeking help in understanding - what am I missing in my understanding/execution to get the pages now to show the navigation menu as well as know where it is in relation to the root.
I would welcome any comments to help me understand how to connect the dots to solve this problem. As I read in another post saying to someone else there was a problem with where their site root folder was pointing (that it was pointing to the server root folder, not the site root folder)-
How would I know if this is what my problem is? How do you check this?
I had this same exact problem going on. What I did to solve it was instead of simply using "..includes/nav.php" I used an absolute path to the file. It should look something like this:
"var/www/html/includes/nav.php"
This specifies the exact route to the file. You can find this through your hosting file management page. Log into the file management, find the nav file within the folders, and use the exact url of the file.

Include php files when they are in different folders

Most of my website is in my root directory. And In that directory there is "css", "functions", "images" folder. Everything works fine when I include php files within index.php or any other root file. It includes it fine and executes it fine.
But problem occurres when I made folder "blog". So this is totally new and separate root folder with CMS and its own "root" files. And I try to include css from main root directory or some php files from "functions" folder in main root directory, Everything breaks down. I know I have to include it as ../functions/myfile.com. But this files includes some other files so it just wont work properly and won't be able to include other files properly.
Is there any idea how to fix this problem?
You can get to the root from within each site using $_SERVER['DOCUMENT_ROOT']. For testing ONLY you can echo out the path to make sure it's working, if you do it the right way. You NEVER want to show the local server paths for things like includes and requires.
Site 1
echo $_SERVER['DOCUMENT_ROOT']; //should be '/main_web_folder/';
Includes under site one would be at:
echo $_SERVER['DOCUMENT_ROOT'].'/includes/'; // should be '/main_web_folder/includes/';
Site 2
echo $_SERVER['DOCUMENT_ROOT']; //should be '/main_web_folder/blog/';
The actual code to access includes from site1 inside of site2 you would say:
include($_SERVER['DOCUMENT_ROOT'].'/../includes/file_from_site_1.php');
It will only use the relative path of the file executing the query if you try to access it by excluding the document root and the root slash:
//(not as fool-proof or non-platform specific)
include('../includes/file_from_site_1.php');
Included paths have no place in code on the front end (live) of the site anywhere, and should be secured and used in production environments only.
Additionally for URLs on the site itself you can make them relative to the domain. Browsers will automatically fill in the rest because they know which page they are looking at. So instead of:
<a href='http://www.__domain__name__here__.com/contact/'>Contact</a>
You should use:
<a href='/contact/'>Contact</a>
For good SEO you'll want to make sure that the URLs for the blog do not exist in the other domain, otherwise it may be marked as a duplicate site. With that being said you might also want to add a line to your robots.txt file for ONLY site1:
User-agent: *
Disallow: /blog/
Other possibilities:
Look up your IP address and include this snippet of code:
function is_dev(){
//use the external IP from Google.
//If you're hosting locally it's 127.0.01 unless you've changed it.
$ip_address='xxx.xxx.xxx.xxx';
if ($_SERVER['REMOTE_ADDR']==$ip_address){
return true;
} else {
return false;
}
}
if(is_dev()){
echo $_SERVER['DOCUMENT_ROOT'];
}
Remember if your ISP changes your IP, as in you have a DCHP Dynamic IP, you'll need to change the IP in that file to see the results. I would put that file in an include, then require it on pages for debugging.
If you're okay with modern methods like using the browser console log you could do this instead and view it in the browser's debugging interface:
if(is_dev()){
echo "<script>".PHP_EOL;
echo "console.log('".$_SERVER['DOCUMENT_ROOT']."');".PHP_EOL;
echo "</script>".PHP_EOL;
}
If I understand you correctly, You have two folders, one houses your php script that you want to include into a file that is in another folder?
If this is the case, you just have to follow the trail the right way.
Let's assume your folders are set up like this:
root
includes
php_scripts
script.php
blog
content
index.php
If this is the proposed folder structure, and you are trying to include the "Script.php" file into your "index.php" folder, you need to include it this way:
include("../../../includes/php_scripts/script.php");
The way I do it is visual. I put my mouse pointer on the index.php (looking at the file structure), then every time I go UP a folder, I type another "../" Then you have to make sure you go UP the folder structure ABOVE the folders that you want to start going DOWN into. After that, it's just normal folder hierarchy.
i had the same issue and found a code on https://css-tricks.com/php-include-from-root/ that fixed it
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/common/header.php";
include_once($path);
?>
None of the above answers fixed this issue for me.
I did it as following (Laravel with Ubuntu server):
<?php
$footerFile = '/var/www/website/main/resources/views/emails/elements/emailfooter.blade.php';
include($footerFile);
?>
Try to never use relative paths. Use a generic include where you assign the DocumentRoot server variable to a global variable, and construct absolute paths from there. Alternatively, for larger projects, consider implementing a PSR-0 SPL autoloader.

Resolve links in include file

I have a site located at http://www.mySite.com. My html/PHP pages are located at http://www.mySite.com/index.php I have created a 'tabs' include file located at http://www.mySite.com/includes/tabs.html. And all is grand!
But I have started to need a bit more functionality and have added pre-coded pages/packages, like a blog. These packages are located in their own directories like http://www.mySite.com/blog.
The problem is the from the new package directories the links in the tabs include are no longer pointing to the correct URL because of the directory change. I understand why this is happening but cannot figure out a work around other than maintaining 2 separate tabs files, which seems wrong.
I had a very similar question that dealt with the css links and solved by adding a slash before the stylesheet path in the stylesheet link. Unfortunately it does not work in this situation also because in the different directory the include is '../includes/tabs.html'.
The tabs link is a standard html link:
<li>Home</li>
Again in my root directory files it works fine but if I put a file in a new directory like "http://www.mysite.com/newDir/index.php" the tabs are pointing to "http://www.mysite.com/newDir/tabURL.php" not "http://www.mysite.com/index.php".
Thank you again for your time,
Todd
All you need to do is update your links in the tab.html file to use absolute paths instead of relative paths.
<li>Home</li>
try including the tabs file using the full path on server no in your site directory e.g. www/mysite/includes/tabs.html

Categories