php post xml_注意您的POST:将PHP POST数据另存为XML

php post xml

One of my main goals when creating PHP web forms is to keep them secure and protected from spammers and automated bots. With the amount of spam that Akismet catches every day, I don't need to be reminded of the importance of securing forms. Since 90+% of my forms are POST transmissions, I've taken a lot of time to develop POST debugging and listening code.

创建PHP Web表单时,我的主要目标之一是确保它们的安全并受到垃圾邮件发送者和自动bot的保护。 由于Akismet每天都会收到大量垃圾邮件,因此无需提醒我确保表单安全的重要性。 由于我90%以上的表单都是POST传输,因此我花了很多时间来开发POST调试和侦听代码。

One function I use to keep track of POST submissions is my custom print_r_xml() function. The function takes a given array (in my case, $_POST), cycles through each key, and places each key=>value into XML format. From there, I can save the XML to a file or place the XML into a database.

我用来跟踪POST提交的函数之一是我的自定义print_r_xml()函数。 该函数采用给定的数组(在我的情况下为$ _POST ),循环遍历每个键,然后将每个key => value放入XML格式。 从那里,我可以将XML保存到文件中或将XML放入数据库中。

The function can be used on any array ($_SESSION and $_GET would be good options too!).

该函数可以在任何数组上使用( $ _SESSION和$ _GET也是不错的选择!)。

PHP代码 (The PHP Code)

/* print the contents of a url */
function print_r_xml($arr,$wrapper = 'data',$cycle = 1)
{
	//useful vars
	$new_line = "\n";

	//start building content
	if($cycle == 1) { $output = '<?xml version="1.0" encoding="UTF-8" ?>'.$new_line; }
	$output.= tabify($cycle - 1).'<'.$wrapper.'>'.$new_line;
	foreach($arr as $key => $val)
	{
		if(!is_array($val))
		{
			$output.= tabify($cycle).'<'.htmlspecialchars($key).'>'.$val.'</'.htmlspecialchars($key).'>'.$new_line;
		}
		else
		{
			$output.= print_r_xml($val,$key,$cycle + 1).$new_line;
		}
	}
	$output.= tabify($cycle - 1).'</'.$wrapper.'>';

	//return the value
	return $output;
}

/* tabify */
function tabify($num_tabs)
{
	for($x = 1; $x <= $num_tabs; $x++) { $return.= "\t"; }
	return $return;
}

Notice that this function is used recursively when a value is an array.

请注意,当值是数组时,将递归使用此函数。

用法 (The Usage)

/* test */
$_POST = array(
				'first_name'=>'David',
				'last_name'=>'Walsh',
				'url'=>'https://davidwalsh.name',
				'languages'=>array('php','javascript','java','css'),
				'title'=>'Web Developer',
				'favorite_blogs'=>array('CSSTricks'=>'http://css-tricks.com','AJAXian'=>'http://ajaxian.com')
			);
echo print_r_xml($_POST);

结果 (The Result)

<?xml version="1.0" encoding="UTF-8">
<data>
	<first_name>David</first_name>
	<last_name>Walsh</last_name>
	<url>https://davidwalsh.name</url>
	<languages>
		<0>php</0>
		<1>javascript</1>
		<2>java</2>
		<3>css</3>
	</languages>
	<title>Web Developer</title>
	<favorite_blogs>
		<CSSTricks>http://css-tricks.com</CSSTricks>
		<AJAXian>http://ajaxian.com</AJAXian>
	</favorite_blogs>
</data>

This function has been a huge help in debugging GET and POST data. Try it out, let me know what you think!

该功能对调试GET和POST数据有很大的帮助。 尝试一下,让我知道您的想法!

翻译自: https://davidwalsh.name/watch-post-save-php-post-data-xml

php post xml

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值