0

I'm having some require_once() issues.

Code:

<?php 
$serverName = $_SERVER['SERVER_NAME'];

if ($serverName != 'xxx.xxx.xxx.xxx') {
    require_once($_SERVER['DOCUMENT_ROOT'] . "/config.php");
    } else {
    require_once($_SERVER['SERVER_NAME'] . "/config.php");
    }
?>

Errors/Warnings:

Warning: require_once() [function.require-once]: Unable to accesss xxx.xxx.xxx.xxx/config.php in /var/www/vhosts/site.com/httpdocs/inc/header.php on line 7


Warning: require_once(xxx.xxx.xxx.xxx/config.php) [function.require-once]: failed to open stream: No such file or directory in /var/www/vhosts/site.com/httpdocs/inc/header.php on line 7


Fatal error: require_once() [function.require]: Failed opening required 'xxx.xxx.xxx.xxx/config.php' (include_path='.:') in /var/www/vhosts/site.com/httpdocs/inc/header.php on line 7

The config.php file is being called in my header.php file. Everything works great locally, but once it's on the live server, I just get the errors above.

Not sure if it matters, but I'm accessing this via IP address since it's a development site.

Yes, the config.php files does exist in the root directory.

Any ideas?

Paul Dessert
  • 6,363
  • 8
  • 47
  • 74
  • Did you just `echo $_SERVER['SERVER_NAME]` to see what you're getting there? – Michael Berkowski Feb 28 '12 at 01:11
  • You cannot access php files via ip address for security reasons – Ibrahim Azhar Armar Feb 28 '12 at 01:13
  • @Michael - Yes, it returns the IP address – Paul Dessert Feb 28 '12 at 01:18
  • **note:** `$_SERVER['SERVER_NAME']` will return the hostname or ip depending on whats used to access the site. i cant see the point of your code, if using relative paths the you would not need to check hosts/ips – Lawrence Cherone Feb 28 '12 at 01:19
  • @LawrenceCherone - The code is called from my `header.php` file. It's used to load the `config.php` file. I use it to get back to the site root folder when I'm loading the `config.php` files from a page such as `mysite.com/subdir/otherfil.php`. The `if` statement is simply checking to see if I'm running on my local host or not. – Paul Dessert Feb 28 '12 at 01:22

2 Answers2

0

require_once need absolute path of script but $_SERVER['SERVER_NAME'] give url instead of path and hence failing to load.

require_once($_SERVER['SERVER_NAME'] . "/config.php");
Pramendra Gupta
  • 14,667
  • 4
  • 33
  • 34
  • 1
    `SERVER_NAME` holds the hostname, not the URL, so a path like `example.com/config.php` is valid for a shared host or virtualhost configuration which uses server names as directories... – Michael Berkowski Feb 28 '12 at 01:14
  • require_once will accept absolute OR relative paths, AND with allow_url_include enabled, will also load remote resources (that apply to include, include_once, require, require_once) – guido Feb 28 '12 at 01:17
  • @Michael well that is not the case for the OP, he would need to use `$_SERVER['SERVER_NAME'] . "../../config.php"` when he might as well use `./config.php` – Lawrence Cherone Feb 28 '12 at 01:23
0

You would be better of accessing the config file with a relative path:

<?php 
//header.php
$serverName = $_SERVER['SERVER_NAME'];

if ($serverName == 'localhost' || $serverName == '127.0.0.1') {
    require_once("./local_config.php");
}else{
    require_once("./config.php");
}
?>
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
  • @Paul are you still getting the same errors, you should maybe just print out `$_SERVER['SERVER_NAME']` and see if its set or use `include()` instead of the slower `require_once()` – Lawrence Cherone Feb 28 '12 at 01:34
  • Yes, `$_SERVER['SERVER_NAME']` is set. I just got of the phone with my hosting company, it looks like my server does not contain a trailing `/` but my local machine does. – Paul Dessert Feb 28 '12 at 01:47