We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 11526
    • 93 Posts
    Hi,

    I am working on a snippet that will display a link to print that page.

    This will open the corresponding page in an other template, that is created for only one reason; print content (so without headers, sitebars,...)

    I am using the function $_SERVER[’HTTP_REFERER’] in my PrintPage-snippet to see where the user came from, so the right page is displayed in the print-template.

    However, $_SERVER[’HTTP_REFERER’] is giving me
    http//www.mydomain.com/index.phpid55 

    in stead of
    http://www.mydomain.com/index.php?id=55

    When i don ’t use the function $_SERVER[’HTTP_REFERER’] in a snippet, but in a php-file, the returned link is correct (with :, ? and =).

    So i am sure it isn’t server-related, but MODx related.

    Any idea’s on what is causing this problem and how to solve them? I am using MODx 0.9.2.1 (rev 1005).

    Thanks!
    Gust
      • 22815
      • 1,097 Posts
      I don’t know, but I wouldn’t recommend this method of getting the page ID, not least because many Print routines in browsers reload the page. I’m not sure what the HTTP_REFERER is supposed to be in that instance, but I bet at least one browser out there sends the printer-friendly page itself as the referrer.

      I think you’ll find it more reliable to pass the ID through in the snippet that links to the print page.
        No, I don't know what OpenGeek's saying half the time either.
        MODx Documentation: The Wiki | My Wiki contributions | Main MODx Documentation
        Forum: Where to post threads about add-ons | Forum Rules
        Like MODx? donate (and/or share your resources)
        Like me? See my Amazon wishlist
        MODx "Most Promising CMS" - so appropriate!
        • 11526
        • 93 Posts
        any suggestions how? grin
          • 727
          • 502 Posts
          I did a similar thing - i.e. I used the TemplateSwitcher snippet that you can find on here to switch to the print template when a link was clicked. I can’t remember all the details but I think it forced me to make all the pages with the link uncached.

          Instead I now use a seperate css stylesheet for the "print" media type. This has the advantage that the user can print any page and they can use the print button in their browser, rather than searching for a link. Also makes the site simpler.

          Andy
            • 26709
            • 4 Posts
            global $HTTP_REFERER;
            
            echo "<LINK HREF=\"/assets/templates/konotop/style.css\" REL=\"stylesheet\" TYPE=\"text/css\" media=\"screen\">";
            
            $startingpoint = "<!-- startprint -->";
            $endingpoint = "<!-- stopprint -->";
            // let's turn off any ugly errors for a sec so we can use our own if necessary...
            error_reporting(0);
            // $read = fopen($HTTP_REFERER, "rb") ... this line may work better if you're using NT, or even FreeBSD
            $read = fopen($HTTP_REFERER, "r") or die("<br /><font face=\"Verdana\">Sorry! There is no access to this file directly. You must follow a link. <br /><br />Please click your browser's back button. </font>");
            // let's turn errors back on so we can debug if necessary
            error_reporting(1);
            
            $value = "";
            while(!feof($read)){
            $value .= fread($read, 10000); // reduce number to save server load
            }
            fclose($read);
            $start= strpos($value, "$startingpoint"); 
            $finish= strpos($value, "$endingpoint"); 
            $length= $finish-$start;
            $value=substr($value, $start, $length);
            
            function i_denude($variable) {
            return(eregi_replace("<img src=[^>]*>", "", $variable));
            }
            function i_denudef($variable) {
            return(eregi_replace("<font[^>]*>", "", $variable));
            }
            
            $PHPrint = ("$value");
            if ($stripImages == "yes") {
            $PHPrint = i_denude("$PHPrint");
            }
            
            $date=date("d. m. Y");
            $PHPrint = i_denudef("$PHPrint");
            $PHPrint = str_replace( "</font>", "", $PHPrint );
            $PHPrint = stripslashes("$PHPrint"); 
            
            echo"<input type=button value=\"Ïå÷àòàòü\" onClick=\"print(document);\">";
            echo"<input type=button value=\"Çàêðûòü îêíî\" onClick=\"javascript:self.close();\"><br><br>";
            echo "<table width=100% bgColor=#ccd9e9 class=votequest><tr><td>Ðàñïå÷àòàíî: ".$date."</td></tr></table><br>";
            echo "<base href=\"$baseURL\">";
            
            
            echo $PHPrint; 
            // Next line is invisible except to SE crawlers, please don't remove. Thanks! :)
            echo "<br/><br/>Ñòðàíèöà îòïå÷àòàíà ñ ñàéòà: $HTTP_REFERER";
            flush ();