Require_once across different files, global scope - php

I've been learning PHP for a couple of days now and I came across a strange issue for me. I have two files :
login.php that stores database access data (user, pw, host, db)
security.php that have functions that manipulate strings/queries to prevent sql/xss injections and also stores salt prefix and suffix for password hashing.
Then I have my index.php, register.php and signin.php. My problem is : when I put require_once '../login.php'; in register.php I can normally call a variable or function like $var. On the other hand, exactly the same pattern in signin.php is not working, PHP tells that that variables are not declared. I tried to do : global $var, and it was working but I have feeling that it's completely insecure.
1) What is the proper way to solve this problem?
2) Is it connected with app architecture?
3) Should all functions (and etc.) that use db connection be written in a single file?

Step 1 - Create a php file , write all database connection into it(example- db.php)
Step 2 - Include this php (db.php) wherever you have a database related operation.
require 'db.php'; //add your database php file here..
This will be easy to organize, and minimizes errors when you migrate your app from one server to another
in php there is require and include. require is good since it will stop executing code if file is not available.
Also this is easy to organize and minimizes the errors caused when migrating from one server to another

install composer
Put all your code into functions in classes with your namespace (as static functions if you must).
Save the classes like specified in psr4 in lib dir.
Create composer.json file as specified on composer website and include your lib dir as autoload as it says in composer docs.
require_once projectroot/vendor/autoload.php in your files.
Call your class methods (with namespace) instead of functions. Be lazy. Avoid require/include hell.

Related

Is there no manual include of Laravel?

I was starting some .php files in the /public directory of Laravel, which works, naturally, but it is separated from the standard Laravel system. In fact, Laravel wants you to you routes I know, and if I want to use some Laravel stuff I would think that calling
require_once 'path/to/vendor/autoload.php';
require_once 'path/to/app/Services/Myfile.php';
would let one use that... but for example "use GuzzleHttp", if used in myfile, gets fatal "Uncaught ReflectionException: Class config does not exist" in Container.php of Laravel.
I know there's SHORTINIT in Wordpress though it is kind of a mess of requiring any file that you need and all the files with functions it uses... Is there something similar for Laravel? Or this is never properly used this way to hold php files within /public?
You don't need to use require as composer takes care of loading the library files.
However when you add any new library or make any changes to routes or config files make sure you run the optimize command.
php artisan optimize

CodeIgniter + Ion Auth: Ion Auth config file is loaded twice

Explanation:
Using CodeIgniter v3.1.2 with Ion Auth 2
I separated the admin area and user area into 2 different applications to improve security.
I wanted to share config files between two apps so for application_admin/config/ion_auth.php I did this:
require_once(__DIR__.'/../../application_users/config/ion_auth.php');
The script stopped working and after some hours I realized the file is being loaded twice because when I use require instead of require_once everything work's fine.
Questions:
Shouldn't CodeIgniter check and prevent loading files that are already loaded twice? if yes then why is this happening, and if not is it okay if I just use require instead of require_once?
Is my approach the correct way of sharing config files and helpers between multiple applications in a single CI install?
Edit:
I put the require_once in the admin/config/ion_auth.php to include user/config/ion_auth.php so basically I'm putting the content of that config file in this config file "only when CodeIgniter itself loads this config file".
Also even if I put the normal ion_auth config file without any require/include etc, it is still loaded twice (I noticed by putting a simple echo 'loaded<br>'; in the end of config file.)
Shouldn't CodeIgniter check and prevent loading files that are already
loaded twice? if yes then why is this happening, and if not is it okay
By making direct call to require or require_once you are bypassing Codeigniter (CI) so it has no way of knowing what you have or have not included.
if I just use require instead of require_once? Is my approach the
correct way of sharing config files and helpers between multiple
applications in a single CI install?
Loading a config file in CI should be done using the Config class. If you dig into the source code for Ion Auth and look at __construct in Ion-auth_model.php you will find heavy use of the config class to read the config file and access various config items. It a good example of the "CI way" to handle configuration.

How to include php file in drupal 7

I'm coding a form in Drupal 7, and I want to 'include' a php file that adds my DB connection variables to my code.
I've tested my code in several ways, and I'm sure that the 'include' makes me get a WSOD when I run Cron in my site.
I've Googled about this and I tried:
include("/sites/all/libraries/php/connection.inc");
include("./sites/all/libraries/php/connection.inc");
include"./sites/all/libraries/php/connection.inc";
I also tried the above with '.php' extension.
And my last try:
define('__ROOT__', dirname(dirname(__FILE__)));
include(__ROOT__.'/sites/all/libraries/php/connection.inc');
Sometimes I get a WSOD when trying to run Cron, and sometimes my page styles get broken when I submit a form.
What is the correct way to include a PHP file manually in Drupal? Note that I don't want to use the 'Drupal way' to do this or to use the webform module. I want to code it manually with PHP.
The libraries directory should only be used to store files shipped as a library through the libraries module (see here https://drupal.org/project/libraries).
For both examples lets assume library.inc is our file and relative_path_to is the relative path based on the module directory to our library.inc file.
To just include a file you can use:
require(drupal_get_path('module', 'module_name') . '/relative_path_to/library.inc');
And to do it the Drupal way (https://api.drupal.org/api/drupal/includes!module.inc/function/module_load_include/7):
module_load_include('inc', 'module_name', '/relative_path_to/library');
Cheers,
j
Try this ways
1) require 'includes/iso.inc';
2) include_once 'includes/iso.inc';
3) include ('includes/iso.inc');
OR Drupal Ways
include_once DRUPAL_ROOT . '/includes/iso.inc';
In my opinion these are correct Drupal (7) ways to include code:
module_load_include(); function - Drupal API documentation - to include any PHP files from within your or other modules where needed,
files[] = ... in your_module.info file to autoload include classes and interfaces within your own module - Writing .info file documentation.
Libraries module provides it's own way to include external PHP files from the libraries.
The second answer is the correct "Drupal way". The one that was accepted as the answer has many potential pitfalls if locations of things change. Using drupal_get_path is the best way. This is also especially true if you are trying to include a file that may not be fully bootstrapped during a module update hook.

How to include external PHP classes in Joomla

I have few PHP classes and files that I want to include in Joomla so that I can call those classes. I tried doing require_once config.php to include a PHP file which also includes all the classes that I would want to use in Joomla.
But each time I execute the page I receive the error below:
Fatal error: Class 'SomeClassName' not found
is there any other way to include external PHP classes or files in Joomla?
Thanks in advance!
Please use Joomla autoloader. Is better.
<?php
// Register an adhoc class.
JLoader::register('AdhocClass', '/the/path/adhoc.php');
// Register a custom class to override as core class.
// This must be done before the core class is loaded.
JLoader::register('JDatabase', '/custom/path/database_driver.php', true);
Edit:
Load classes with autoload instead of require/include have a better performance, because PHP will only read (require access to the disk) and compile (require memory and CPU usage) if you really use your class.
To do the same with require/include you have to be sure to only use if will really use the class.
Source:
http://developer.joomla.org/manual/ch01s04.html
require_once should work just fine within Joomla. Make sure the class you want to use is really loaded within your file, and the file is properly referenced in the require_once. Something is going wrong there and it has nothing to do with Joomla itself :-)

PHP extending requires on files

I have a problem where I would like assistence with.
Currently on working on a new project for myself and I am trying to start with a good foundation.
I have build the following file structure (relavant part):
Class/
Class/class.filename.php
Class/class.etc.php
Func/
Func/func.filename.php
Func/func.etc.php
Config/config.php
index.php
In my config file all classes and functions are included with the require_once function (I loop alle files and directories inside func and class). In the folder class the file for firePHP is located which I include and then setup in the config.php.
In my config.php and index.php I can call this log function perfectly, but when I use it in one of the func.filename.php or class.filename.php it errors. The child (func/class) is not seeing the other included functions within config.php.
Hope somebody can help me out with this.
You func.filename.php file needs to "require_once" the file that contains the log function.
Another solution that may work for you (since you have this structure) is to have all your required files in config.php (included with require_once(<file>)). This seems to work. Then, when other files required any other function for other files, just require_once('config.php').
It is best to only include what you need, when you need it. Why spend the time loading 50 different classes/functions/snippets when you might only need 2 or 3 for the script at hand? Paring out unnecessary include statements can increase the performance of your scripts considerably.
Make use of the __autoload() function for classes. It is just super.
Chain include_once() or require_once() through your various files so that grabbing one file that you need for a given job automatically gets its own dependencies.

Categories