Absolute path with header location

Django Anonymous picture Django Anonymous · Mar 22, 2012 · Viewed 10.3k times · Source

abspath()

function abspath()
{
    echo $_SERVER['DOCUMENT_ROOT'];
}

directory()

function directory()
{
    echo '/folder/';
}

Code Line:-

header('Location:'.abspath().directory());

Gives me the following as output:

C:/xampp/htdocs/folder/

When I use

header('Location:'.$_SERVER['DOCUMENT_ROOT'];.directory());

It sends me to my index.php in C:/xampp/htdocs/folder/index.php Why its not working with functions?

I want to go to C:/xampp/htdocs/folder/index.php using this

header('Location:'.abspath().directory());

- Is there any problem?

Answer

chrisn picture chrisn · Mar 22, 2012

The problem is that your functions are echoing your output and not returning it. You'll want to change your functions to:

function abspath()
{
    return $_SERVER['DOCUMENT_ROOT'];
}

function directory()
{
    return '/folder/';
}

So you can use the returned value (namely $_SERVER['DOCUMENT_ROOT'] or '/folder/', in this case) in your string concatentiation.