Share facebook Include PHP

2

I have a two buttons to share content from a web page (one of fb and one of tw).

I am using inlcude to repeat the content on all pages <?php include 'share.php';?> . The problem is that Facebook asks me for a specific url for each page, how can I achieve this if include only allows me to repeat content? Is there a way to detect the URL according to the page where I am? With Twitter, there is no problem (it automatically detects it).

<span class="text-share">Comparte:</span>

<div class="bt">
            <a class="bt-facebook" href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fkhapac.com/portafolio-pecsa-multimedia/">Facebook</a>
        </div>
        
<div class="bt">
            <a href="https://twitter.com/share" class="bt-twitter" data-hashtags="Khapac" target="_blank">Twitter</a>
            <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
        </div>
    
asked by Paco Zevallos 03.03.2017 в 20:04
source

1 answer

3

If you leave the URL empty (the parameter u ), then Facebook will automatically take the URL of the page where the share button is located. For example, the following code will open a window with the URL of the page that contains it:

<div class="bt">
  <a class="bt-facebook" href="https://www.facebook.com/sharer/sharer.php?u">Facebook</a>
</div>

If you prefer to write a URL, then you should look at this other SOes question, which explains what you are looking for: How to get the full URL in PHP?

    
answered by 03.03.2017 в 20:32