dongyun65343 2018-09-13 18:40
浏览 51

如何在PHP和JavaScript中进行重定向?

Problem description

Based on this answer I have made a PHP custom page for WordPress. The page do the follow: when I go to www.mysite.com/wp/GoTo/?URL=http://www.google.com, the browser redirect to www.google.com.

The problem is that I would like to use Google Analytics to register the access to the page, in particular when user access a link like the following www.mysite.com/wp/GoTo/?URL=http://www.google.com&utm_campaign=MyChampaign.

In other words I have my PHP redirect page

<?php


    /*
    Template Name: GoToPage
    */

    /**
     *
     * @package MyTheme
     */

    $url = $_GET["URL"];

    // Remove all illegal characters from a url
    $url = filter_var($url, FILTER_SANITIZE_URL);

    if (filter_var($url, FILTER_VALIDATE_URL)) {
        header("location: " . $url);
        exit;
    } else {
    echo("$url is not a valid URL");
    }
    ?>

And I have to insert the Google Analytics snippet.

1st attempt

This doesn't work. I think that the problem is that, for the redirect, I have to output ONLY the header and nothing else.

<?php


/*
Template Name: GoToPage
*/

/**
 * The template for displaying all pages.
 *
 * This is the template that displays all pages by default.
 * Please note that this is the WordPress construct of pages
 * and that other 'pages' on your WordPress site will use a
 * different template.
 *
 * @package MyTheme
 */

$url = $_GET["URL"];

// Remove all illegal characters from a url
$url = filter_var($url, FILTER_SANITIZE_URL);

if (filter_var($url, FILTER_VALIDATE_URL)) {
    <script type="text/javascript" >
    window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
    ga('create', 'UA-11111111-0', 'auto');
    ga('send', 'pageview');
    </script>
    header("location: " . $url);
    exit;
} else {
echo("$url is not a valid URL");
}
?>

2ns attempt

Based on this and this answers I have tried to convert the client side code from HTML to JavaScript code. This gives me a 404 error for every attemp I have made.

<?php


/*
Template Name: GoToPage
*/

/**
 * The template for displaying all pages.
 *
 * This is the template that displays all pages by default.
 * Please note that this is the WordPress construct of pages
 * and that other 'pages' on your WordPress site will use a
 * different template.
 *
 * @package MyTheme
 */

$url = $_GET["URL"];

// Remove all illegal characters from a url
$url = filter_var($url, FILTER_SANITIZE_URL);

if (filter_var($url, FILTER_VALIDATE_URL)) {
    echo """
        <head>
        <script type="text/javascript" >
            window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
            ga('create', 'UA-11111111-0', 'auto');
            // Plugins

            ga('send', 'pageview');
        </script>
        </head>
        <body onload="onWindowLoad()">
        <script>
            window.location.replace(".$url.");
        </script>
        <p>If the page does not automatically redirect please click on<a href=".$url.">this link</a>.</p>
        </body>
    """;
    exit;
} else {
echo("$url is not a valid URL");
}
?>

Any idea about how to include the GA snippet in my PHP redirect custom page?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度