Page 1 of 1

PHP_SELF and dirname function behavior. Hint Please!

Posted: Wed Apr 14, 2010 11:07 pm
by crisstina
Hello all,

I created a folder with a file that works as a redirect function, so the users do not need to learn a long login link.

Here is the file content and the syntax of the function:

<?php
header('Location: ' . dirname(dirname($_SERVER['PHP_SELF'])) . '/index.php?option=com_user&view=login');


The folder name is /login and it is located on the domain's root folder.

When users go through the main domain it works like a charm:

If they enter:
www.domain.com/login
They are redirected to:
www.domain.com/index.php?option=com_user&view=login

which is what I want.

-------BUT

When they go through a subdomain:
subdomain.domain.com/login
They are redirected to:
http://www.index.php/?option=com_user&view=login


I do not understand this difference on the result. :crazy: It should do the same! Please help!



Any ideas?

Thank you all!

Re: PHP_SELF and dirname function behavior. Hint Please!

Posted: Fri Apr 16, 2010 2:23 am
by mrcoffee
I'm not sure why the behavior would be different. I'd think you'd get the same problem you're having with the subdomain when running the script on your primary domain.

Given the script's location, dirname(dirname($_SERVER['PHP_SELF'])) should evaluate to '/' resulting in:

Code: Select all

header('Location: //index.php?option=com_user&view=login')
which would understandably try to redirect to the domain "index.php".

Since you're already including the literal character '/' in your redirect, you could use rtrim to get rid of any trailing '/':

Code: Select all

header('Location: ' . rtrim(dirname(dirname($_SERVER['PHP_SELF'], "/"))) . '/index.php?option=com_user&view=login');
However, since index.php is on the root level, I would just use:

Code: Select all

header("Location: http://' . $_SERVER['HTTP_HOST'] . '/index.php?option=com_user&view=login');
(unless perhaps I'm missing the point of what you're doing)

Hope that helps.

Re: PHP_SELF and dirname function behavior. Hint Please!

Posted: Fri Apr 16, 2010 6:49 pm
by crisstina
Worked like a charm! thank you very very much! I appreciate your answer.

Have a nice weekend!