Skip to content
Advertisement

How to show download button to redirected users from a specific website

I have a free software downloading site on wordpress. Anyone clicking on the download button is being redirected to another site page in which there is the download link for the software.

Example: Clicking on download button in “https://example1.com/510-2/” users are being redirected to “https://example2.com/608/”. Then clicking on “Unlock Download Links” on the top of the second page, a hidden div (button containing the download link) on the bottom of the page is visible.

Problem: Thus, anyone can copy and share the url of second site page without viewing the main site page (https://example1.com).

Expected Output: Only users being redirected from “https://example1.com/510-2/” could be able to see the “Unlock Download Links” on the top of the second page. If anyone tries to copy and reopen the second page url “https://example2.com/608/”, won’t be able to see the “Unlock Download Links” div.

example2.com post format:

    
    <h2 style="text-align: center;"><strong>
    <a onclick='document.getElementById("download").style.display = "block";' href="#wait">Unlock Download Link</a>
    </strong></h2>
    
    <style>
    #download
    {display: none}
    </style>
    
    <p>Lorem Ipsum is simply dummy text</p>
    
    <div id="download">
    <a href="file download link" target="_blank" rel="noopener noreferrer">Download</a>
    </div>

Advertisement

Answer

Doing this in PHP, you could create a form, set the method attribute of the form to post and set the action attribute of the form to point to your https://example2.com/608/ page that hosts the downloadable content for your users. Then on your downloadable content page have a conditional that checks if the global array for $_POST is set else throw an error on a redirect to your initial page.

The form would look like so:

NOTE: the page hosting the form would need to be .php page to use php on that page

<form method="post" action="https://example2.com/608/">
  <?=$error?><!-- PHP for echo $error variable later defined I'll cover this later in explanation -->
  <input type="submit" value="Click Here" name="download">
</form>

When this form is submitted, it will send a post global variable for the inputs name, download. On the https://example2.com/608/ page, which would need to be a php page, you would have php code at the top of the html that would see if global variable $_POST has a key for download. To do this, you could use isset() in php inside of a conditional if statement. Something like the following: if(isset($_POST['download'])) –> if the POST global has a key called download set in the global POST array. The code will evaluate the conditional and if the $_POST array has a key for download it will return true.

Above the conditional you could define a variable for display and set it to NULL, then if the conditional returns true, set the variable to display the downloadable content.

Something like:

$output = NULL;    

//--> now for the conditional that will see if the `$_POST` variable 
//--> is set and then define the $output variable to display the downloadable content
//--> or throw a redirect header back to the page you wish users to view prior to downloading 
//--> along with an error using the URL $_GET method
if(isset($_POST['download'])){
    $output = //--> add your downloadable html content here...
}else{
    header("Location: https://example1.com/510-2/?error");
    exit;//<-- EXIT THE PHP CODE!
}
//<-- If our evaluation was true we echo out the $output variable in our html
    
<div>
    <?=output?><!--// Short tag for php echo within html content on a php page //--> 
</div>

header("Location: https://example1.com/510-2/?error"); redirects the user back to your initial page and then sends a $_GET global variable over the url that can be used on the target redirect page. So here we see if the $_POST isset and if it is we display the downloadable content, else if it is not set, we redirect the user automatically to the page you wish to display BEFORE they are allowed to download the content. We are essentially funneling the users through the chain we wish them to take to get to the prize: downloadable content, which must have a post variable set only by clicking on the button you want them to click on.

In the php redirect -> header() you see another global variable that is set over the server, ?error the ? is simply the start of a url defining key/value pairs in the url post… error is simply the key, no value is present. But we can check back on our redirected page using PHP to see if it is set, just like we did with the $_POST variable.

Now on https://example1.com/510-2/ we have some PHP code that checks to see if ‘error` is set in the $_GET global array, if so, we do the same thing we did on the downloadable content page to check except we use the $_GET method instead of $_POST method.

if(isset($_GET['error'])){
    $error = 'Please review this info for downloadable content...';
}else{
    $error = '';
}  

Now in the earlier form HTML, you notice a php short tag for echo -> <?=$error?> this is used directly in the php pages html, this is the same as <? php echo $error; ?>, also used for the $output variable as well. So if the conditional evaluates as false, it simply echos nothing and nothing is seen by the user, if it evaluates as true, we know the user was redirected back from the downloadable content without proper permission to download the content and it echos out the instructions we wish for them to take in order to get the downloadable content.

NOTE: I realize that this is all php code and not your original question using javascript, however because you are refreshing your pages and directing users to other pages for content, honestly the answer should be the use of a server side language like PHP or perhaps AJAX.

This is basic PHP code, everything I explain is easily re-searchable on the web and well defined in the php manual. Create a couple of dummy pages on your local host and play around with the POST and GET methods to refine your code.

https://www.php.net/manual/en/language.variables.superglobals.php https://www.php.net/manual/en/reserved.variables.get.php https://www.php.net/manual/en/reserved.variables.post.php https://www.php.net/manual/en/function.header.php
https://developer.mozilla.org/en-US/docs/Learn/Forms/Sending_and_retrieving_form_data

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement