skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    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.


  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>
            
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search