what am i doing wrong in these php scripts? - php

Here is config.php
<?php
define("DB_SERVER","localhost");
define("DB_NAME","photo_galley");
define("DB_USER_NAME","root");
define("DB_PASSWORD","");
echo DB_SERVER."config file<br>";
?>
Here is classes.php
<?php
var_dump(include_once("config.php"));
echo DB_SERVER."class file<br/>";
?>
<?php
class MySQLDatabase{...
and here comes third file temp.php
<?php
include_once("includes/classes.php");
echo DB_SERVER." tem.php <br/>";
?>
both classes.php and config.php are in includes folder, includes folder is in photo_gallery folder , temp.php is in photo_gallery folder
how it looks like when i open classes.php in browser.
it shows thing as i thought. First show DB_SERVER value from config.php file then from classes.php file
But when i include classes.php file in any other file like in temp.php as shown in third piece of code it gives something that i didn't understand
here is it
I didn't understand output.
I think it's output should be same as that of classes.php with one more line showing DB_SERVER value from temp.php.
Would you please tell me why these constants are shown as undefined when i open temp.php in browser?
if include function in line is successful as shown by int(1) then why not it show DB_SERVER value from config.php?

You include config.php from wrong location in temp.php. Remove the path. Also you should use require_once rather than include_once as config.php is crucial, so lack of it should stop the script.

In temp.php your config.php file path is wrong. Thats why you got the undefined constant.
config.php file inside the includes directory. But your temp.php file outside the includes directory.
in classes.php, You can use $_SERVER['DOCUMENT_ROOT'] add full path in such way you can access the same in all files.

Your scripts are fine. You just have a problem with your server configuration.
They're working correctly in mine:

When you include classes.php relative path to config.php is includes/config.php.
<?php
include_once("includes/config.php");
class MySQLDatabase{...

so we can say that when we use require include function it just copies code from file mentioned to file from which this function called. so when i call config from classes file it works fine. But when i call classes from temp it just copies code from there but for temp file config file location is different so it doesn't work. Am i correct Mr. Developers...

Related

PHP file path error for autoloader

Here is my file structure on my cpanel webserver
root
> vendor
> autoload.php
> public_html
> folder
> file2.php
> script_folder
> include_file.php
> file1.php
Inside include_file.php I have
require_once('../vendor/autoload.php');
file1.php and file2.php both contain the same call to include_file.php
require_once($_SERVER["DOCUMENT_ROOT"]."/script_folder/include_file.php");
This works fine when I run file1.php but when I run file2.php i receive the following error message.
No such file or directory Fatal error: require_once(): Failed opening required '../vendor/autoload.php' (include_path='.:/opt/cpanel/ea-php71/root/usr/share/pear') in /lvl1/lvl2/public_html/script_folder/include_file.php
However, if I change require_once('../vendor/autoload.php'); to require_once('../../vendor/autoload.php'); in include_file.php then file2.php works and file1.php does not work. It showes a similar error.
I understand this is a file path issue but, what I don't understand is why. Shouldn't the path in include_file.php always be the same no matter what file is calling it; i.e. file1.php or file2.php?
The way I see it is the the actual require_once statement is being called from include_file.php but, the behavior I'm seeing makes me think the require_once statement is being ran from file1.php or file2.php resulting in the filepath error.
Can someone please clarify?
UPDATE: inside include_file.php I have tried using:
require_once($_SERVER["DOCUMENT_ROOT"] . '/vendor/autoload.php');
and
require_once(dirname(__FILE__).'/vendor/autoload.php');
neither of these work since both return public_html as the main working directory. My vendor folder is outside the main working directory.
what is returned is this:
/script_folder/vendor/autoload.php
I understand I can simply include the correct file path at the beginning of file1.php and file2.php but, I was trying to figure out a way to reduce the number of requires I need in each file to only one by pointing them to include_file.php, then letting include_file.php do the rest of the work.
My include_file.php file contains several other require statements to other scripts on my server. Kind of like a mini autoloader. All of the other require statements work fine except the autoload.php one I'm having trouble with here.
The only difference I can see is my other scripts are within my public_html folder and my autoload.php file is located outside of public_html.
Assuming you are using Composer, you only need to require "autoload.php" once in file1.php and the file should contain a namespace like namespace App; at the top of the file below the opening PHP tag. Subsequently, file2.php should contain namespace App\folder; and included_file.php should contain namespace App\script_folder;.
The namespace should also be defined in composer.json like so:
"autoload": {
"psr-4": {
"App\\": "public_html/"
}
}
This seems to work perfectly. Guess instead of a relative path I need to use the absolute path.
include_file.php:
$autoload = str_replace('public_html','vendor/autoload.php',$_SERVER["DOCUMENT_ROOT"]);
include($autoload);
it allows me to call:
require_once($_SERVER["DOCUMENT_ROOT"]."/script_folder/include_file.php");
from any files that are in my public_html folder.
in include_file.php $_SERVER["DOCUMENT_ROOT"] returns /lvl1/lvl2/public_html (the full server path to the publicly visible folder)
then I replace public_html with the the path to my autoloader vendor/autoload.php

Php Error: include_once(): Failed opening 'XXXXXXX' for inclusion (include_path='XXXXX') in XXXXX on line 7

I am here because my php code keeps giving me this annoying error whenever i include something from a directory , or just including something from a file. The error is originating from includes.php. I later found that you have to add the COMPLETE path to the directory. So I did, but it just keeps giving me the same error.
My code:
include_once (__DIR__."/inc/defines.inc.php");
Note that __DIR__ gets replaced by the files own directory path. The issue here is most likely that the path from the script you include does not match the directory you want to include a file from.
A common practice is to have a file at the root of your project that defines the root path. If you include that config file (or whatever you call it), it is easier for you to include other files.
For example. imagine a project like this:
config.php
foo/bar.php
foo/baz/bat.php
Example content of config.php:
<?php
define('ROOT_PATH', __DIR__);
Now, the content of foo/baz/bat.php could be:
<?php
// Get the config file
include '../../config.php';
// Include the content of foo/bar.php
include ROOT_PATH . '/foo/bar.php';

php included files and directories

Lets say I have two files in directory new_folder: a.php and b.php
In the file of a.php there is a statement:
include_once 'b.php'
In addition, I have a file demo.php in a parent folder, in the demo file there is a statement:
include_once 'new_folder/a.php'
I.E. the folder structure is:
demo.php
new_folder
- a.php
- b.php
Why if I write in the a.php file:
include_once 'b.php' - correct path
include_once './b.php' - incorrect path
include_once 'new_folder/b.php' - incorrect path
Try This
require_once('../b.php');
my answer is based on php.net:
Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing.
that means that if I write in a.php: include_once 'b.php' ; and then include a.php in demo.php then first it looks for the file in the include_path, then it will look for the file in the directory of the calling script (a.php), and only then it will look for the file in the working directory (that is the directory of the file that we run and starts all the includes- demo.php)
edit: I tried running it and found out that first if there is b.php in the working directory then it will include it and not the b.php from the calling scrip's own directory.
the last check for the file is in the new_folder (the calling script's own directory)

require_once won't read variables from third script

I'm working on some web application, learning PHP OOP. I have 3 scripts: index.php, database.php and config.php.
In config.php are the constants for accessing the DB.
In the database.php is a class for opening and closing the connection to the DB and so on. This file includes the config.php file.
Now on the index file I include the database.php file and I'm testing if $database exists. The files database.php and config.php are in different folder than the index.php.
When I run the index.php file it says: Use of undefined constant DB_SERVER - assumed 'DB_SERVER'
Everything works fine if they are in the same folder. It also works if I include the config.php file separately in the index file, but it won't work if only the database file is included
config.php
<?php constants ?>
database.php
<?php
require_once("config.php");
...
?>
index.php
<?php
require_once("../includes/database.php");
...
?>
Why won't the index.php file recognize the variables from config.php?
Its probably some problem with the include path. Even though database.php and config.php are in the same folder, its index.php that is making the calls and therefore the include path is the index.php folder. When database.php attempts to include config.php it will be looking for it there.
There are a couple ways you can solve this:
Make index.php call all the includes, including the database.php dependencies;
Make database.php aware that its path relative to the site root is ../includes and it too will refer to config.php as ../includes/config.php even though its right there in the same folder with it (not recommended specially if you need database.php in subfolders of your site);
Use set_include_path(). Example:
index.php:
set_include_path('../includes');
require_once 'database.php';
I believe the problem lies with the directories. When you include the ../includes/database.php file, all paths defined in that file become relative to IT! This means that instead of looking for the configuration file in the directory that index.php is in (lets say /var/www/html/funfile/index.php), it's now looking for it in /var/www/html/includes/ (so the literal path of the include would be /var/www/html/includes/config.php). It's a pretty easy fix, just make the path to the config file literal.
Try:
require_once("/var/www/html/funfile/config.php");
And replace the path I made up, with the actual path to the config file. That should solve your problem.
You should give DB_SERVER in quotes like this:
define('DB_SERVER',$db_host);
define('DB_USER',$db_user);
define('DB_PASS',$db_pass);
define('DB_NAME',$db_name);

Issues with defining a path for PHP include across multiple folders

cant figure out how to do what im trying.
I want a way to include PHP files so that i can call the same file from within any file in any level folder and it always points to the same files.
So i have a website setup e.g.
root
init.php
databaseconn.php
index.php
/css/
main.css
/settings/
user_profile.php
/template/
sidebar.php
/includes/
get_user.php
For example, on user_profile.php i am including the sidebar.php file.
For now i would use:
../template/sidebar.php
And then ../includes/get_user.php inside that sidebar file.
Which works fine. The problem however is that i also include the same sidebar.php file on my index.php page. This then causes the issues.
To counter this, i have tried this:
<?php
$isDevelopment = true;
$baseInclude = ($isDevelopment ? '//localhost/website/' : '//www.site.com/');
?>
And then when including any files across the site i have used:
include $baseInclude. 'template/sidebar.php';
When doing this however i am getting an error.
The error shows:
Warning: include(//localhost/website/template/sidebar.php): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/website/settings/user_profile.php on line 46
I dont understand why its looking for the file inside the user_profile.php file when it shows and i have defined that its coing from the localhost path...
Any ideas?
in your init.php file add this at the top
define('ROOT', __DIR__);
now in your other files use
include (ROOT . '/full/path/to/file.php');
in your code it would look like this
include (ROOT . '/template/sidebar.php');
One possible solution:
$baseInclude = getcwd();

Categories