how to set a global link in php - php

I have a question, I have a file in php where I need to include another one
<?php include('includefile.php'); ?>
but if I am in a subfolder I have to call this file like this
<?php include('../includefile.php'); ?>
however, some lines in this files will change, example
<a href="css/style.css">
now is
<a href="../css/style.css">
How can I do to set no matter where in the project
thanks

Generally, how this is achieved is by using a define which is named something like ROOT which stores the absolute path to the root of your project:
config.php
define('ROOT', __DIR__ . DIRECTORY_SEPARATOR);
Then in your other files, you include this one file (config.php) and make use of the ROOT define in your code. This saves you the hassle of having to worry about the ../ and how many directories up you need to go etc.
Reading Material
Predefined Constants

Related

PHP - structuring folders, paths, templates

I have a question about templating my php project.
Currently my folder structure looks like this:
/Root
|index.php
|_footer.php
|_header.php
|_nav.php
|
|site_1.php
|site_2.php
|
|css/
|js/
|images/
|subfolder1
|site_3.php
|site_4.php
|subfolder2
|site_5.php
I usually include "_header" and "_footer.php" in my index.php file, but I have problems with subfolder pages. For example, my _header.php has an include "_nav.php". But when I include the _header.php in the site_4.php I get the problems with the assets and navigation.
I read to make a config file to define site url, asset paths etc, but It's not so clear to me. Ideally I would like to have an "assets" folder with subfolders for css, js etc.
Also I would like to know who can I with include "_nav.php" with site_url(). I tried that out, but I always get errors.
The main question is, how to make the site_url, base url (at this point I'm still confused about those terms and how to use them)
Thank you.
Because you're using pure PHP (no framework being used) this is a bit tricky. There are several ways to solve this, I'd suggest the following:
In your Root folder, create new file, let's name it "base.php", which has the content of:
<?php
// This is base.php
define('ROOT_PATH', realpath(dirname(__FILE__)));
Now, in each of your site pages like (site_1.php, subfolder1/site_3.php, etc.) include the base.php file:
<?php
// This is site_1.php
require_once 'base.php';
// ...
In subfolder site pages, include it like this:
<?php
// This is subfolder1/site_3.php
require_once '../base.php';
// ...
If deeper site page, include it like this:
<?php
// This is subfolder1/subfolder2/site_5.php
require_once '../../base.php';
// ...
Now, in any php file, if you want to include another php file (like _nav.php), you can include it like this:
<?php
// This is _header.php
require_once ROOT_PATH . '/_nav.php';
// ...
?>
<!-- to include any css/js/images files, it would be like this: -->
<link rel="stylesheet" type="text/css" href="/css/my_css.css">
<script src="/js/my_js.js"></script>
<img src="/images/my_image.jpg">
I didn't test the code, but it should work.
What you're looking for is the HTML base tag. This will make every relative link use the specified base.
ie. having this in your head:
<head>
<base href="http://www.yoursite.com/assets/" />
</head>
...will make the following code load the image from the assets folder:
<img src="image.jpg" />
See: https://www.w3schools.com/tags/tag_base.asp

Relative URL issue

I will have multiple folders/modules to access common files. But accessing them seems to be big deal for me!
I did gone through this link to understand the relative positioning and managed to solve some . But not all. Reference: Relative URL's/paths in php
My folder structure is as below:
Website runs on root folder:
/(index|ajax).php
and then the subfolders:
/css/style.css
/img/*.(jpg|png|gif)
/inc/(header|footer).php
/js/*.js
/registration/(ajax|getsubjects|response|success).php
Now, this is how I included files in the index.php page(this displays correctly, meaning, style,css,js,config all accessible)
<?php
include('inc/header.php');
?>
content here
<?php
include('inc/footer.php');
?>
This index page will have to fetch getsubjects.php, response.php and then finally land in success.php.
The success.php need some styling whereas the previous two were only for processing.
So now in the success.php I access header and footer as below:
include('../inc/header.php');
include('../inc/footer.php');
But this doesn't apply any styling!
inside header.php and footer I include files like this:
<link rel="stylesheet" href="./css/style.css">
<script src="./js/script.js"></script>
How should I include the files here please?
./css/style.css means from current directory and would achieve the same result as css/style.css. The easiest answer is to determine what the base path of your application is and use that. For instance, if your application is running as http://myapp.com, then you could set all your front-end paths to /css/style.css. If your app runs in a subdirectory, such as http://example.com/myapp, then your paths would be /myapp/css/style.css.
This does not apply the same on the PHP side. For them, you should really use document-relative paths. Having a PHP file that you include in multiple places in your app, the contents of which having something like include('../myDoc.php');, can lead to complications as the path isn't based on the included document's path, but rather the including. So using document-relative paths, you get around this include(__DIR__ . '/../myDoc.php');. Just something to consider if your app grows.
Your PHP-includes seem to be correct. But in your HTML you need to change the linking to the CSS and JS Files (maybe even to your images).
You could use absolute paths:
<link rel="stylesheet" href="/css/style.css">
<script src="/js/script.js"></script>
the leading dot makes your paths relative to the HTML-Document, so if they are linked from a document in a subfolder, they point to a wrong location.
Including files with
<?php
include("page1.php")
?>
put the code (or content) from page1 into the caller page.
So you may have to detect from where your pages are called, or try absolute links (beginning by /)
I hope I answer you question correctly.

include files from subdirectory which calls from another directory

I have a file here: public_html/wiki/index.php
Im calling these files:
<?php
include "../auth.php";
include "../header.php";
?>
However, my page looks like this:
If I paste the same file in public_html/index.php then it will look like this:
I think I've narrowed it down to my header.php file which is being called from /wiki/ but header calls files from ../css.. and ../js and so on.
How can this be adressed? I've looked at many posts already which tells me to define a global variable but it doesn't help me. Like so:
<?php //config file in root folder
define("ROOT", __DIR__ ."/");
?>
and then calling them with this:
<?php
include_once("../config.php");
include (ROOT ."auth.php");
include (ROOT ."header.php");
?>
How should I do this so I can get all my javascripts and the actual site with me in another folder?
NOTE: I WANTED THIS AS A COMMENT BUT DO NOT HAVE THE REP
We have a different folder structure, but it is good practice to put your includes in one folder and from there link all the JS files (best practice is, place them in a pre-footer for speed) and then do
<?php include $_SERVER['DOCUMENT_ROOT'].'/includes/page-footer.php'; ?>
Just change the file name to suit you :) hope this helps
the problem was my include file didnt specify links as /js/file.js
i had them as js/file.js.
thanks #cd001

PHP include paths within directories

I have looked around at a few different questions about the same sort of problem I'm having. I have took a solution and adapted it to my own project.
Here is my directory structure.
/css
-style.css
/includes
-shop.css
-header.php
-footer.php
/php
/js
/shop
-index.php
-index.php <-- homepage
-config.php
Inside my config.php I have
define('ROOT_PATH',$_SERVER['DOCUMENT_ROOT']);
My header.php
<?php include './config.php';?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<?php echo '<link href="'.ROOT_PATH.'/css/style.css" rel="stylesheet">';?>
<?php if ($_SERVER['REQUEST_URI'] == '/shop/'){echo '<link href="'.ROOT_PATH.'/includes/shop.css" rel="stylesheet">';} ?>
</head>
The only problem is, for any other page other than the root index.php file, the path for the config.php file becomes incorrect. Thus the CSS paths then become incorrect as ROOT_PATH isn't defined anywhere.
What would be the best way to handle paths when using includes?
Use $_SERVER['HTTP_HOST'].$_SERVER['DOCUMENT_ROOT'] gets the document root for eg var/com/images. $_SERVER['HTTP_HOST'] will get current url like http://example.com/images.TYour code should look like this
define('ROOT_PATH',$_SERVER['HTTP_HOST']);
And include this way
<?php include'../config.php';
Hope this helps you
Maybe using relative paths for the include in the other index.php file.
<?php include '../config.php'; ?>
You are using the server's actual filesystem path to refer to your stylesheets. That's like trying to do something like:
<link href="C:\your_website_path/includes/shop.css"...
and wont work.
I would recommend to change that to something like:
define('ROOT_PATH', 'http://www.your-website-url.com/');
Regards,
what you need to do is make the include for your config absolute, not relative. you begin the path with a dot ./config which means its relative. instead set up your header to include the config file with an absolute path like this
<?php include '/home/user/config.php';?>
This way, any page can find the file no matter its location in the directory structure.

Basic PHP: Passing An Array From One PHP file to Another

I am making a website in which I want a column on the left hand side to be a list of links. This column populated with links will be on every single web page of the entire site, and it will be updated with more links quite frequently. So the problem I need to solve is how to make it so that whenever I want to add a new link to my website's "link list", I don't have to manually add it in the HTML code in every single web page.
I know some Java but I'm completely unfamiliar with PHP. I did a bit of research and found the "include" declaration, and kind of slapped together something that does work, but I have no clue whether this is a good idea or not - as in if its bad code/bad style, so could I get some opinions on this please? Is it good code? Or am I going about it the wrong way?
The entire php file that contains the array, title "videos_array.php":
<?php
$videoArr = array(
"<li>Video 1</li>",
"<li>Video 2</li>",
"<li>Video 3</li>");
?>
And the web pages where I want to insert these values into look like this:
<html>
<head>
<!--All the necessary tags-->
</head>
<body>
<!--More Tags-->
<div id="link_column">
<?php include 'videos_array.php';
foreach($videoArr as $val){
echo $val;
}
?>
</div>
</body>
</html>
When referencing a php file with the include declaration, is it a relative path that I should be using, or are absolute and virtual references ok as well?
Thanks in advance for your input
Your $videoArr is used only once, so there is no need to use it in another php file you can just echo after declaring it like,
<?php
$videoArr = array(
"<li>Video 1</li>",
"<li>Video 2</li>",
"<li>Video 3</li>"
);
foreach($videoArr as $val){
echo $val;
}
?>
Even no need to create an array if it is not used just echo it as a string like,
<?php
echo "<li>Video 1</li>",
"<li>Video 2</li>",
"<li>Video 3</li>";
?>
And in your index page use it like,
<div id="link_column">
<?php include 'videos_array.php';?>
</div>
When referencing a php file with the include declaration, is it a relative path that I should be using, or are absolute and virtual references ok as well?
What you're doing is fine however there's some points to be wary of around PHP's include path.
The include path is a FIFO stack of server file-system paths that PHP will use as base directories when resolving paths provided in include and require statements (and probably some other things). The basic include path usually includes . (or the current working directory). This is resolved as the directory containing the first (parent) PHP script executed. This can catch people out once you go a few includes deep.
What I find generally works best is to prefix include paths with the __DIR__ constant. This resolves to the parent directory of the current script.
For example, say you have the following directory structure
foo.php
bar.php
dir/baz.php
To include bar.php from foo.php, you can use
include __DIR__ . '/bar.php';
To include bar.php from dir/baz.php
include __DIR__ . '/../bar.php';
You can also manipulate the include_path configuration. For example (in foo.php)
// add "dir" to the top of the include path stack
set_include_path(implode(PATH_SEPARATOR, [
realpath(__DIR__ . '/dir'),
get_include_path()]));
// now include baz.php
include 'baz.php'; // this works because we added "dir" to the include path

Categories