Include Relative Path Problem

Problems with the Windows version of XAMPP, questions, comments, and anything related.

Include Relative Path Problem

Postby c.nickerson » 07. June 2013 23:30

I just got a new laptop and installed latest version of XAMPP on it. I moved over a web application I am working on from my desktop to my laptop and it isn't work. It appears the "require" I'm doing isn't working. Was there something changed within recent versions of this? Relative paths don't work.

Code: Select all
index.php
includes/config.php
includes/include.php


Inside 'includes/include.php' I had it require "config.php"; Since it is in the same directory as the include.php it would work. But now I need to do require "includes/config.php" since the include.php is being required by the index.php. All the locations seem to be relative to the index.php as opposed to where the file is located.

How can I go about fixing this?
c.nickerson
 
Posts: 6
Joined: 07. June 2013 00:23
XAMPP Version: 5.6.3
Operating System: Windows 7

Re: Include Relative Path Problem

Postby c.nickerson » 07. June 2013 23:40

Okay apparently the include IS working. But it isn't reading the defined constants I have in config.php in the include.php. I don't think SmartyPHP is working either.

Code: Select all
Notice: Use of undefined constant DB_HOST - assumed 'DB_HOST' in C:\xampp\htdocs\Xion\includes\include.php on line 6

Notice: Use of undefined constant DB_USER - assumed 'DB_USER' in C:\xampp\htdocs\Xion\includes\include.php on line 6

Notice: Use of undefined constant DB_PASS - assumed 'DB_PASS' in C:\xampp\htdocs\Xion\includes\include.php on line 6

Notice: Use of undefined constant DB_NAME - assumed 'DB_NAME' in C:\xampp\htdocs\Xion\includes\include.php on line 6

Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\Xion\includes\include.php on line 6

Warning: mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\Xion\includes\include.php on line 6
Could not connect to MySQL: php_network_getaddresses: getaddrinfo failed: No such host is known.
c.nickerson
 
Posts: 6
Joined: 07. June 2013 00:23
XAMPP Version: 5.6.3
Operating System: Windows 7

Re: Include Relative Path Problem

Postby Altrea » 08. June 2013 13:54

I bet the problem is the require of the config.php.
But we don't know anything about the sourcecode so we can't help you fixing it.
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 6818
Joined: 17. August 2009 13:05
XAMPP Version: 5.5.19
Operating System: W7Ux64

Re: Include Relative Path Problem

Postby c.nickerson » 08. June 2013 16:32

Altrea wrote:I bet the problem is the require of the config.php.
But we don't know anything about the sourcecode so we can't help you fixing it.


Well what is really interesting is I made a test directory and did exactly the same structure and it works in that? It has to be something with the folder permissions or something? It seems to be including the file properly but it isn't reading from it. Bizarre...

index.php
Code: Select all
<?php

require 'includes/include.php';

if (isset($_GET['page'])) {
   $template = $_GET['page'];
} else {
   $template = "index";
}

$smarty->display($template . '.tpl');

?>


includes/include.php
Code: Select all
<?php
   
require 'config.php';

// MySQL Config
   $db_connect = mysqli_connect (DB_HOST, DB_USER, DB_PASS, DB_NAME) OR die ('Could not connect to MySQL: ' . mysqli_connect_error() );

// SmartyPHP Config
   require 'smartyphp/libs/Smarty.class.php');
   $smarty = new Smarty();
   $smarty->caching = 0;
   $smarty->template_dir = 'templates/default';
   $smarty->compile_dir = 'templates_c';
   
// User Permissions
   session_start();
   
   if ( isset($_SESSION['user']) ) {
      $logged_in = "TRUE";
      $smarty->assign('logged_in', $logged_in);
      
      foreach ( $_SESSION['user'] as $key => $value ) {
         $smarty->assign($key, $value);
      }
      
   } else {
      $logged_in = "FALSE";
      $smarty->assign('logged_in', $logged_in);
   }

?>


includes/config.php
Code: Select all
<?php

// WEBSITE INFO

   DEFINE ('WEBSITE_URL', 'http://localhost/xion/'); // Database name.
   DEFINE ('WEBSITE_MAIN', 'index.php'); // Website main page.


// MySQL

   DEFINE ('DB_NAME', 'xion'); // Database name.
   DEFINE ('DB_USER', 'admin'); // Database user.
   DEFINE ('DB_PASS', 'admin'); // Database password.
   DEFINE ('DB_HOST', 'localhost'); // Database host.
   DEFINE ('DB_PFIX', 'xion_'); // Table prefix for multiple installs.

?>
c.nickerson
 
Posts: 6
Joined: 07. June 2013 00:23
XAMPP Version: 5.6.3
Operating System: Windows 7

Re: Include Relative Path Problem

Postby Altrea » 08. June 2013 19:30

Change
Code: Select all
require 'config.php';

to
Code: Select all
require __DIR__ . 'config.php';
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 6818
Joined: 17. August 2009 13:05
XAMPP Version: 5.5.19
Operating System: W7Ux64

Re: Include Relative Path Problem

Postby c.nickerson » 08. June 2013 22:50

Altrea wrote:Change
Code: Select all
require 'config.php';

to
Code: Select all
require __DIR__ . 'config.php';


Fixes that problem but I get a ton of more error message. Mostly undefined variables. Seems the Smarty variables aren't getting defined with the SmartyPHP. Something isn't right... It should work fine the way I was doing it. Always has for the years I've used XAMPP developing various web applications...

I don't know if its something different with new version of XAMPP or because this is Windows 7 (desktop is Vista).

Again like I said I made a test directory and did the same structure and it has no problems. Makes me think something happened to the files after I zipped them and uploaded them to my email and downloaded them on new laptop... IDK! Pulling my hair out! lol.
c.nickerson
 
Posts: 6
Joined: 07. June 2013 00:23
XAMPP Version: 5.6.3
Operating System: Windows 7

Re: Include Relative Path Problem

Postby c.nickerson » 09. June 2013 16:31

Seems to be the name config.php in the include file... I change the name and it works?
c.nickerson
 
Posts: 6
Joined: 07. June 2013 00:23
XAMPP Version: 5.6.3
Operating System: Windows 7

Re: Include Relative Path Problem

Postby Altrea » 09. June 2013 20:39

No.
PHP does have a defined procedure how to handle these includes and requires.

http://www.php.net/manual/en/function.include.php wrote: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. [...]

If a path is defined — whether absolute (starting with a drive letter or \ on Windows, or / on Unix/Linux systems) or relative to the current directory (starting with . or ..) — the include_path will be ignored altogether. For example, if a filename begins with ../, the parser will look in the parent directory to find the requested file.


Because you don't specified a path, the include_path is the first ressource the file will be searched. There are two entries by default in that include_path, first "." which is a representation for the current woring directory (which will be the file which gets requested and where all the other files get included to) and the second entry is PEAR and THERE you will find an config.php file too.
So it is not your file that gets included, but the PEAR file.

The root of your problem is, that you don't specify paths in your includes and requires. One solution that i have told you is using __DIR__ which is a representation of the Path the included file comes from.

So don't blame the name of the file, PEAR or someone else for that issue, it's you don't using require and include the way you should.

best wishes,
Altrea
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 6818
Joined: 17. August 2009 13:05
XAMPP Version: 5.5.19
Operating System: W7Ux64


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 41 guests