Skip to content
Advertisement

Header not redirecting if __DIR__ is used

header ('Location:'.__DIR__.'/view/prijava_view.php'

If I use above header in index.php file it just shows a blank page with “localhost” URL. It works if I use include instead of header though.

Is this an issue with DIR magic constant? It seems you can’t use it in headers, or do I have a problem with my code?

Advertisement

Answer

__DIR__ is a file-system path. It has NOTHING to do with the URLs that client browsers will see. If your site’s files are physically stored at

/home/sites/example.com/html/view/projava_view.php

and the code inside this php script looks at DIR, you’ll get

/home/sites/example.com/html/view/

if you pass that out via a header() for redirect, you’ll be redirecting to a file-system path

header("Location: http://example.com/home/site/example.com/html/view/.....");

which is NOT reachable by external users. That path is NOT inside your document root, and the browser will actually be requesting the full path, which the server will then tack on the document root AGAIN, so the full end-request would actually be

/home/sites/example.com/html/view/home/sites/example.com/html/view/projava_view.php

In general, the __DIR__ magic constant is utterly useless when trying to build URL-space paths, because it’s not intended for use in URLs.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement