简体   繁体   中英

Auto-fill form with link to external page

I am looking for a way for a text field and submission button to take me to a new site, fill out a text input field and hit enter. The page I want to link to is Googles speed test . I know I can link to spped test results as well like this: https://developers.google.com/speed/pagespeed/insights#url=http_3A_2F_2FYOURDOMAINHERE&mobile=false but how can I have a customer fill out a "test my page" field on my site, hit submit, and it create a link to: https://developers.google.com/speed/pagespeed/insights#url=http_3A_2F_2FYOURDOMAINHERE&mobile=false with their field submission in the "YOURDOMAINHERE" area of the link. This seems linke not a huge task but i cannot wrap my head around it, php, javascript??? not sure. Any help would be greatly appreciated.

You can do something similar to this, although if Google catches you according to their TOC they *could* lock or ban your account:

<?php
if (!empty($_POST['url'])){
    $url = preg_replace('!http[s]://!','',strip_tags($_POST['url']));
    $url = preg_replace('![%]!','_',urlencode($url));
    $newlink = "https://developers.google.com/speed/pagespeed/insights#url=$url&mobile=false";

    $page = '<!doctype html>
<html lang="en">
<head>';

    $page .= '<script type="text/javascript">
    function replaceDoc()
  {
  window.location.replace("'.$newlink.'")
  }
    </script>';


$page .= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Page for testing</title>
</head>
<body onload="replaceDoc()">

    <a href="'.$newlink.'">Test your page.</a>

</body>
</html>
    ';
} else {
 $page = '<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Page Checker</title>
</head>
<body>
<form method="post">
Enter your URL:<br />
<input name="url" type="text" style="width:50em;" />
<input type="submit" name="submit" value="Check your page" />
</form>
</body>
</html>';

}

echo $page;
?>

Be careful directly injecting headers:

<?php
if (!empty($_POST['url'])){
    $url = preg_replace('!http[s]://!','',strip_tags($_POST['url']));
    $url = preg_replace('![%]!','_',urlencode($url));
    $newlink = "https://developers.google.com/speed/pagespeed/insights#url=$url&mobile=false";

    header("Content-Type: application/x-www-form-urlencoded");
    header("Referer: https://developers.google.com/speed/pagespeed/insights");
    header("Location: $newlink");

} else {
 $page = '<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Page Checker</title>
</head>
<body>
<form method="post">
Enter your URL:<br />
<input name="url" type="text" style="width:50em;" />
<input type="submit" name="submit" value="Check your page" />
</form>
</body>
</html>';

}

echo $page;
?>

make a submit.php

have the form submit to that script with the "DOMAIN NAME"

your php should look something like this..

$domainName = $_POST["domainname"];
$redirectURL = "https://developers.google.com/speed/pagespeed/insights#url=http_3A_2F_2F".$domainName."&mobile=false";
header('Location: $redirectURL');

That script should work havn't tested it but it'll give you an idea...

make sure you sanitize your inputs too... :) good luck

Hows about a simple str_replace ? Something like:

<?php

$link = "https://developers.google.com/speed/pagespeed/insights#url=http_3A_2F_2FYOURDOMAINHERE&mobile=false";
$replace = "YOURDOMAINHERE";

if(isset($_POST['myDomain'])){
  $newHeader = str_replace($replace, $_POST['myDomain'], $link);
  header("Location: ".$newHeader);
}

?>


<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">       
   <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>googleDomain</title>
    </head>
    <body>
        <form action="domain.php" method="post">
            <input type="text" name="myDomain" />
            <input type="submit" />
        </form>
    </body>
</html>

I haven't tested this at all, but should be something pretty similar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM