【问题标题】:How to implement urlencode in a HTML form that uses post?如何在使用 post 的 HTML 表单中实现 urlencode?
【发布时间】:2016-06-03 23:48:49
【问题描述】:

鉴于此 HTML:

 <input type="text" class="form-control" name="link" rows="3" action="add2.php" method="post"  placeholder="link here">

我想先对该 URL 进行编码,然后将其发送到 add2.php

add2.php如下:

    <?php

$url=$_GET["linki"];

       include("../db.php");
       $url=$_GET["linki"];
       $html = file_get_contents("$url");
       preg_match('/<meta property="og:image" content="(.*?)" \/>/', $html, $matches);
       preg_match("/<title>(.*)<\/title>/i", $html, $title);

       $pdo -> exec("INSERT INTO `postss`(`link`) VALUES ('$w')");

    ?>

当我尝试汇总 URL 时,服务器回复“fordinended 错误”。

【问题讨论】:

  • 那肯定不是一个有效的文本输入。 (文本输入本身没有参数action。它放在带有该参数和提交按钮的表单中)
  • 为什么?它的引导形式
  • 是的,你是真的,我有 sumbot 按钮,但它不是问题/...

标签: php decode encode


【解决方案1】:

为了简短起见:您的示例未显示有效的 HTML 表单,并且您的问题未反映您要在代码中完成的任务。

不过,这可能会给你一个起点。

HTML 表单:

<form action="add2.php" method="POST">
    <input type="text" name="link" class="form-control" placeholder="Link here">
    <input type="submit" name="submit" class="btn btn-default" value="Submit">
</form>

add2.php 用于处理(由于我不能/不会验证您的代码本身的功能,所以从您的 Q 内部部分复制/粘贴):

<?php
include("../db.php");

if (isset($_POST["submit"]))
{
    $link = $_POST["link"];
    $html = file_get_contents($link);

    preg_match('/<meta property="og:image" content="(.*?)" \/>/', $html, $matches);
    preg_match("/<title>(.*)<\/title>/i", $html, $title);
    $pdo->exec("INSERT INTO `postss`(`link`) VALUES ('$link')"); // Homework: SQL Injection, read up on it. This is still vulnerable.
}

请注意,您也没有使用您对原始问题中收到的内容所做的任何事情......上面的代码的一半根本没用,另一半需要阅读 SQL 注入。不过应该可以帮助您开始提交和处理表单。

【讨论】:

  • 它在 mysql $pdo 中保存的标题和图像 -> exec("INSERT INTO postss(link) VALUES ('$w')");
  • 可能托管不允许通过邮寄或获取方式发送链接
  • 阅读注释...这与url编码等无关。您也没有使用您在问题中创建的值的一半($matches$title 从下载的 html 中解析但从未使用过,这使得 $html 和下载本身相当无用)。
  • 我只是将它保存在 sql 中,然后我将使用它到另一个页面。
  • 1) StackOverflow cmets 不是聊天。 2) 我刚刚向您展示了如何处理 POST 表单的示例。您使用$_POST 而不是$_GET。我没有调试你的整个 sn-p。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-06
  • 2018-09-23
  • 1970-01-01
  • 2021-03-09
  • 2021-05-04
相关资源
最近更新 更多