I'm using XAMPP on my home machine and also using it as a web server.
I have a main folder which is for my site, I then have a dev folder where I'm trying to work on a new site. (not sure if this is relevant info but worth mentioning)
On my local machine the site is c:/MySite/Dev
within Dev I have something along the lines of
Dev
assets
css
js
includes
footer
header
core
init
folder
subfolder
files.php
index.php
init is included at the top of every page, header and footer includes are also included. Within header is my nav bar.
I'm trying to define a site root so that I don't have to keep changing ../ and ../../ etc depending on the location of the files.
Also, seeing as my header gan be in many subfolders, the links to main index etc can get messy as index.php from a subfolder won't take me to the root index.php
I've tried using DIR and SERVER_ROOT bt of which I couldn't get to work properly, server root returned the c:/www and DIR returned the folder of the file in which it was called.
I'm still new to php so learning as I go.
This is what I've tried:
in init:
define("WEB", "http://mySite.com/Dev");
$environment = WEB;
echo $environment;
this works in the first instance, then when I'm in the subfolder and try using the home link I get :
http://mySite.com/Dev/Folder/Subfolder/%3Cbr%20/%3E%3Cb%3ENotice%3C/b%3E:%20%20Undefined%20variable:%20environment%20in%20%3Cb%3EC:/www/Dev/includes/header.php%3C/b%3E%20on%20line%20%3Cb%3E38%3C/b%3E%3Cbr%20/%3E/index.php
can anyone help please?
EDIT: The error after cleaning the output:
http://mySite.com/Dev/folder/subfolder/
Notice: Undefined variable: environment in C:/www/dev/includes/header.php on line 38
/index.php
Related
require_once($_SERVER['DOCUMENT_ROOT']."/Event_Logger.php");
global $Event_Logger;
$Event_Logger = new EventLogger();
Doing this displays a blank page and does not continue loading the page
The file on my server is located at public_html/Event_Logger.php
So I know I am doing this wrong $_SERVER['DOCUMENT_ROOT'] returns something I wasnt expecting so I am asking how do I get the top level of my site basically the folder public_html
You can't (and shouldn't) include a file using URLs. You should use the path to the file locally, on the same server. If the plugin you're building lives on technologyforthefuture.org, then you can require it from the root path. If you're trying to include the file from an external website, that's not possible.
I have phpbb in a directory of my joomla install which also happens to be inside a directory on the root (so that's me.domain.com/joomla/phpbb). I have successfully been able to rewrite joomla's directory to the root of a parked domain (me.domain.com/joomla/ --> domain2.com/ ) but when every I go to domain2.com/forum non of the style sheets work but domain2.com/joomla/forum works fine. Any help please?
Edit
I found that the css files werent being called properly. The page was calling from domain2.com/styles/ instead of from domain2.com/forum/styles/
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.
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.
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.