0

I am trying to put together a redirect page so that I can place URLs in my emails and when clicked, it will run a tracking script, then redirect.

My URL's are formatted http://www.site1.com/redirect.php?http://www.site2.com

I wrote the redirect.php with the following code but I am just getting a white page when it loads. Permissions are 644.

<?php
    require( dirname( __FILE__ ) . '/wp-blog-header.php' );
?>

<!DOCTYPE HTML>
<html>
<script type="text/javascript">
    var trackcmp_email = '';
    var trackcmp = document.createElement("script");
    trackcmp.async = true;
    trackcmp.type = 'text/javascript';
    trackcmp.src = '//trackcmp.net/visit?actid=myid&e='+encodeURIComponent(trackcmp_email)+'&r='+encodeURIComponent(document.referrer)+'&u='+encodeURIComponent(window.location.href);
    var trackcmp_s = document.getElementsByTagName("script");
    if (trackcmp_s.length) {
        trackcmp_s[0].parentNode.appendChild(trackcmp);
    } else {
        var trackcmp_h = document.getElementsByTagName("head");
        trackcmp_h.length && trackcmp_h[0].appendChild(trackcmp);
    }
</script>
</html>

<?php
    $uri = $_SERVER['REQUEST_URI'];
    $pieces = explode("?", $uri);
    $newURL = intval( $pieces[1] );
    header( "HTTP/1.0 302 Found" );
    header( "Status: 302" );
    header('Location: '.$newURL);

?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Brian
  • 622
  • 10
  • 29
  • 1
    White page, try error reporting http://php.net/manual/en/function.error-reporting.php and look at your console. – Funk Forty Niner Feb 15 '17 at 14:49
  • A Parser-blocking, cross-origin script, http://ajax.cloudflare.com/cdn-cgi/nexp/dok3v=f2befc48d1/cloudflare.min.js, is invoked via document.write. This may be blocked by the browser if the device has poor network connectivity. See https://www.chromestatus.com/feature/5718547946799104 for more details. – Brian Feb 15 '17 at 14:54
  • @brian I think found is used on http/1.1, could you please try that – iCezz Feb 15 '17 at 14:57
  • @iCezz same error using 1.1 Also tried removing those two lines completely with no luck. – Brian Feb 15 '17 at 15:00
  • @brian how about using 1.1 and change to 307 – iCezz Feb 15 '17 at 15:03
  • no go..maybe this is my issue http://stackoverflow.com/a/33802849/972017 – Brian Feb 15 '17 at 15:14
  • You might checkout http://stackoverflow.com/a/8028987/296889 I think the issue is that you cannot redirect after you've already sent output (i.e. JS tracking code). You might consider either moving the tracking to the server before the redirect or using a JS redirect i.e. `window.location = someNewLocation`. – jeremysawesome Feb 15 '17 at 16:17

1 Answers1

0

I think its because the $newUrl is an int. In this case a zero. So when you try to

header('Location: '.$newURL);

its like

header('Location: 0');

change

$newURL = $pieces[1];

and the redirect will work.

But the javascript will not be executed.

Maybe you can try something like this:

header( 'refresh:5;url=' . $pieces[1] ); 

instead of header('Location:' . $pieces[1]);

But it feels a little bit dirty and its not a 302 redirect! Hope it helps anyhow.