Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am using the following function to get my server URL :

/* get url parameters */

    function url() {
        return sprintf(
                "%s://%s%s", isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http', $_SERVER['SERVER_NAME'], $_SERVER['REQUEST_URI']
        );
    }

The following is my url :

http://enunua.com/emarps/recover_password.php

From the above url , I want only the following parameters as part of my url :

http://enunua.com/emarps/

that is excluding recover_password.php but the above function gives me the whole url inclusive of recover_password.php, Please advise how can I be able to get only : http://enunua.com/emarps/ ?

share|improve this question
1  
return sprintf( "%s://%s%s", isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http', $_SERVER['SERVER_NAME'], dirname($_SERVER['PHP_SELF']) ); –  Navjot Singh Mar 16 at 13:38

1 Answer 1

You are using $_SERVER['REQUEST_URI'] which contains the whole Request-URL so also the "recover_password.php". You can use sth. like "strrpos" to get the URL without the filename.

Try to use this:

function url() {
    return sprintf(
        "%s://%s%s",
        isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http',
        $_SERVER['SERVER_NAME'],
        substr($_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'], '/') + 1)
    );
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.