PHP, HTML, and Javascript Redirect Help

7 replies
Hey Warriors :-)

Previously I had:
Code:
<?php
$thankspage = $_POST['redirect'];
header("location: $thankspage");
?>
Now I want to include the Google Analytics snippet code, which is in a body-bottom.php file, into this page but cannot as this doesn't work.

Code:
<?php
$thankspage = $_POST['redirect'];
include($_SERVER['DOCUMENT_ROOT'].'/inc/body-bottom.php');
header("location: $thankspage");
?>
So I've come up with two alternatives that appear to work.

Code:
<?php
$thankspage = $_POST['redirect'];
?>
<html>
<head>
<meta http-equiv="refresh" content="0;url=<?php echo $thankspage ?>"> 
</head>
<body>
<?php include($_SERVER['DOCUMENT_ROOT'].'/inc/body-bottom.php'); ?>
</body>
</html>
Code:
<?php
$thankspage = $_POST['redirect'];
?>
<html>
<body onLoad="submit_form();">
<form name="myform" action="<?php echo $thankspage ?>" method="POST">
<p>Redirecting. If not, please <a href="javascript:document.myform.submit();">click here</a>.</p>
</form>
<?php include($_SERVER['DOCUMENT_ROOT'].'/inc/body-bottom.php'); ?>
</body>
</html>
My questions:
1) Would the body-bottom.php get loaded in both the meta and javascript redirect?
2) Is there some way to use the php redirect and include the Analytics file? I really like the php header redirect because you don't see the page load - creating a smoother (and seemingly safer) user experience.
#html #javascript #php #redirect
  • Profile picture of the author PHIMind
    Both solutions will work but the meta solution can give you mixed results since google analytics is a javascript-based analytical tool. The page might redirect faster than the GA code can run since you are using a refresh content=0 for the timer.

    Use the second one, even if it does not convey a transparent look to the redirection the code you have on "onLoad" will make the page only redirect after the GA code has run since it creates an image tag to run the analitics.

    Signature
    {{ DiscussionBoard.errors[3198105].message }}
  • Profile picture of the author Brandon Tanner
    Hey Joshua,

    I haven't been able to figure out a way to successfully integrate G Analytics into a php redirect page yet, so I would be curious if this is possible as well. Trying to output code to the browser (such as the Analytics code) before the redirect will result in a 'headers already sent' error. To get around that, I tried using 'output buffer' on the redirect page, but G Analytics still didn't pick up the page.

    If this is possible, I'd like to know how to do it as well.
    Signature

    {{ DiscussionBoard.errors[3198222].message }}
    • Profile picture of the author Joshua Uebergang
      Interesting points PHIMind. I'll definitely avoid the meta solution then.

      I forgot to mention I included the following in the body of the javascript option:

      Code:
      <script language="javascript">
      <!-- function submit_form() { document.myform.submit() } -->
      </script>
      Brandon, you're talking about "output buffer"... That's beyond me dude! You've got more chance at solving this than me!
      {{ DiscussionBoard.errors[3198314].message }}
  • Profile picture of the author webpro4hire
    Joshua,

    Is there a reason to include the GA code in the redirecting page, why not in the thank you page only?

    if you want to track who gets redirected (and who skips the thank you page), there are other ways.

    The only way I see this working flawlessly is with the Meta method, making the timer longer than 0 (say 5 seconds).

    Good luck,
    WP4H
    {{ DiscussionBoard.errors[3198589].message }}
  • Profile picture of the author AlexanderC
    Hey Joshua, if you want to use the header function you can't output anything to the page (so caching the last output in a buffer doesn't do anything because you still have to write it to the page so the header function still wont work).
    What you can do is to parse the globals $_SERVER variables and get the ip, browser version etc. Then figure out how GA formats the tracking url, then you can send a request to that url, before calling the headers function.
    {{ DiscussionBoard.errors[3199141].message }}
    • Profile picture of the author Joshua Uebergang
      I have to use this redirect page to track conversions for my split-testing software. I can't put php code on the thank you page (using Delavo).

      Hmm, I'll look into having the Analytics snippet on the actual thank you page. I could never get Google to recognize the url for Analytic Goals because the thank you url is different for every user.
      {{ DiscussionBoard.errors[3201834].message }}
  • Profile picture of the author indianbill007
    Php expert here. Ping me for advanced tips and techniqes
    {{ DiscussionBoard.errors[3204015].message }}

Trending Topics