Not allowed to load local resource PHP Xampp - php

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.

Related

Linking CSS file with PHP throgh all folders

INTRO
I am new to php. I love that it allows me to change one header.php file and it updates all over the site.
IF all - index.php, header.php, style.css, article.php, homework.php files are in the ROOT folder, everything works like magic, I like it. I use:
<?php include_once "header.php"; ?>
at the top of index.php, article.php and homework.php and the header appears.
to load css a regular =
<link href="style.css" rel="stylesheet" type="text/css">
is enough to have in my header.php file, because all the files are in the same directory.
MY PROBLEM
When the amount of articles becomes too large and I decide I want to put those articles in different folders, that is when stuff gets confusing and I would like to know a proper way to solve it.
New website folder structure
C:\xampp\htdocs\articles\homework.php
C:\xampp\htdocs\views\header.php and style.css
C:\xampp\htdocs\index.php
C:\xampp\htdocs\articles.php
Now please help me how to make homework.php file to load the css from the header.php? I manage to load the header itself with
<?php include_once "..\views\header.php";?>
BUT the css file doesn't load for some reason.
I read something about "basenames", "site roots", but don't know how to properly set them up.
The perfect scenario
The perfect scenario would be if I could have a basename variable that I can change, so when I make my server live I can just change the basename to the appropriate new server directory and because all the header.php and other blog files were linked to that basename, everything would change automatically. I have done too many manual directory rewriting to do it once again, please tell me a way to automate it :)
Thank you a lot!
p.s!!!!!! Before I even post this question I realized that the header.php is trying to load views/style.css, which doesn't make sense, because the style.css file is in the same folder as header.php now.. Somehow basenames, site roots are a must here I believe...
You can specify relative paths such as ../css - means up one folder then look in css folder or ../../ - means up 2 levels then look in css folder.
../../main/css/style.css - would mean up 2 levels then look in main/css for the file style.css.
The problem with using / or ../ etc is that if you decide to change the location of the resource you still have to change all your paths. Using a var you can simply change it to reflect the new location.
with PHP you can also getcwd() and dirname() and realpath() to get a string representing a location, you can then set a base variable for your files and 'path' down from it.
This way you can use the same variable to locate a file rather than different relative paths depending on the level of the file calling it.
DIRECTORY_SEPARATOR is also useful for avoiding errors between / and \ with linux and windows OS. However I believe Windows and Linux will both resolve /
Personally I like to set path locations to commonly used files such as /includes in config.php then I can use that setting from anywhere
In summary you are just either discovering a path using PHP or setting a path as a variable
$path = 'c:/htdocs/mysite/includes/';
then using the variable as part of the path name when you access the file
$_SERVER['DOCUMENT_ROOT']
can also be useful to identify your site home folder

Moving website to localhost: Document Root changed

I have download a website that was written from scratch using PHP, MySql, html, css, jQuery.
After fixing some configuration with MySql and htaccess, I get to a point where all my CSS and JS files are not loaded because of a relative path error.
This is how it looks:
The error in the console GET http://localhost/fonts/fonts.css sends me to index.php which loads all those files with a relative path.
Now, if I remove the / at the beginning of each line, the files will be loaded, however, I get more errors on different pages with bad URLs and such, that means that I need to fix this / problem instead of just renaming the hrefs.
So my question is, how to fix it?
I have tried to set a base href tag:
<base href="http://localhost/israelrescue/">
or
<base href="http://localhost/israelrescue">
or in PHP:
$_SERVER['DOCUMENT_ROOT'] = 'http://localhost/israelrescue';
or
$_SERVER['DOCUMENT_ROOT'] = $_SERVER['DOCUMENT_ROOT'] . '/israelrescue/';
P.S:
israelrescue is the name of the folder I am using in my localhost installation for this website.
Nothing seems to work, any ideas?
Thanks!
Answers:
It's because you've got the site in a subfolder in localhost. Is it possible for you to move the site into the root? I think #MukeshPanchal is telling you to load the assets from live, which is missing the point somewhat. – thebluefox
Looks like this site was not written to be run from within a
sub-folder. Easiest way to work around that would be to set up a
VirtualHost for this project in your local server, so that you can
load it directly via a local domain name, without any additional path.
(And btw., setting DOCUMENT_ROOT to an HTTP URL is complete nonsense.)
– CBroe
You can leave the site in its subfolder as long as you drop the "/" in the beggining of every file path :
<link rel="stylesheet" href="/css/main.css">
<script type="text/javascript" src="/js/common.js"></script>
becomes
<link rel="stylesheet" href="css/main.css">
<script type="text/javascript" src="js/common.js"></script>
Should work like this

HTML project path confusion

I am writing an HTML/CSS/JS project on my localhost.
The project root is found at http://localhost/projects/project1/.
I want to know if it is possible to make the HTML files treat my project
root as its' base URL so that referencing the same javascript/css files
doesn't depend on the path of the HTML file(unless I reference relative to
current directory).
Here is my project structure:
projects
project1
index.html
pages (directory)
page1.html
page2.html
apps(directory)
app1(directory)
index.html
style.css
app.js
deps(directory)
jquery.js
As you can see, to refer to jquery.js inside my /projects/project1/index.html,
I have to write:
<script src="deps/jquery.js"></script>
To refer to jquery.js inside /projects/project1/pages/index1.html,
I have to write
<script src="../deps/jquery.js"></script>
To refer to jquery.js inside http://localhost/projects/project1/apps/app1/index.html,
I have to write
<script src="../../jquery.js"></script>
At which point I am not sure I wrote the correct number of ../s and can easily
cause errors.
Worse, If i reorder my directories around or rename them, it means I have to deal with
rewriting the paths again, which can be very daunting.
I have thought of various ways to address them none of which are appealing(although option 3 comes close).
Thoughts/Attempts:
I tried playing with .htaccess RewriteEngine/RewriteBase but nothing worked.
I could just create a new server for each project as NodeJS makes it really easy to do so.
This way, each server has the directory as its' static path and is '/'.
The problem with this approach is that this managing
many servers up at once on different ports which can be confusing.
I could set an absolute path variable in .htaccess via
SetEnv PROJECT_PATH /projects/project1/
However doing this means I have to open .htaccess each time I move the folder
or rename it and change the PROJECT_PATH. This may seem simple but if you
give the project to somebody else and they don't know how .htaccess works,
it is very painful to explain how to find this path, and how to change it,
and even more problematic if the .htaccess is longer than one line.
Ideally, I want this SetEnv to figure out the folder it is inside by itself,
but I don't know how to accomplish this. This also has a problem that the pathing becomes dependent on the PHP preprocessing which makes it painful to move to another server that does not have .htaccess or PHP such as NodeJS(which can still invoke php but even then it needs to deal with .htaccess).
I want to know if it is possible to either
Make .htaccess file create an environment variable containing deduced path
of this particular .htaccess file so that php files inside this directory can
do pathing relative to $_ENV{PROJECT_ROOT}. This way if the project is moved or shared, it will still work without needing to modify .htaccess.
Make .htaccess to force HTML files contained in the same or lower directories that all "/" references treat this directory as the root.
Or if there are other things to address this issue that I have not thought of.
Thanks ahead of time.
Dmitry, you can try this:
1) In your file httpd.conf, add the following line at the end:
Redirect permanent /deps http://localhost/projects/project1/deps/
2) Restart apache service
3) Refresh your index page(/projects/project1/pages/index1.html)
I have used something like this:
<html>
<head>
<script src="/deps/jquery.min.js"></script>
</head>
</html>
<?php
echo "welcome to index"
?>
How do I access a directory outside of my document root?
This explain how to give users access to a directory that is outside of your directory root on Windows.
Assuming that your directory is located at C:/www/newfolder/ then you would create the following in your httpd.conf file.
Alias /newfolder/ "C:/www/newfolder/"
<Directory "C:/www/newfolder">
Options Indexes MultiViews
AllowOverride AuthConfig
Order allow,deny
Allow from all
</Directory>
Now, when a user goes to www.domain.com/newfolder/ then they see the folder that is outside your document root and it appears in the browser to be a directory contained below the document root..
One thing to consider is that if you have virtualhosts, then an alias works for every virtualhost, so try to use uncommon names for your alias.
Hope this helps

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.

PHP broken relative links in nested directories

I'm sure I'm missing some simple explanation, but I want to confirm - so assume I know very little.
I have a directory structure like so (for the time being) of:
My main site (localhost/project/ on my testing server, and C:/xampp/htdocs/project on my HDD) with these files and folders:
Root
graphics
variousgraphics.png
support
stylesheet.css
templates
header.php
footer.php
initialize.php
you
default.php
index.php
anotherfile.php
Up until I created the folder 'you' everything was fine, i.e. I included the initialize file for index.php as <?php include(templates/initialize.php) ?>
But when I decide to include initialize.php using the above method for the default.php file (inside 'you'), it errored out with Warning: include(templates/initialize.php) [function.include]: failed to open stream: No such file or directory in C:\xampp\htdocs\photoquilt\you\default.php
So naturally I appended ../ to create <?php include(../templates/initialize.php) ?> but then of course that didn't work because the files referenced inside initialize.php weren't appended in the same way, and so I get to here.
It's worth noting for me, an echo of $_SERVER['document_root'] leads to C:/xampp/htdocs
So in summary:
Is there any way to make sure all the link/paths work correctly irrespective of where the originating path was from?
In default.php you can define a constant like
define('ROOT_PATH', dirname(__DIR__));
or for php versions prior to 5.3.0
define('ROOT_PATH', dirname(dirname(__FILE__)));
and then use ROOT_PATH in all scripts to build the the file paths.
see
- http://docs.php.net/language.constants.predefined
- http://docs.php.net/dirname
There are a couple problems here as far as I can tell: the server-sided and the client-sided.
As for the PHP goes, you are doing it fine. Referencing the file by its relative path (../templates/initialize.php) is the way to go. There's another way of achieving the same, though I wouldn't recommend it: editing the include_path to add the root directory of your project. You can do it in an .htaccess located in the root directory, ie:
php_value include_path ".:/path/to/your/project:/usr/local/lib/php"
For the HTML part (images not loading, stylesheets not found), you can set a base href:
<base href="http://path.to.your/in-server/" />
The base href should point the root of your directory. All the images, stylesheets, etc in HTML must then be fixed to use relative URIs from the root of the project (graphics/variousgraphics.png).

Categories