0

I don't know how to search for it, so I'm asking you here.

Situation is the following:

Got a vServer with root. Apache, PHP5, MySQL installed and running.

Now I have an index.php where I want to include 'config.php';. Simple thing!

In the config.php I have a variable like $url = 'http://xxxxxxx';, but I cannot access it in the index.php. There's just an empty Array, when I print_r(parse_url($url)); it.

The curious thing is, when I'm connected with ssh to the server and run php index.php, the output is the whole array as expected.

Do you have any idea?!

4
  • 2
    Try switching from include to require. The only thing I can think of that might be causing that is your included file not being found. Commented Dec 10, 2013 at 15:51
  • Just tried it, it also doesnt work. When I want to include a not existing file, my site doesnt load anything (maybe need to fix the error handling..) Commented Dec 10, 2013 at 16:01
  • I'm not sure what you mean; when you used require did you get a message saying it couldn't find the file (and so didn't work)? Or did it try to run, but still displayed an empty array for $url? Commented Dec 10, 2013 at 16:03
  • I think I destroyed my php.ini (something with display_errors), so the whole site isnt loading, when I require a not existing file. Need to fix it later when I'm at home. Commented Dec 10, 2013 at 16:11

3 Answers 3

1

Have you tried to include your config.php with absolute path, eg. include('/var/www/config.php');

Sign up to request clarification or add additional context in comments.

1 Comment

Try var_dump($url) and see if its recognized.
0

The best strategy for a config file would be having it written in the following format:

<?php
    return array(
        'url' => 'http://xxxxxxx',
        ...
    );

Then in your index.php you can do $config = require('wherever/it/is/config.php') and access your parameters like $config['url']. Otherwise you would probably have to use globals which is almost never a good practice.

3 Comments

This is how I want to do it later, but for now I just need to access any variable, before extending my config. Thanks!
Ok, then just make sure you are using the global version of the $url variable, 'cause if, for example, you're doing your include, then are trying to use the variable from within a function without declaring it as global, it will be a completely different variable. See here for details. Sorry, if I'm stating the obvious, but without the complete listing all I can do is assume. :)
Is it something similar to the following? index.php: <?php include 'config.php'; echo $url."\n"; ?> and config.php: <?php $url = 'http://somethingcool.com/'; ?> It's always a good idea to provide a kind of listing if you want to receive some meaningful answers.
0

Damn, I'm so stupid.

I had something like:

config.php

foreach {
    if(true){
        something;
        return false;
    }
}
$url = ...;

I just wanted to stop the foreach if it was successful. Maybe worked too good..

But I don't understand, why it hadn't any errors when phping it in the terminal..

Thank you guys!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.