Path troubles when require or include file in PHP - php

I have directory structure like this:
- index.php
- templates(directory)
- login.php
- header.php
- js(directory)
- jquery.min.js
I include script jquery.min.js in header.php, then I include header.php in login.php, but the path to jquery.min.js brokes. How I try to solve it by replace this:
<script src="js/jquery.min.js">
to this
<script src="<?php echo dirname(__FILE__)?>js/jquery.min.js">
but then I get this path to resourse:
http://localhost/var/www/auth/js/jquery.min.js
And again 404 error. Can somebody explain to me how I can solve this issue.

js's script files work in context of web-root. as long as your web-root is where your index.php is then <script src="/js/jquery.min.js"> should work
You use <script src="js/jquery.min.js"> which is a relative path. so, if you call login.php browser requests js relatively to it: templates/js/jquery.min.js which doesn't exist, obviously

What you're trying to do is find the path of the javascript file relative to the root of your application, rather than any specific php file. So, if you change the script tag to begin with a /, it'll serve from the root of your application, regardless of where the php files are.
Changing it to <script src="/js/jquery.min.js"> will serve it from http://localhost/js/jquery.min.js regardless of where in your structure your php files are.

Related

Root directory path problems

I am working on a website for a client and I am using xampp to serve and work on locally.
The site is loading fine at localhost/site but the resources are not being found. The previous developer used absolute paths in the index.php to define resources like so:
<script href="/includes/javascript.js">
The site fails to load javascript.js because it looks at:
localhost/includes/javascript.js
When it NEEDS to be:
localhost/site/includes/javascript.js
index.php is located here:
localhost/site/index.php
It works fine on the client server. What am I missing here?
A URL starting with / is always absolute, you need a relative path here. So use
<script href="includes/javascript.js">
in your index.php or all files located in localhost/site or use
<script href="/site/includes/javascript.js">
for use in all files.
You have a two mistake use src not href and remove / from start. try like that
<script type="text/javascript" src="includes/javascript.js"></script>

Relative paths and includes

So I have some php files that I'm trying to include with no luck.
I have the root directory, and off that I have a directory called aboutus. In the aboutus directory, I'm trying to include a css file from the root. So I use this code:
include $_SERVER['DOCUMENT_ROOT'] . "/meta-head.php";
The include works fine, but in the meta-head.php there are some calls to files, one of which is:
<script src="_assets/js/navbar.js"></script>
The problem is that it's trying to access this from root/aboutus/_assets/js/navbar.js (which doesn't exist), when I want it to acccess it at root/_assets/js/navbar.js.
What am I doing wrong so that it won't access the file relative to the root?
Thank you!
use absolute path or use this:
<script src="/_assets/js/navbar.js"></script>

JS Include inside included .php file

I have included a PHP file on my mainpage, inside the php file there's another include of a .js file, the included php file is in a different directory than the mainpage, so the included .js file can't be found.
To visualize:
Mainpage - Root
Included PHP file -File A located in Folder A
Included JS file in File A is located in Folder A
But it's looking for the .js file in Root instead of Folder A
How do I get the path right ?
Also it should be dynamic, I might not know the actual name of Folder A
Use the .. notation to traverse folders
the path would be '../script.js'
Site:
www.yoursite.com
Folders:
/index.php
/lib/header.php
/contacts/index.php
/js/main.js
Let's say you want to inclue your /js/main.js file inside /lib/header.php in order to work wherever you include /lib/header.php, for example in /index.php and /contacts/index.php.
You would have the following inside /lib/header.php :
<script src="http://www.yoursite.com/js/main.js" type="text/javascript"></script>
if include.php and xyz.js are in the same directory. than
include .js file in include.php like that--
<script type="text/javascript" language="javascript" src="xyz.js"></script>
if it is not working than u should use Relative Url/SITE URL like that--
<script type="text/javascript" language="javascript" src="http://localhost/folder/xyz.js"></script>

Path problems HTML

Im creating my own website, but I am having some path problems.
My scripts will not load when I refer to the path.
I have simply created a folder called "js" under my repository and are trying the following code:
<script src="js/startup.js"></script>
But the script wont load.
I've also programmed some PHP scripts and used include, and it seems to work fine there.
Any tips?
Regards
When including resources for the browser, the path is relative to the web root but when including php files, it is relative to the current file.
/project
/webRoot
-index.php
/js
-startup.js
/lib
-some.php
given this structure, your script tag would work, even if included in some.php. In index.php, you would need
include(dirname(__FILE__).'/../lib/some.php');

Trouble including .js file into php

I am trying to include a .js file into a php file.
My folder structure looks like this:
root
---js (FOLDER)
------file.js
---blog (FOLDER)
------index.php
------js (FOLDER)
---------blog.js
If I am using this:
<script type="text/javascript" src="../js/blog.js"></script>
it works just fine.
What I can't seem to do is include file.js from under the root's "js" directory.
I've tried everything I can think of but I just can't seem to make it work.
<script type="text/javascript" src="/js/file.js"></script>
would reference the javascript file in the top level js directory
To access the blog, is a user going to www.domain.com or www.domain.com/blog?
If /root/blog is your DOCROOT in your web server, the /root/js directly likely isn't web-accessible. The location you're referencing MUST be accessible to the client's browser.
An easy way to test this is to enter the path to the JS file directly into your browser.
it looks like blog is in a subdirectory of the folder index.php is in. Discard the ../ bit.
<script type="text/javascript" src="js/blog.js"></script>
<script type="text/javascript" src="../js/file.js"></script>

Categories