I am working on a project (the first one) and got this error :
[I am NOOB and new]
First of all my directory is something like this: (Adding on main files)
[Yellow lines point to files and the purple one leads to a folder.]
I am working on the User Section right now.
I made a header folder and added a header.php file to it. which contains a header(): void. this header function basically has my navbar (few things are retrieved with DB). Also this same file is linked with .css and .js from AdminSection.
Now while making this function I was doing try and error on my user dashboard so it is now working 100% correctly as I wanted.
But when I included this function into another PHP file for let's say UserProfile it doesn't work anymore as all the included files in Header.php were working for UserDashboard.php (since it is in another directory which is working because of my try and error.)
I searched for a bit about this and found no solution but to make a header file for every directory which seems to be stupid.
Any solutions for it?
A Snippet from my header file if required
[The paths are working for Dashboard.php but not for files in another directory. I want a solution where I have only one header.php and it works for every file no matter its directory]
Related
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
You see how facebook works, like if my profile is
www.facebook.com/myusername
then there is a specific index.php file and other lots of files that open when I open facebook.com/myusername.
Now I believe it is highly unlikely that Facebook copied the same files into each and every user's username directory.
How else would it work?
I'll be having many different users using the same application, i.e. the same set of files with minor changes in one or two files. Do I necessarily have to copy all the files into all the user directories each time?
I tried putting an index.php file in the subdirectory that contains this code:
<?php
require '../index.php';
?>
Now even though that runs the ../index.php file inside the subdirectory, but when the ../index.php file redirects to say another file named 'otherfile.php', then it gives a 404 not found error, because 'otherfile.php' is not present in the subdirectory, it is present in the parent directory.
How do I solve this problem?
I'm trying to include file from another php framework but doing so it's giving me an error failing open the stream for the files which are included inside the file I'm trying to include.
Any idea on how to include it properly so that the files included inside my included file are able to be processed?
What Framework are you using?
I have little experience with templates but I don't think you should be adding PHP code to a template file.
Click Here for another question on how to add PHP code to a template file.
However as far as your path goes, try using:
include("../../other/index.php");
Your include only looks only one directory up.
include ("../../other/index.php");
would be two directories up so into the "admin" directory then the "other" directory
This has been asked before here
Let's say you downloaded a third party app such as a database browser. You upload it into its own folder in your public_html folder. Instead of sending a user to its internal link, such as www.website.com/App/Login.php, you want to have Login.php loaded on one of your own pages higher up: website.com/Login.php.
The file would contain either:
$('#div').load('App/Login.php');
or
<?php include '/App/Login.php';
Unfortunately the PHP method is not working for me at all, and the jQuery method works, however none of the file's dependencies are in-tact. Eg. if App/Login.php was calling for App/js/File.js, it would look in the wrong directory.
I would like to include cars.php file to index.php theme; the cars.php file is script, which loads data from the server and works as booking system, when I am trying to connect as usual way include('/.cars.php');, and check the source code in browser, its there, but no forms appear.
Could be the problem with wordpress engine itself?
I think you have a typo include('/.cars.php'); is likely include('./cars.php');
What you have now would look in the root directory for a file called .cars.php