PHP_SELF and dirname function behavior. Hint Please!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
crisstina
Forum Newbie
Posts: 4
Joined: Wed Apr 14, 2010 11:02 pm

PHP_SELF and dirname function behavior. Hint Please!

Post 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!
mrcoffee
Forum Commoner
Posts: 31
Joined: Tue Nov 10, 2009 3:03 pm
Location: Wyoming, USA

Re: PHP_SELF and dirname function behavior. Hint Please!

Post 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.
crisstina
Forum Newbie
Posts: 4
Joined: Wed Apr 14, 2010 11:02 pm

Re: PHP_SELF and dirname function behavior. Hint Please!

Post by crisstina »

Worked like a charm! thank you very very much! I appreciate your answer.

Have a nice weekend!
Post Reply