Using .load() or PHP include to load a file with dependencies - php

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.

Related

Directory Mismatch while including other PHP files in it

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]

Wordpress plugin not recognizing required file

require_once($_SERVER['DOCUMENT_ROOT']."/Event_Logger.php");
global $Event_Logger;
$Event_Logger = new EventLogger();
Doing this displays a blank page and does not continue loading the page
The file on my server is located at public_html/Event_Logger.php
So I know I am doing this wrong $_SERVER['DOCUMENT_ROOT'] returns something I wasnt expecting so I am asking how do I get the top level of my site basically the folder public_html
You can't (and shouldn't) include a file using URLs. You should use the path to the file locally, on the same server. If the plugin you're building lives on technologyforthefuture.org, then you can require it from the root path. If you're trying to include the file from an external website, that's not possible.

PHP - How to use the same set of files in each subdirectory?

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?

How to secure a PHPMailer file

Just finished doing a simple mail transfer at my site using PhpMailer
I got 3 question about it -
I have read that's needed to store your credentials on a different file, read that there's 2 options - ini/php, which one would be better and how exactly this file should look like.
Regarding the directory of the credentials file, read it should be located outside the web root (just one level above its fine?), in that case how do I call it from inside the web root?
On the same matter, should the Mail.php itself be located on the site directory? or should I take it out as well?
It's generally safest to put values like these in .php files because they will render to nothing, unlike a .ini file which will usually render as plain text.
Yes, one level above is fine - it means that the file does not have a public URL of its own. From a script running inside the web root, you'd just load it with require '../settings.php';
You don't say what Mail.php is, but generally any other PHP scripts can stay put. Things like class definitions are safe because they have no effect when run directly (or at least should have no effect, if you've written them safely!). That said, it's common to put your composer vendor folder outside the web root since you don't necessarily have control over what ends up in there.

cannot directly call the php file inside folders using the directory path

I have one root folder called GASS where I put all my php files and other related folders (templates,images,js,fonts,css)inside. When i try to run my project in localhost, http://localhost/GASS/alarm_A16GSM.php everything went smoothly. I wanted to change the URL to be more specific, http://localhost/GASS/alarmsystem/16zone/A16/overview.php thus i rename the php file and put it inside folders.
GASS
alarm-system
16-zone
A16
overview
However when i try to run the new URL,the page shows error.This is the error message:
Warning: include(templates/header.php): failed to open stream: No such file or directory in .....
Code for the first URL where the page load successfully.
<div class="overview"><a href="alarm_A16GSM.php" id="overview-selected"><span>
Code for the new URL where the page shows error.
<a href="alarm-system/16-zone/A16/overview.php" id="overview-selected">
It seems like i need to configure something which i do not know what it is.
How am i going to load the page successfully using the new URL? How am i going to traverse four levels up to the root directory so that the page load successfully? Why i cannot directly call the php file using the(alarm-system/16-zone/A16/overview.php) path?
p/s: sorry for my bad English.
It looks like there is a line in your Php file, probably like
include 'templates/header.php';
Include can't find the file using that relative path, because you moved the calling file.
Probably you could change that to
include '../../../../templates/header.php';
To get back down to the GASS folder that apparently has a folder called 'templates' with a file 'header.php' that is required.
An absolute path would be good, instead but it refers to the filesystem path, not webserver path - so you'd need to know your web root folder name on the server.
Copying all the folders (templates,images,js,fonts,css) to the folder overview will solve the issue. Now there is no template file on the folder 'overview' so header.php is failed to load. Another option is create a file save all the included file path and call this file.

Categories