File with important path definition in PHP - php

I'm trying to create file configuration.php with all path's defiitions.
First, there is the autoload.php file included in index.php just after page and session is started.
require_once $_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'configuration.php';
Const. DIRECTORY_SEPARATOR is default "/" but I tried set DS manualy as "/" too.
Then, in configuration.php:
define('DS', DIRECTORY_SEPARATOR);
define('DIR_BASE', $_SERVER['DOCUMENT_ROOT']);
define('DIR_CONFIG', $_SERVER['SCRIPT_FILENAME']);
define('DIR_APP', DIR_BASE.DS.'app');
define('DIR_ASSETS', DIR_BASE.DS.'assets');
define('DIR_CSS', DIR_ASSETS.DS.'css');
But path generated this way looks following:
<link rel="stylesheet" href="C:/xampp/htdocs/pro_name\assets\css/vendor/bootstrap.min.css">
<link rel="stylesheet" href="C:/xampp/htdocs/pro_name\assets\css/vendor/font-awesome.min.css">
<link rel="stylesheet" href="C:/xampp/htdocs/pro_name\assets\css/app.css">
<link rel="stylesheet" href="C:/xampp/htdocs/pro_name\assets\css/debug.css">
and in Chrome Developer Tool I get for example:
Not allowed to load local resource: file:///C:/xampp/htdocs/pro_name/assets/css/vendor/bootstrap.min.css
I want to create universal paths for localhost and production server. Where I made mistake?

The question doesn't specify this, but I suspect the actual problem is that the production system runs on website.com/index.php and the development system is using localhost/w8/index.php. This extra folder on the dev system causes the issue when the autoloader is set to use / as the project root.
The issue with the chrome dev tools is caused by using $_SERVER['DOCUMENT_ROOT'] which returns the filesystem path instead of the relative website folder path.
To solve this, you need to replicate the production system on your localhost. You can do this by setting a hosts entry to something like project.local and setting up apache to serve the htdocs/w8/ folder when accessing this domain.
Use this guide to modify your hosts file to have the following entry:
127.0.0.1 project.local
Then, add an entry to your apache config to allow requests to the project.local domain:
<VirtualHost project.local:80>
DocumentRoot "C:/xampp/htdocs/w8/"
ServerName project.local
</VirtualHost>
NOTE: This apache config is untested! Your best bet is to copy the existing virtualhost, change * to project.local, and to modify the document root to be the w8 folder instead of the entire htdocs folder.
Now, you should be able to access your project by opening http://project.local in your browser. You can then set / as your DIR_BASE and it should work on both your production server and your test system (as long as you use the alias domain http://project.local when working locally).
I haven't actually tested this, but these are the steps you need to follow to get it working.

Related

Why the path beginning with '/' points to 'C:/'?

I have created a site in localhost in the path C:/xampp/htdocs/seintian.altervista.org and then I hosted it in a hosting platform (Altervista).
After some months of programming in that directory I changed the root directory of the site in localhost to, simply, C:/xampp/htdocs because in that way I thought that I would have been able to run the site by searching http://localhost/ and, also, to use the paths /sub/dir/of/the/site instead of the relative ones like ../sub/dir/of/the/site, etc.
However, they somehow point to the path C:/ instead of C:/xampp/htdocs that is the DOCUMENT_ROOT of the Apache site (the local hosting is powered by XAMPP).
Plus, I tried to upload the server folder to Altervista, to see if there worked, but - and you can check by yourselves here - also there it didn't work, responding with a "file or directory not found at [...]" error.
Is it normal that /path/to/file points to C:/ even if the DOCUMENT_ROOT constant is set to C:/xampp/htdocs? And, in any case, how can I make possible that paths like /sub/dir/of/the/site point to the DOCUMENT_ROOT, how expected?
PS: just to say, some paths in link elements in the head section of the page, point correctly to the right path, for example the assets of the page situated in C:/xampp/htdocs/assets. how is it possible? Does PHP just hate me? :'(
Thanks in advance <3
/... points to default storage, so in your case it's C:.
While links in webpage /... links to root directory of http server (do not mix directory path with URL path, they are not the same)
So these are pointing to same location, but has different meaning:
file_get_contents('/etc/dir/xampp/htdocs/assets/images/a.js');
<html>
<head>
<script src="/assets/images/a.js"></script>
I personally suggest to make your site portable by appending __DIR__ to all paths that are used in PHP:
// C:/xampp/htdocs/index.php
ROOT_DIR = __DIR__;
...
file_get_contents(ROOT_DIR . '/assets/images/a.js');

Not allowed to load local resource PHP Xampp

I am trying make bootstrap.php file that gets called everytime before i open a file, like my css. And i do this since i use the header in every page i need to have all of the links working without having to do multiple header depending on where in my folder structure i am. And this is the error:
Not allowed to load local resource: file:///C:/xampp/htdocs/PHP/assets/css/bootstrap.css
And here is my file structure.
index.php
include ('bootstrap.php');
include (FOLDER_MAIN_HTML.'header.php');
bootstrap.php
define ('FOLDER_ROOT', 'C:/xampp/htdocs/PHP/');
define ('FOLDER_CSS', FOLDER_ROOT.'assets/css/');
header.php
<link rel="stylesheet" href="<?php echo FOLDER_CSS;?>bootstrap.css">
Dont know if its neccesarry but this is my httpd.conf in xampp
DocumentRoot "C:/xampp/htdocs/PHP"
<Directory "C:/xampp/htdocs/PHP">
So thats what i have but it doesent work. Even when when i switch the root folder to __DIR__ it still says no allowed. So do i need a htaccess file, and if so how do i do that? Or is there some other way.
And yes i have searched everywhere for a fix, and the code i have right now is from another post, from here.

CAKEPHP - Change default path to webroot

I'm new to cakephp and I'm having some problems with setting up a local development server. I have my cake install located at http://localhost/dropbox/my_site/. However, when I try to visit that url, it tells me the dropbox controller isn't set up. How do I tell CakePHP to start in my_site rather than /localhost/?
I've tried adding connect(/localhost/dropbox/*) to the routes, but it seems like it still looks for models in the wrong location.
I tried editing index.php in app/webroot but all the examples show how to write the directory in linux format rather than windows, so I'm not sure how to structure 'ROOT'
CakePHP will work happily in a subdirectory - I have several Cake sites running at http://localhost/{appname} on my dev machine.
Cake defines its ROOT directory in the root index.php file. If you look inside you'll see the following lines:
define('ROOT', dirname(__FILE__));
define('WEBROOT_DIR', 'webroot');
define('WWW_ROOT', ROOT . DS . APP_DIR . DS . WEBROOT_DIR . DS);
Since it's taking ROOT from dirname(__FILE__), it will always point to that file's location.
I suspect you have problems in your routing file. Did you create any custom routing rules to account for being located in a subdirectory? If you did, your cake install may be trying to access http://localhost/dropbox/my_site/dropbox/... and that's why you're getting that error.
This likely doesn't have so much to do with CakePHP as it does with your web server.
If dropbox is your document root, it should be mapped accordingly in your web server configuration. For example using DocumentRoot in Apache.

XAMPP Local installation File Path problems

I like to work on websites locally before uploading to my host. I use PHP/MYSQL servers in an XAMPP install.
I have multiple directories in XAMPP htdocs directory (one for each project). Each project usually has at least:
header.php
index.php
footer.php
styles/stylesheet.css
This worked fine until recently.
I am now working on a more extensive file/directory structure. Now, when /about/index.php calls header.php, the path to the stylesheet directory doesn't point in the right direction. Image paths no longer point in the right place either since they are all relative paths.
I tried pointing everything to the home directory first using a "/" at the beginning of every path, but in XAMPP the home directory now refers to localhost, instead of the directory for the particular project.
What is the solution? Is there a better way to be working on projects locally so I can upload to my web host simply, using all relative paths and not having to change them for live and dev versions of the website?
The simplest solution as answered by #Vladimir Dimitrov in this thread goes as following (I will just copy it here):
The easiest way is to create separate virtual host for each site folder in /htdocs So you will access the http:// mysite.local instead of http:// localhost/mysite
There are two things to do: 1. edit C:\xampp\apache\conf\extra\httpd-vhosts.conf (by default) adding something like:
<VirtualHost *:80>
ServerName mysite.local
DocumentRoot C:/XAMPP/htdocs/mysite
</VirtualHost>
edit c:\windows\system32\drivers\etc\hosts adding
127.0.0.1 mysite.local
restart xampp and try http://mysite.local
Possibly this helps you?
You have to edit some config to reference each site to a base-link.
creating-multiple-sites-on-a-local-web-server
You could try this:
create a common configuration file
define a BASE_URL constant to your home directory (e.g. http://localhost/my_project/)
in your templates use all your links and references with BASE_URL
When you will deploy, you will need to change only one file.
You could also set a BASE_PATH constant to your directory (e.g. c:/xampp/htdocs/my_project). This might be useful when trying to include scripts from various sub-directories, without "guessing" the local path. (e.g. include BASE_PATH . 'templates/my_template.php')
Try including them using `DOCUMENT_ROOT for your PHP files, ie:
include($_SERVER['DOCUMENT_ROOT']."folder/header.php");
This assumes, when looking in the browser, header.php can be ound by going http://127.0.0.1/folder/header.php
For other files, such as CSS, Javascript you could define the location as follows:
define("SCRIPTS_URL", "http://127.0.0.1/_scripts/");
Include the above in your header.php file, and make sure you include header.php before calling the actual html header, eg:
<?php
include($_SERVER['DOCUMENT_ROOT']."folder/header.php");
?>
<html>
<link rel="stylesheet" type="text/css" href="<?php echo SCRIPTS_URL; ?>stylesheet.css">
... etc etc ...
You can further combine define and build up directory parts, for example:
$project = "project_x";
include($_SERVER['DOCUMENT_ROOT'].$project."/header.php");
define("SCRIPTS_URL", "http://127.0.0.1/".$project."/_scripts/");
If you do it like above, then you only need to change the project variable, if you see...
Update
The below would be index.php:
<?php
// Make the header relative to index.php (as we don't know the project) - assume header is located at /_template/header.php and this file is located at /index.php [if, in future you have /content/index.php - then the below would be ../_template/header.php, etc]
if(file_exists("_template/header.php")){
include_once("_template/header.php");
} else {
die('Fatal error - no header found');
}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="<?php echo BASE_URL; ?>styles/stylesheet.css">
</head>
<body>
// Content goes here
</body>
</html>
<?php
if(file_exists(ROOTPATH."_template/footer.php")){
include_once(ROOTPATH."_template/footer.php");
}
?>
And header.php:
<?php
define("PROJECT_NAME", "project_x");
define("ROOTPATH", $_SERVER['DOCUMENT_ROOT'].PROJECT_NAME."/");
define("BASE_URL", "http://".$_SERVER['SERVER_NAME']."/".PROJECT_NAME."/"); // $_SERVER['SERVER_NAME'] automatically puts 'localhost' or the domain name in automatically
?>
As you can see - everything is defined in this header file and when it is included on index.php - index.php can access those definitions, as can any other file that is included after the definition has been made (note that you cannot overwrite a definition and cannot define the same definition twice.
I solve this problem by defining some constants:
# index.php
# handles pretty much everything for the site
define( 'DS', DIRECTORY_SEPARATOR );
define( 'ROOT', dirname(__file__) );
define( 'HOST', 'http://' . $_SERVER['HTTP_HOST'] . dirname( $_SERVER['PHP_SELF'] ) );
Then, for includes, I do something like this:
include ROOT . DS . 'directory' . DS . 'file_i_want.php';
For CSS and whatnot, it may be easier to just set the base URL in the markup.

Apache Virtualhosts, stopping PHP throwing errors with "/"

I have a pretty stable development machine set up running Apache and using virtual hosts to keep my projects separate, and running a dyndns.org service which I use to access them. Each VHost directive typically looks like this:
<VirtualHost *:80>
ServerName [my_internal_subdomain].[my_dyndns_name].dyndns.org
ServerAlias home
DocumentRoot "D:/webserver/[projectDirectory]/httpdocs"
php_admin_value open_basedir "c:/WINDOWS/TEMP;D:/webserver/[projectDirectory]/"
ErrorLog "D:/webserver/[projectDirectory]/logs/error.log"
TransferLog "D:/webserver/[projectDirectory]/logs/access.log"
</VirtualHost>
Obviously [projectDirectory], [my_internal_subdomain] and [my_dyndns_name] are all values I know/change for each directive, just no need to post them here :)
One thing has always confounded me however, in PHP, if I want to require a file from say /includes/, I would expect to use:
require("/includes/myfile.php");
Except doing so throws an open_basedir restriction error - because "/" is trying to go, presumably to the root of D:/ - whereas I would like each virtual host to recognise that the value of DocumentRoot is in fact where I would like PHP to go if I call a file by "/".
This is how my production server seems to work, and its a bit of a pain having to code everything to work out what the relative path to /includes/ is for the benefit of my dev machine.
I expect this is a fairly simple/obvious directive I'm missing but I can't find it. Can anyone help?
Do you mean this?
require($_SERVER['DOCUMENT_ROOT'] . '/includes/myfile.php');
require paths takes filesystem paths so "/" will always be the root of the filesystem OR paths that relate to your include_path setting. You can change your includes slightly and set the include path like this to get around it:
ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . 'D:/.../httpdocs');
require('includes/myfile.php');
This will work because it will find the includes folder in your include_path.
PHP by itself has no real concept of a DocumentRoot for its file-related functions. It seems only the file system of the server. The whole DocumentRoot business is something applied at the webserver layer.
If you want to use require('/....') in your code, you'd need to use mod_chroot (or mod_security's chroot functionality), so you can "jail" each vhost into its own directory tree, and make the / directory be whatever you want for each one.
However, chrooting has its own problems - it would lock off the rest of the file system. If your script exec()'s something externally, or connects to MySQL, or does anything on the filesystem, you'd need to have copies of those executables, .so libraries, MySQL socket file, etc... within the jail.
In linux it's more common to have a 'chrooted' environment then in windows. You may determine your root by setting a configuration directive from a file in your root and get the directory name like this
$dirname = dirname(__FILE__);
// or however you wish to arrange your affairs.
$myConfigObject->set('root',$dirname);
Edit: This approach does not nescesairly use the document root, however I find that in many cases I will have multiple instances of the same project, and that most of my php files will be outside the document root for security reasons.
It will be something along the lines of this:
/home/dir (project root
/home/dir/stable (project instance root)
/home/dir/dev (project instance root2)
/home/dir/stable/lib (php files)
/home/dir/stable/web (webroot)

Categories