获取实际的URL,本案例的最佳实践

时间:2011-01-24 23:15:09

标签: php javascript html

我需要在此链接上插入实际网址:

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

哪种方法最好?谢谢!

1 个答案:

答案 0 :(得分:2)

您可以使用超全局$_SERVER数组; - )

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

您的链接将是......

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

// 编辑
$myURL应为urlencode() d并检查协议 - &gt;

<?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']);
?>