SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2011
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Getting full URL

    Hi,

    I'm trying to get the full URL not URI for the current page. I have made a menu system and want to set the relevant <li> tag as "current".

    All the urls in the menu system are using mod_rewrite so if I use $_SERVER['REQUEST_URI'] it will return something like - index.php?mod=user&page=dashboard. What I want to find is "http://www.domain.com/dashboard/". Is it possible to get the URL instead of URI?

    Thanks for any help.

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Take a look at the output of:
    PHP Code:
    var_dump($_SERVER); 
    Which you should embed temporarily in the page you are trying to establish the url of.

    That spits out a lot of vars. I cannot remember exactly how to do what you want, hence a somewhat woolly answer... hth

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2011
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for your help, unfortunately it didn't return what I need. I can get the URI but not the URL, seems stupid it's not a $_SERVER variable.

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Well if this is what you are getting back:
    PHP Code:
    index.php?mod=user&page=dashboard 
    and you only want "dashboard" then you should be able to use something like this:
    PHP Code:
    // spoofing your actual value for test purposes
    $_SERVER['QUERY_STRING'] =  "mod=user&page=dashboard";

    parse_str$_SERVER['QUERY_STRING']  , $output);
    echo 
    $output['page'];  // dashboard 
    Then use that $output['page'] variable in your url string for your menu.

    parse_str man page contains sample I hacked that from

  5. #5
    SitePoint Enthusiast
    Join Date
    Jan 2011
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yea that won't work either, otherwise I could just use $_GET['page'];

    Basically I have a table with 100+ links in and I need to check if the link is the current active one - aka the url value equals the url for that page.

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    What I want to find is "http://www.domain.com/dashboard/".
    Well that is what I showed you - how to get that variable from the $_SERVER var
    PHP Code:
    $url "http://www.domain.com/" $output['page']; 
    Is your question how to match that var against the other 100?

  7. #7
    SitePoint Enthusiast
    Join Date
    Jan 2011
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So if I use the dashboard example again -
    page = http://www.domain.com/dashboard/ ... translates to ... http://www.domain.com/index.php?mod=user&page=dashboard

    In the database there's 3 rows -
    (id, url, text)
    1, /, Home
    2, dashboard/, Dashboard
    3, forum/, Forum

    What I need to do is using for example $row['url']; check something along the lines of -
    PHP Code:
    <?php
    $thisPage 
    'Unknown function';
    define'URL''http://www.domain.com/' );

    #mysql query & fetch array here

    if( $thisPage == URL.$row['url'] )
    {
    print 
    'this is the current page ' $row['text'];
    }
    ?>
    What I need to find is how to get the value for "$thisPage".

  8. #8
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    // spoofing your actual value for test purposes
    $_SERVER['QUERY_STRING'] =  "mod=user&page=dashboard";

    function 
    getThisPage(){
    parse_str$_SERVER['QUERY_STRING']  , $output);
    return 
    $output['page'];  // dashboard  
    }

    $thisPage getThisPage();

    if (
    $thisPage == $row['url'] ){
    // bingo , its a match


    No?

  9. #9
    SitePoint Enthusiast
    Join Date
    Jan 2011
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But then on the forum it won't work if it translates to http://www.domain.com/index.php?mod=forum.

    The navigation goes 3/4 layers deep and it needs to be able to tell that. I guess my only option seems to be typing out the equivalent query string in the db as well.

    Thanks for your help, I'm still open to ideas

  10. #10
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    function getThisPage(){

    parse_str$_SERVER['QUERY_STRING']  , $output);

    if( isset(
    $output['page']) ){
    return 
    $output['page'];  // page if it set 
    }
    elseif( isset(
    $output['mod']) ){
    return 
    $output['mod'];  // mod if it set  
    }else{
    return 
    '';  // return to index page onlu
    }



  11. #11
    SitePoint Enthusiast alexson's Avatar
    Join Date
    Jun 2010
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, This is the function I wrote that should work, I've used it on several sites:

    PHP Code:
    // Get url of current page, including get vars - all url encoded so they can be passed as a single argument
    // $use_get - if true then add $_GET arguments to end of filename

    function get_this_page($use_get=false){
        
        
    $files_arr explode("/"$_SERVER["SCRIPT_NAME"]);
        
    $thispage $files_arr[count($files_arr)-1];
        if (
    count($_GET) > && $use_get == true) {
            foreach (
    $_GET as $key => $value) {
                
    $thispagevars $key.'='.$value.'&amp;';
            }
            
    $thispagevars substr($thispagevars0, -5);
            
    $thispagevars '?'.urlencode($thispagevars);
        }
        else {
            
    $thispagevars '';
        }
        return 
    $thispage.$thispagevars;

    probably not the neatest way, but it's worked with everything I've thrown at it.

  12. #12
    SitePoint Enthusiast
    Join Date
    Jan 2011
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks alexson for that function, unfortunately it only returned "index.php", nothing else.

    Seem's like there is no shortcut for this, I'll just type out each query string for each link then match it that way.

    Thanks for your help.

  13. #13
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Add this code temporarily to one of your normal pages (where you are expecting menu items to be pre-selected), and see if there is any output.

    PHP Code:
    parse_str$_SERVER['QUERY_STRING']  , $output);
    var_dump$output );
    $files_arr explode("/"$_SERVER["SCRIPT_NAME"]);
    var_dump$files_arr); 

  14. #14
    SitePoint Enthusiast
    Join Date
    Jan 2011
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay that's started to output what I need, thanks. I'll try messing about with that and let you guys know how it goes.

  15. #15
    SitePoint Enthusiast alexson's Avatar
    Join Date
    Jun 2010
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When you used my function did you pass the true value? it's set to only default to filename otherwise you should call it using:
    PHP Code:
    $this_url get_this_page(true); 

  16. #16
    SitePoint Enthusiast
    Join Date
    Jan 2011
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by alexson View Post
    When you used my function did you pass the true value? it's set to only default to filename otherwise you should call it using:
    PHP Code:
    $this_url get_this_page(true); 
    I tried it with true again and it still just showed index.php. Thanks for posting though.

    This is what I've got working thanks to help from you both -
    PHP Code:
    parse_str$_SERVER['QUERY_STRING']  , $output );
    foreach( 
    $output as $i => $bit )
    {
        
    $output[$i] = urlencode$bit );
    }
    $thisPage implode'/'$output );
    $thisPage URL $thisPage;
    $thisPage = ( strrpos$thisPage'/' ) == strlen$thisPage ) ) ? $thisPage $thisPage '/'


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •