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 6 of 6

Thread: Full Path Links

  1. #1
    New Coder
    Join Date
    Aug 2003
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Full Path Links

    I want to give all my links the full path to the desiered page. (instead of <a href="myfile.php">, use <a href="http://www.site.com/myfile.php">. Incase I change domains or something like that I want to use a variable of some some sort that give my links the full path.

    Something like this maybe?:

    PHP Code:
    <?php

    $link_path 
    "http://www.mysite.com";

    ?>
    Then maybe...
    Code:
    <html>
    <head>
    <title>Title</title>
    </head>
    <body>
    
    <a href="{$link_path}/myfile.php</a>
    
    </body>
    </html>
    but I don't think this will work, so what should I do?
    Last edited by Goober; 12-26-2003 at 09:56 PM.

  • #2
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Two ways I'd do it are:

    PHP Code:
    <?php
    define
    ("SITEURL","http://yourdomain.com");
    ?>

    <html>
    <head>
    <title>Title</title>
    </head>
    <body>

    <a href="<?php echo SITEURL?>/myfile.php</a>

    </body>
    </html>
    Or
    PHP Code:
    <?php
    $siteurl 
    "http://yourdomain.com";
    ?>

    <html>
    <head>
    <title>Title</title>
    </head>
    <body>

    <a href="<?php echo $siteurl?>/myfile.php</a>

    </body>
    </html>

  • #3
    me'
    me' is offline
    Senior Coder
    Join Date
    Nov 2002
    Location
    Warwickshire, England
    Posts
    1,229
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Or even (a bit shorter and easier to read, IMO)
    Code:
    $domain = "www.site.com";
    
    ...
    
    <a href="<?=$domain?>/myfile.php">
    David House - Perfect is achieved, not when there is nothing left to add, but when there is nothing left to take away. (Antoine de St. Exupery).
    W3Schools | XHTML Validator | CSS Validator | Colours | Typography | HTML&CSS FAQ | Go get Mozilla Now | I blog!

  • #4
    New Coder
    Join Date
    Aug 2003
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts
    actually i just thought of a new one...

    you can simply define the variable and then print the html code in the php code like this:

    PHP Code:
    <?php

    $site_url 
    "http://www.mydomain.com"

    echo (

    <
    html>
    <
    head>
    <
    title>Title</title>
    </
    head>
    <
    body>

    <
    a href="{$site_url}/myfile.php</a>

    </body>
    </html>

    );
    ?>
    im not actually sure if that would work but it might.

  • #5
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    it would work for sure , but in general you would try and avoid echo()ing everything unless you actually needed to , sometimes functions or classes will return a block of HTML to be printed , but when doing so inline you are just making more work for the PHP interpreter which basically ignores everything that is not inside <?php ?> tags.

    the difference in efficiency is minimal but it exists.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #6
    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
    Alternatively, you could use

    Code:
    <?php
    print "<a href=\"http://".$_SERVER['HTTP_HOST']."/myFile.php\">Link</a>";
    ?>
    the $_SERVER['HTTP_HOST'] will always be http://mydomain.com


  •  

    Posting Permissions

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