为了账号安全,请及时绑定邮箱和手机立即绑定

Foreach 2 var $_POST

Foreach 2 var $_POST

MMMHUHU 2023-12-25 16:18:38
我有这个表格:<form method='post'>  <input name='i' type='hidden' value='' >  <input name='email' type='text' >  <input name='pass' type='password' >  <input type="submit" ></form>以及打印 .txt 文件中的数据的 PHP 代码:<?php  $handle = fopen("data.txt", "a");  foreach($_POST as $variable => $value) {    fwrite($handle, $variable);    fwrite($handle, "=");    fwrite($handle, $value);    fwrite($handle, "\r\n");  }  fwrite($handle, "\r\n");  fclose($handle);  exit;?>结果是:i = 价值电子邮件 = 价值通行证 = 价值我不需要打印$_POST['i']值。任何想法?
查看完整描述

1 回答

?
米脂

TA贡献1836条经验 获得超3个赞

添加一个 IF 来测试$variable循环i迭代,然后使用continue;


您还可以使用一个写入每一行的所有数据fwrite


<?php

$handle = fopen("data.txt", "a");

foreach($_POST as $variable => $value) {

    if ( $variable == 'i' ){

        continue;       // will stop this itereation and continue with the next

    }

    // make one write for all 4 bits of data.

    fwrite($handle, "$variable = $value \r\n");

}

fwrite($handle, "\r\n");

fclose($handle);


查看完整回答
反对 回复 2023-12-25
  • 1 回答
  • 0 关注
  • 33 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信