Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Feb 2007
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post

    PHP to find the current URL?

    I need a simple bit of code for the following.

    I'm testing a site offline at http://localhost/sites/examplesite/index.php

    This problem is that I have to use full URL's in every link, I can't just do <a href="page.php">Page</a>.

    So there's a problem when I come to upload to the server since all the links will point to localhost.

    I tried:
    Code:
    <a href="<?php $_SERVER['PHP_SELF']; ?>/index.php" >Home</a>
    That code only finds the domain name and not the subfolders it's in. I don't know much PHP, but I need something simple like that if it's possible so that the links will work when I upload it.

  • #2
    Regular Coder BonRouge's Avatar
    Join Date
    Mar 2006
    Location
    Sendai, Japan
    Posts
    328
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Why do you have to use full URLs?

  • #3
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    Probably because their include files and he's on a dev environment.

    I usually have a global configuration file that I use to hold this type of info...

    Code:
    //globals.php
    define("SITE_PATH","http://localhost/devdirectory/");
    //more stuff...
    Then in my code...

    Code:
    <a href="<?=SITE_PATH?>/index.php">Home</a>

  • #4
    Regular Coder meth's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Thanks
    0
    Thanked 9 Times in 9 Posts
    I use a switch in the config file so I only have to change a single var for the same result:

    PHP Code:
    $config['env'] = 'local' // or test or live

    // site root
    switch ( $config['env'] ){
        
        case 
    'live':
        
    $config['root'] = 'http://www.someDomain.com.au/';
        break;
        case 
    'test':
        
    $config['root'] = 'http://test.server.net.au/~userName/';
        break;
        case 
    'local':
        
    $config['root'] = 'http://localhost/userName/public_html/';
        break;

    I do Web Design, Brisbane based.
    More time spent in PHP/MySQL Web Development.
    And Search Engine Optimisation takes up the rest of it.

  • #5
    New Coder
    Join Date
    Feb 2007
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post
    Thanks guys, those work

  • #6
    New to the CF scene
    Join Date
    Feb 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey mate,

    This is how I've solved that issue. Works well for me

    PHP Code:
    // This function gets the current directory name and splits it up into an array seperated by the '/' character
    function current_dir(){
       
    $var_dirfind explode('/'dirname($_SERVER['PHP_SELF']));
       return(
    $var_dirfind); // The return() statement immediately ends execution of the current function, and returns its argument as the value of the function call.
    /* return end($var_dirfind);    This statement immediately ends execution of the current function, and returns the LAST array value to the function call.    

    I use the return end() option to give me just the current file name. perhaps there's an easier method, I haven't found it yet though...
    */

    }
    $dir_now current_dir(); // and throw that array into a variable to call from later

    // Then to call all or bits of that out you can do the following:

    print_r ($dir_now); // shows the full array
    echo $dir_now[1]; // shows the array [1] value 
    Have fun!

  • #7
    New Coder
    Join Date
    Jan 2007
    Posts
    64
    Thanks
    0
    Thanked 1 Time in 1 Post
    another simple one is
    <a href="<?php $_SERVER['REQUEST_URI']; ?>/index.php" >Home</a>
    if u must have the full url
    No trees were harmed in the transmission of this message.
    However, a rather large number of electrons were temporarily
    inconvenienced.


  •  

    Posting Permissions

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