How to take a page link and assign it to a PHP variable?

Asked

Viewed 474 times

1

I’m wanting to use the Facebook plugins on comment, only I don’t want to have to keep changing the code on each page just because of the link. You can do a function that picks up the link automatically and assigns it to a variable to put this variable in the data-href in the code below?

<div class="fb-comments" data-href="http://developers.facebook.com/docs/plugins/comments/" data-numposts="5" data-colorscheme="light"></div>
  • Miguel, could you [Dit] your question by adding an example of how your code is? This makes it easier in the answer.

2 answers

1

In short...

<div class="fb-comments" data-href="<?= "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]" ?>" data-numposts="5" data-colorscheme="light"></div>

this way you will be putting in the date-href the address of the current page, however be careful, this practice is not very safe because the HTTP_HOST can be changed.

  • How can it be changed?

  • @Miguelgoffredo, yes, I can change the HTTP_HOST on my machine while accessing your site. Of course, the most I can achieve is to affect my navigation and if I comment on the post, I will comment with the way I want... but it only works for the person who is making the change. In your example, you can use without problem, but don’t use these variables for things that need more security.

  • I don’t quite understand why these HTTP_HOST variables and REQUEST_URI are not safe. I’m sorry if I’m being too "stupid" but could these variables be any other? I was trying to find a function to take the link and add it to a variable, but this way you did it worked too. And with the social plugins FB has to do moderation to accept or not the comment, I do not know if it gives a certain security, if you can help me I appreciate. @Rafaelribeiro

  • @Miguelgoffredo can be quiet, in your case do not have "insecurity" I just warned that depending on so that to use these variables can cause problems in the application, but this only in more specific use on servers.

-1

I think this will leave a bar missing, the right one would not be:

<div class="fb-comments" data-href="<?= "http://$_SERVER[HTTP_HOST]"."/"."$_SERVER[REQUEST_URI]" ?>" data-numposts="5" data-colorscheme="light"></div>
  • No... $_SERVER[REQUEST_URI] already comes with /

Browser other questions tagged

You are not signed in. Login or sign up in order to post.