开发者

Getting the actual URL, best practices for this case

i need to insert the actual URL on this link:

<a href="http://www.face开发者_如何学运维book.com/sharer.php?u=**MY_URL**&t=<?php echo($title); ?>" target="blank">Share on Facebook</a>

What method will be the best? Thanks!


you can use the superglobal $_SERVER array ;-)

<?php
$myURL = 'http://'. $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
?>

and your link will be...

<a href="http://www.facebook.com/sharer.php?u=<?php echo($myURL); ?>&t=<?php echo($title); ?>" target="blank">Share on Facebook</a>

// EDIT:
$myURL should be urlencode()d and check for protocol -->

<?php
$protocol = 'http://';

if(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' || $_SERVER['SERVER_PORT'] == 443) {
  $protocol = 'https://';
}

$myURL = urlencode($protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜