我可以消除在 html 中使用隐藏表单字段在数据是所有 table 行的行 ID 的页面之间传递数据吗
Can I eliminate use of hidden form fields in html to pass data between pages where the data is row id of all table rows
我正在开发一个网络应用程序,用户应在其中点击显示在订单旁边的 link 以生成订单详细信息 pdf
将特定用户的订单显示为具有两列的 table 的页面:- 订单时间和每个订单的 pdf link 具有此片段
echo '<table class="table"><tr>
<th>Order Submitted on</th>
<th>Get Details</th>
</tr>';
while ($row = $ordersByUser->fetch(PDO::FETCH_ASSOC)) {
echo '<tr><td>'.$row['timestamp'].'</td>';
echo '<td><form method="POST" action="generateorderpdf.php">
<input type ="hidden" name="orderid" value='.$row['id'].'>
<input type="submit" value="CLICK" class="btn btn-dark">
</form></td></tr>';
}
echo '</table>';
我将每个订单的主键
$row['id']
存储在一个隐藏字段中,然后将其发送到 generateOrderPdf.php 页面以使用 post 方法通过表单生成订单 pdf .我的问题是用户可以使用一些浏览器开发工具更改隐藏的输入字段并为其他用户生成 pdf,我绝对不希望用户这样做(这也是我向生成器发送 post 请求的原因pdf 页面,因为任何人都可以编辑 get url 并查看其他人的订单)。那么有什么方法可以消除对隐藏输入字段的依赖,将订单 ID 发送到 generateOrderPdf.php 页面?
我读到我可以使用会话来存储敏感数据,这样就不需要使用隐藏的表单字段,但我不知道是否可以使用会话变量来解决这个问题,如果可能的话如何因为这是 table 的数据?
实际上,您可以使用会话变量来做到这一点。
在会话中将所有订单 ID 放入一个数组中。不要将订单 ID 放入隐藏输入中,而是将数组索引放入。
$myorders = [];
$order_index = 0;
echo '<table class="table"><tr>
<th>Order Submitted on</th>
<th>Get Details</th>
</tr>';
while ($row = $ordersByUser->fetch(PDO::FETCH_ASSOC)) {
$myorders[$order_index] = $row['id'];
echo '<tr><td>'.$row['timestamp'].'</td>';
echo '<td><form method="POST" action="generateorderpdf.php">
<input type ="hidden" name="orderid" value="'.$order_index.'">
<input type="submit" value="CLICK" class="btn btn-dark">
</form></td></tr>';
$order_index++;
}
echo '</table>';
$_SESSION['myorders'] = $myorders;
然后在 generatorderpdf.php
中,您使用 $_SESSION['myorders'][$_POST['orderid']]
获取订单 ID。
我正在开发一个网络应用程序,用户应在其中点击显示在订单旁边的 link 以生成订单详细信息 pdf 将特定用户的订单显示为具有两列的 table 的页面:- 订单时间和每个订单的 pdf link 具有此片段
echo '<table class="table"><tr>
<th>Order Submitted on</th>
<th>Get Details</th>
</tr>';
while ($row = $ordersByUser->fetch(PDO::FETCH_ASSOC)) {
echo '<tr><td>'.$row['timestamp'].'</td>';
echo '<td><form method="POST" action="generateorderpdf.php">
<input type ="hidden" name="orderid" value='.$row['id'].'>
<input type="submit" value="CLICK" class="btn btn-dark">
</form></td></tr>';
}
echo '</table>';
我将每个订单的主键
$row['id']存储在一个隐藏字段中,然后将其发送到 generateOrderPdf.php 页面以使用 post 方法通过表单生成订单 pdf .我的问题是用户可以使用一些浏览器开发工具更改隐藏的输入字段并为其他用户生成 pdf,我绝对不希望用户这样做(这也是我向生成器发送 post 请求的原因pdf 页面,因为任何人都可以编辑 get url 并查看其他人的订单)。那么有什么方法可以消除对隐藏输入字段的依赖,将订单 ID 发送到 generateOrderPdf.php 页面?
我读到我可以使用会话来存储敏感数据,这样就不需要使用隐藏的表单字段,但我不知道是否可以使用会话变量来解决这个问题,如果可能的话如何因为这是 table 的数据?
实际上,您可以使用会话变量来做到这一点。
在会话中将所有订单 ID 放入一个数组中。不要将订单 ID 放入隐藏输入中,而是将数组索引放入。
$myorders = [];
$order_index = 0;
echo '<table class="table"><tr>
<th>Order Submitted on</th>
<th>Get Details</th>
</tr>';
while ($row = $ordersByUser->fetch(PDO::FETCH_ASSOC)) {
$myorders[$order_index] = $row['id'];
echo '<tr><td>'.$row['timestamp'].'</td>';
echo '<td><form method="POST" action="generateorderpdf.php">
<input type ="hidden" name="orderid" value="'.$order_index.'">
<input type="submit" value="CLICK" class="btn btn-dark">
</form></td></tr>';
$order_index++;
}
echo '</table>';
$_SESSION['myorders'] = $myorders;
然后在 generatorderpdf.php
中,您使用 $_SESSION['myorders'][$_POST['orderid']]
获取订单 ID。