在采取行动之前验证表单字段链接

时间:2016-08-18 16:25:09

标签: php html paypal

好的,所以我需要这样做的主要原因是因为我需要通过自定义字段将会话变量传递给我的PayPal IPN。 (至少那是我想到的将变量会话传递给IPN的唯一解决方案)

现在,我需要检查是否有人使用浏览器检查元素将id更改为另一个id。正如您所看到的,我已经在下面使用了if语句,如果我删除了操作链接,但是由于表单操作已设置,它将不会运行if set语句。所以问题是,如何在不删除操作链接的情况下或在转到操作链接之前验证表单字段?

如果这不可能,有没有其他选择呢? (或者至少,有没有其他方法将会话变量传递给PayPal IPN而不在HTML中显示id?)

它已经差不多2天试图让这个工作起来没什么......

...
<?php
    if (isset($_POST['submit'])) {
        if ($_POST['custom'] == $_SESSION['id']) {
            header('Location: https://www.sandbox.paypal.com/cgi-bin/webscr'); // This is what I tried but no success. (I did remove the action link when I added this)
        } else {
            header('Refresh: 0');
        }
    }
?>
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" target="_top"> <!-- I want it to verify first if the $_POST['custom'] is equal to the $_SESSION['id'] before it goes to the PayPal website.-->
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="*********">
<table>
    <tr>
        <td>
            <input type="hidden" name="on0" value="Items">Items
        </td>
    </tr>
    <tr>
        <td>
            <select name="os0">
                <option value="Item1">Item1 $1.00 USD</option>
                <option value="Item2">Item2 $2.00 USD</option>
            </select>
        </td>
    </tr>
</table>
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="custom" value="<?php echo $_SESSION['id']; ?>"/>
<input type="submit" name="submit" value="Buy now" />
</form>
?>
...

1 个答案:

答案 0 :(得分:0)

我建议将表单数据发送到服务器上的另一个页面,然后将会话信息和帖子添加到paypal。另外,我不确定为什么在表格中使用表格。这似乎没必要。

用户输入页面

<form action="path/to/submit_page.php" method="post" target="_top"> <!-- I want it to verify first if the $_POST['custom'] is equal to the $_SESSION['id'] before it goes to the PayPal website.-->
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="*********">
<table>
    <tr>
        <td>
            <input type="hidden" name="on0" value="Items">Items
        </td>
    </tr>
    <tr>
        <td>
            <select name="os0">
                <option value="Item1">Item1 $1.00 USD</option>
                <option value="Item2">Item2 $2.00 USD</option>
            </select>
        </td>
    </tr>
</table>
<input type="hidden" name="currency_code" value="USD">
<input type="submit" name="submit" value="Buy now" />
</form>

<强> submit_page.php

<?php

session_start();

$url = 'https://www.sandbox.paypal.com/cgi-bin/webscr';

// Specify everything you need to send here
$data = array('os0' => $_POST['os0'], 'currency_code' => $_POST['currency_code'], 'custom' => $_SESSION['id'], ... );

$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data)
    )
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ }

// Send user to success page