Hello Could someone help me out with some simple php code to display the http referer in a page. I know how to detect it, but want to output the results to the page. <?php echo $_SERVER['HTTP_REFERER']; ?> Show the referer, and if referer is blank show message : referer is blank I'll chuck $5 to who ever can help me out Thanks
<?php if( isset( $_SERVER['HTTP_REFERER'] ) && $_SERVER['HTTP_REFERER'] != "" ){ echo "<p> Referer: ". $_SERVER['HTTP_REFERER']. "</p>"; }else{ echo "<p> No Referer </p>"; } ?>
Even simpler: echo empty($_SERVER['HTTP_REFERER']) ? 'Referrer is blank' : $_SERVER['HTTP_REFERER']; Code (markup): Ternary operators are your friend. Also keep in mind that "referer" is misspelt. Whilst PHP / HTTP uses HTTP_REFERER the actual word you should use in your output is referrer. There's no such word as "referer" in English.
further hint php.net is amazing var_export variables so you can see what's in them, what the right spelling is. Not getting anything from $_SERVER['HTTP_REFERER'], then try $_SERVER Do not give links back on your page to your referrers, you'll be spammed to death to get you to link to dodgy sites.
Actually I keep forgetting that as of PHP 7, they added a "null coalescing operator" https://www.php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op So in fact this could just be done as: echo $_SERVER['HTTP_REFERER'] ?? 'Referrer is blank'; Code (markup): ... and @sarahk, what are you even on about?!?
That part. I mean so long as you set the anchor to rel="nofollow noindex" you should be fine. Though I'd assume this is related to some sort of buffered logging. I often set buffers up so I can echo out certain logs during my processing stage, then switch them off (or set to a gzhandler) for my output stage. Separation of concerns and all that. Also easier during testing to route echo to output instead of the logs by just changing one line. Same code, handles both functions.