'get the original URL in PHP file from a Wordpress site

I have a Wordpress site with about 100 pages that contain a link to a contact form that is on a non-Wordpress site.

Let's call my Wordpress site https://example.com. Let's call one of the pages https://example.com/refinance-your-home-today/.

The contact form has the following code, which contains "https://example.com" as the original url (client_url in the code) about 75% of the time. Using the example above, the client_url should capture "https://example.com/refinance-your-home-today/."

When I use the following code, the client_url value is NULL:

<?php

session_start();
function wp_get_original_referer() {
if ( ! empty( $_REQUEST['_wp_original_http_referer'] ) && function_exists( 'wp_validate_redirect' ) ) {
    return wp_validate_redirect( wp_unslash( $_REQUEST['_wp_original_http_referer'] ), false );
}

return false;

}

$_SESSION['client_url'] = wp_get_original_referer();

When I use the following code the client_url appears correctly only about 25% of the time. It appears about 75% of the time as "https://example.com":

<?php

session_start();

$_SESSION['client_url'] = $_SERVER['HTTP_REFERER'];

Any help is greatly appreciated.



Solution 1:[1]

I found a workaround that solved my issue.

In the original URL page, I changed the a tag to include the client_url=(the page url).

In the index.php (1st page of the form), I added the following:

if(isset($_GET['client_url'])) {

$cu = $_GET['client_url'];

}

$_SESSION['cu'] = $_GET['client_url'];

echo $cu; // to verify the URL is being captured

Then in the body of the form I added the following:

<input type="hidden" name="client_url" value="<?php echo $_SESSION['cu']; ?>">

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hristian Yordanov