1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Best Way to Code Links for Navigation PHP or HTML?

Discussion in 'PHP' started by sem-profiteer, Aug 8, 2015.

  1. #1
    I'd like feedback on what you feel is the best way to do links on a website for navigation.

    For example:
    <a href="<?php echo 'http://' . $serverName;?>">Home</a> 
    Code (markup):
    or
    <a href="<?php echo 'http://' . $_SERVER['REQUEST_URL']; ?>">Home</a> 
    Code (markup):
    or
    <a href="http://www.my-web-site.com">Home</a> 
    Code (markup):
    or
    maybe there is a better way?
     
    sem-profiteer, Aug 8, 2015 IP
  2. lasersgopew

    lasersgopew Member

    Messages:
    15
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    48
    #2
    <a href="<?php echo 'http://' . $serverName;?>">Home</a> 
    Code (markup):
    Could be acceptable, depends on the origin of $serverName.

    <a href="<?php echo 'http://' . $_SERVER['REQUEST_URL']; ?>">Home</a> 
    Code (markup):
    Never, ever think for one second about doing this, as you're leaving yourself open to XSS and CSRF attacks. Also, it's "REQUEST_URI" not "REQUEST_URL".

    <a href="http://www.my-web-site.com">Home</a> 
    Code (markup):
    This is always okay.
     
    lasersgopew, Aug 8, 2015 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    Well... opening and closing PHP all the time is bad coding practice, and should be avoided. If this is a main menu (as it seems to be) why isn't this pulled from a database or something? Ie, put the whole menu function in a function (or class, depending on how you're building your page).

    Then you'll have something like this:
    
    echo '<a href="'.$_SERVER['SERVER_NAME'].'">Home</a>';
    
    PHP:
    usually, I do something like this in my config-file:
    
    $httptype = (isset($_SERVER['HTTPS'])) ? 'https' : 'http';
    $domain = ($_SERVER['SERVER_PORT'] == 80 || $_SERVER["SERVER_PORT"] == 443) ? $_SERVER['SERVER_NAME'] : $_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'];
    $rootfolder = '/'; //leave rootfolder as / if in installed in root webfolder
    $baseurl = $httptype.'://'.$domain.$rootfolder;
    
    PHP:
    Then you'd just use $baseurl whenever you'd need to use the website-url. Again, depends on size of project, and what you intend to do.
     
    PoPSiCLe, Aug 8, 2015 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #4
    Uhm... none of the above?

    <a href="/">Home</a>
    <a href="/forums">Forums</a>

    etc... etc... why are you dicking around with absolute URI's on what should be RELATIVE links?!? The browser already knows the domain, stop wasting bandwidth sending it again!

    I know bloated halfwit ineptly coded like turdpress does this, do the world a favor and do NOT follow their example!
     
    deathshadow, Aug 10, 2015 IP
    NetStar and ketting00 like this.
  5. SilentEntrepreneur

    SilentEntrepreneur Greenhorn

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #5
    I like what @deathshadow has used, I think you are looking to far into the situation.

    following off the root "/" and moving forward allows for the code to work across any domain name, meaning if you build something solid, it will work again for any project.

    I always go with <a href="/">Home</a> , if the server isn't configured to handle this, fix your server, not your code.

    Best of luck!
     
    SilentEntrepreneur, Aug 11, 2015 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    Personally, I prefer full links - I don't really care about the minimal overhead the added characters add to the code, and my solution is definitely cross-domain compatible, since the absolute urls aren't hardcoded.
     
    PoPSiCLe, Aug 12, 2015 IP
  7. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #7
    I prefer small links, less data the better! :) i use the <base tag> so every link knows where to go. :)
     
    EricBruggema, Aug 13, 2015 IP
    deathshadow likes this.
  8. Lalit Goyal

    Lalit Goyal Greenhorn

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #8
    I take the main page link in a constant:
    define('SITE_URL' , 'http://yoursiteurl.com');
    PHP:
    And use it in navigation.
     
    Lalit Goyal, Aug 14, 2015 IP
  9. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #9
    I fail to understand said preference - is there a legitimate reason to do so?

    Doesn't really come across as minimal once you end up with two or three k of them -- again, see the train wreck of ineptitude turdpress vomits up and has the brass to call a menu; between the absolute URI's and endless pointless classes for nothing, they seems to think a menu needs to be larger in code than the content of most sites is as plaintext.

    But not only takes more bandwidth to send, it also means processing data server-side you don't need to process server side. It's a waste of code for something you shouldn't be needing to do -- to be brutally frank it's dicking around doing something you don't even need to do in the first place.

    ... and if you DO need to do it, you are probably doing something horrifyingly and terrifyingly wrong with your HTML. Admittedly, that describes the result of the majority of PHP developers idea of what constitutes HTML!

    <base> is useful if you are doing a mass redirect to an index.php for friendly URI's while wanting to maintain the ability to run under a subdirectory. You end up with a link like:

    http://www.deathshadow.com/blog/20141231_Disqus+added+and+Code+Progress+Update/
    Code (markup):
    Relative links like "images/" would resolve under /blog instead of the root. Setting <base href="/"> eliminates that problem without having to say the leading / on every URI... or if you're on a testing server where I have /xampp/htdocs/deathshadow being served as localhost/deathshadow, <base href="/deathshadow/"> instantly handles it for me.

    But I wouldn't dive for that unless in such a redirect situation... though that does describe most sites I write as I'm a "one index.php to rule them all" kind of developer.

    Which is why:
    Is the final piece in how I do it in my own systems.

    	define('URI_BASE',
    		($temp = pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_DIRNAME)) == '.' ?
    		'/' :	str_replace('\\', '/', $temp) . '/'
    	);
    	echo '<base href="', URI_BASE, '">';
    Code (markup):
    No need to screw around detecing http vs. https, no need to slap it into every blasted URI for no good reason, corrects slashes for when winblows screws them up (and believe me, it will), even detects when DIRNAME is current and returns '.'

    Though if it's only ever going to run from the root of the domain:

    <base href="/">
    Code (markup):
    Is overkill. You do NOT need to be dicking around with the full URI, and if you DO need it, then you've probably done something really stupid... like having a directory structure that inhales the proverbial equine of short stature that would make your relative links resort to garbage like "../" which shouldn't even be allowed to work on URI's in the first place. (seriously, you know how many hacking methods would be instantly blocked if "../" was always rejected as invalid?)

    The ONLY reasons to resort to ANY of the methods presented in the first three posts of this thread are sloppy coding, sleazy practices, or an ignorance of how things work... All three typically propagated by people blindly copying and pasting from crappy tutorials and code examples online.
     
    Last edited: Aug 14, 2015
    deathshadow, Aug 14, 2015 IP
  10. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #10
    ... and hey look, Xenforo butchers URI's that have use the ACTUAL URI encoding of plus signs -- whoddathunkit... (oh wait, given the bloated slow asshat bullshit stunts it pulls turning things into links -- just about ANYONE who knows the first damned thing about the topic).

    Fine, edited into a code tag instead of URL.
     
    deathshadow, Aug 14, 2015 IP
  11. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #11
    Why? Forget about the additional bandwidth and all the crap that no one would notice....how about for the sake of ease and simplicity? Doing more work to accomplish the same outcome of same depth of quality is bad practice. Some times we form habits with coding preferences and formatting code that we create more work for ourselves. This is something I still struggle with but am slowly letting go of.
     
    NetStar, Aug 15, 2015 IP
  12. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #12
    Assuming you consider more code written for no good reason to be the "same depth of quality" -- though the past seven or eight years people seem to be throwing the word "quality" around a lot about things that have a clear and present lack of it. See all the idiotic frameworks, much less the garbage many CMS and Forums now vomit up and have the giant pair of donkey brass to call a website... or even just the PSD jockey "appearance first" approach that leaves me thinking I have a wildly different opinion of the meaning of "professional" than the current industry.

    Something I've been watching other developers do for some three and a half decades. Comments for no reason is a stunning example of this:

    <!-- start content -->
    <div id="content">
    Code (markup):
    No shit sherlock, a DIV called #content is the start of the content?

    But again, it's worse with garbage like jQueery or Bootcrap, where people seem bound and determined to make more work and write more of their own code, that then relies on even more code to even function -- then have a noodle doodle delusion that it's somehow faster, easier or made them more productive when the exact opposite is the case. Combine that with sleazy design asshattery like fixed metric fonts, fixed width layouts, fixed height background images and/or containers, and other concepts that never had any damned business on a website in the first place... Well, it's not exactly surprising that for many users -- like myself -- the web is now less functional and less useful than it was a decade ago; and SLOWER on broadband now than it was on dial-up a decade and a half ago when delivering the same basic information!!!

    Just look at these forums, take the PHP board index:
    https://forums.digitalpoint.com/forums/php.37/

    What the plowing blazes does that need 133 frelling K of HTML for, much less the batshit ratshit dirty old **** insane 488k of scripttardery for?!? You know it's bad when the markup and CSS combined are bigger than the blasted image total, and the scripting all on it's own is TWICE THAT. It's even sadder when bloated halfwit crap like Xenforo is actually better than some others on that front despite the painfully obvious developer ineptitude!

    It's WAY too easy to form bad habits, particularly in web development since there's SO MUCH bad advice out there. NOBODY, including the people writing the books and tutorials on how to do this stuff seems to be taking the time to understand the simplest of HTML's concepts -- which is why their CSS is rubbish -- and why since it's PHP's job to output HTML (it is called a Hypertext Preprocessor for a reason) the code most people vomit up on the back end suffers as well. Just a fraction more understanding can reduce all the endless pointless code-bloat asshattery to near nil, whilst at the same time resulting in easier to debug, easier to diagnose code...

    But that's NOT modern developments emphasis -- it's just all "credit mentality" now; We want it now, who cares what it costs later! I've watched WAY too many people with good ideas deep-six themselves by blindly believing bad practices, thinking that garbage frameworks and off the shelf solutions are legitimate, and in general willfully ignoring the endless good practices "grey-hairs" like myself keep trying to drive home; much less actual things like usability and accessibility that are now slaughtered on the altar of "gee ain't it neat" flashy bull designed to impress the "suits with checkbooks" who really know **** about **** when it comes to websites.

    Then people wonder why most Internet startups fail in their first year, and the majority of those that don't end up money pits or worse, time and manpower pits. But what can one expect in an age where endless online fly-by-night marketing scams is an accepted norm as people get more and more desperate for get rich quick schemes. Hardly a shock they dive for the "sleaze it out any old way as fast as possible" methodology when most such sites are throwaways to either be flipped in six months to some ignorant fool willing to fork over cash for vague promises, or intentionally allowed to fail once milked for the handful of coin they might make when new, so some new scam can be launched in its place.
     
    deathshadow, Aug 15, 2015 IP