Conditionally Exit() PHP script on URL parameter

时间:2018-04-18 17:54:05

标签: php html pdf-generation

I have a script that builds a document into a pdf after a form has been completed, it's a custom template script for Gravitypdf, after the user has submitted the form they click on a link to generate and download the pdf.

If I exit() the script at the very end of the template code it displays the plain text page generated for the compilation in the browser without making the pdf. So far so good.

I want to have working the existing link to the pdf and an alternative link to the html (text in browser) version on the confirmation page after submission.

I am trying to code that if the url happens to be xyz (the link for the html version) then execute the exit() command and output the html version. If it is anything other than that url then ignore that command and fully compile the pdf eg when the alternative pdf download link is chosen.

I thought initially I should be looking at using exit(header('Location: xxxxx.php')); but found this is a redirect, I then looked at <?php if ($_SERVER['REQUEST_URI'] === 'https://example.com/pdf/5a47cec7d4b62/{entry_id}/textversion') { exit(); } but that's not working.

How should I go about doing this? (The {entry_id} part of the url varies for each fillout of the form when the link generates via Gravitypdf - that works fine).

Thanks in advance.

EDITED UPDATE: I managed to get it working and tied in preg_match using ([^&]*) for the variable in the url. My clickable link for user is eg. example.com/pdf/5a47cec7d4b62/{entry_id}/textversion

`<?php
function currentUrl( $trim_query_string = false ) {
$pageURL = (isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on') ? 
"https://" : "http://";
$pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
if( ! $trim_query_string ) {
    return $pageURL;
} else {
    $url = explode( '?', $pageURL );
    return $url[0];
}
}
?>
<?php if 
(preg_match("#^https://example.com/pdf/5a47cec7d4b62/([^&]*)/textversion#", 
currentUrl())): ?> 
<?php exit() ?>
<?php endif; ?>  `

0 个答案:

没有答案