1

I am installing Disqus Comment system on my website. I understand that I can dynamically call the page url or id via php and thus do not need to hardcode the URL or page ID everytime I have a Discus comment box.

This question asked 7 years ago this answer was provided by @Yogus but I couldn't get it to work.

<HTML> 
<title>HTML with PHP</title>
<body>
<h1>My Example</h1>

<?php
include 'testConnection.php';
?>

<b>Here is some more HTML</b>
<?php
//more php code
?>

</body>
</HTML>

I've hear that to use PHP I must change my website pages to end in .php rather than .html. I don't want to do this for every page as it would probably affect SEO and I've currently 325 pages.

I'd like to do a call to a PHP file, have it return a variable with the URL or Page ID and then I can plug this into the Discus. I've read there is a dedicated variable maybe $_SERVER which returns the page URL or ID.

Any help would be appreciated! Oh, a link to one page where I've Discus installed is here {go to bottom}. This page is hard coded in. coinsandhistory.com/countries/Ancients_Rome/Ancients_48_Rome_Empire_Tetrarchy-Constantine.html

  • _"This answer was provided by..."_ - What answer? I also don't see how the posted code have anything to do with either your question or any answer? What is it suppose to show us? – M. Eriksson Dec 25 '20 at 21:54
  • The question is how to get the page url or page ID from a php code. It's explained in the title of the post. – Nicholas Bourbaki Dec 26 '20 at 18:40
  • I'll add some more information on the question. This will be some of the things that I've tried and what I'm trying to do. The "answer" provided isn't my code but was provided by someone else 7 years ago. Nonetheless I'll elucidate what I'm trying to do with the php. – Nicholas Bourbaki Dec 26 '20 at 19:26

2 Answers2

2

You can still keep your pages as HTML in this case.

You can make a call to the PHP page with Javascript, get the result via AJAX, and then use that result in a URL. In this example, I will feed the php a parameter that will determine what the URL will be. The HTML page will use javascript to populate the link in the anchor tag with the data coming from the php file.

example the PHP code would look something like:

<?php
$p = $_REQUEST['p'];

if($p == 'one')
   echo 'http://urlone.php';
   
if($p == 'two')
   echo 'http://urltwo.php';
   
?>

the HTML code would look something like:

             <html>
               <script>
                  
                function populateURL(param){

                  //get the anchor tag 
                  var link = document.getElementById('dlink');

                 //call the php file to return the url as a string, 
                 //then set the url of the anchor to that string
                  fetch('url.php')
                  .then(response => (response){dlink.href = response});  
                }
               //get the url if the value is 'one' 
               populateURL('one');
               </script>
               <body>
                  <a id="dlink">CLICK HERE </a>
               </body>
            </html>
        
  • 1
    Thanks very much. I'm working on it and will follow up soon about how it worked out. Thanks you very much. I've read other info that Ajax is indeed the way to go. – Nicholas Bourbaki Dec 28 '20 at 15:22
  • Sorry I can't get it to work. Chrome reports a run error in the script. Also there are 3 conditions that won't be met. My page URL will never be one or two. How can I pass the paramter "page URL" to the script? I don't want to pass "one". This code has an error: .then(response => (response){dlink.href = response}); I don't know what it does. Also I cannot have the fcn execute only on click, it should execute automatically. Thanks for any info you can provide. – Nicholas Bourbaki Dec 28 '20 at 16:47
  • Being I did not know the exact details of what you are working on, i provided this as just an idea on what to do. If you can get me more specifics, I would gladly give a working example for your exact scenario. @NicholasBourbaki – Oluwakayode Dagbo Jan 02 '21 at 00:31
0

I found a working answer on SO from ~10 years ago. Get current URL with jQuery?

The solution was to use javascript from within the HTML which has queries for such things.

// Returns path only (/path/example.html)
var pathname = window.location.pathname; 
// Returns full URL (https://example.com/path/example.html)
var url      = window.location.href;  
// Returns base URL (https://example.com)   
var origin   = window.location.origin;   

To look at the results I just use the javascript to write out the answer, i.e

<p>JavaScript variables<br><br>
page url:
<script type="text/javascript"> document.write(page_url)
</script>

Thus php nor an external file wasn't needed. A note the javascript variable assigns MUST be done before the print commands, otherwise Chrome reports an error & nothing is displayed.