share links for current page LINKEDIN and FACEBOOK(name taked by host name)

3.5k Views Asked by At

I make share buttons for my web page and my domain can be changed some times, that's why, I can't use static domain names in my links. The querstuion is: How can I create my share links, that should work on all domains? share links like that:

<li><a href="https://www.linkedin.com/cws/share?url=http://MYPAGE.com" target="_blank"><img src="img/1.png" alt="linkedin"></a></li>
<li><a href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2MYPAGE.com" target="_blank"><img src="img/2.png" alt="facebook"></a></li>
1

There are 1 best solutions below

1
On

i guess this would help

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

this will make a variable named site_url with your site address no matter what the domain was.. then you can simply trigger the share button with it like that

 <li><a href="https://www.linkedin.com/cws/share?url=<?php echo $site_url ;>?" target="_blank"><img src="img/1.png" alt="linkedin"></a></li>
<li><a href="https://www.facebook.com/sharer/sharer.php?u=<?php echo $site_url ;>?" target="_blank"><img src="img/2.png" alt="facebook"></a></li>

hope it help .. thanks