First time asking a question. PHP Amateur.
Details: I am working on an eCommerce web project. The following is code contained in a file called config.php
in a folder titled 'inc
'. Its purpose to define file and directory paths so that they need not be repeated when using the incude()
function in other files.
Problem: The CSS fails to render and also links break when hosted on XAMPP or the campus web server, but when hosted on a proper domain e.g www.myproject.com, it displays perfectly fine and works without any hiccups.
Additional Information: The private campus web server takes the following address format 192.168.170.15/62631
, with the last section, '62631', being the student's folder on which the project is hosted.
<?php
if(!isset($_SESSION)) {
session_start();
}
// site domain name with http
defined("SITE_URL")
|| define("SITE_URL", "http://".$_SERVER['SERVER_NAME']);
// directory separator
defined("DS")
|| define("DS", DIRECTORY_SEPARATOR);
// root path
defined("ROOT_PATH")
|| define("ROOT_PATH", realpath(dirname(__FILE__) . DS."..".DS));
// classes folder
defined("CLASSES_DIR")
|| define("CLASSES_DIR", "classes");
// pages directory
defined("PAGES_DIR")
|| define("PAGES_DIR", "pages");
// modules folder
defined("MOD_DIR")
|| define("MOD_DIR", "mod");
// inc folder
defined("INC_DIR")
|| define("INC_DIR", "inc");
// templates folder
defined("TEMPLATE_DIR")
|| define("TEMPLATE_DIR", "template");
// emails path
defined("EMAILS_PATH")
|| define("EMAILS_PATH", ROOT_PATH.DS."emails");
// catalogue images path
defined("CATALOGUE_PATH")
|| define("CATALOGUE_PATH", ROOT_PATH.DS."media".DS."catalogue");
// add all above directories to the include path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(ROOT_PATH.DS.CLASSES_DIR),
realpath(ROOT_PATH.DS.PAGES_DIR),
realpath(ROOT_PATH.DS.MOD_DIR),
realpath(ROOT_PATH.DS.INC_DIR),
realpath(ROOT_PATH.DS.TEMPLATE_DIR),
get_include_path()
)));
Some additional information: There is only one other file in the 'inc
' folder named autoload.php
. It interacts with the config.php
script as follows:
<?php
require_once('config.php');
function __autoload($class_name) {
$class = explode("_", $class_name);
$path = implode("/", $class).".php";
require_once($path);
}
Another instance that's similar to the xampp and campus server problem: The files are hosted in a sub-domain that points to a a folder named 'project'. When loaded as project.site.com it works fine, but when site.com/project is typed in, the same problem is encountered.
I guess, problem is in defined("SITE_URL")
. Echo the $_SERVER['SERVER_NAME']
& see, you will get only 192.168.170.15 not 62631.
defined("SITE_URL")
|| define("SITE_URL", "http://".$_SERVER['SERVER_NAME']."/62631");
You may be don't need this answer anymore, but I am answering.
I am having the same problem. So I tried again and again, then found the problem and solve. Everything is right in the code except
require_once('config.php');
Please replace that with
require_once('inc/config.php');
Now you are done. Because it wasn't getting the config file.
As the function by which you can call only the file name instead of additional path directory is defined in the config file. So, it will not work for it's own.