Re-fill posted form data via PHP securely

£可爱£侵袭症+ 提交于 2019-12-24 02:14:00

问题


How can I ref-fill posted form data via PHP in the event of an error. I have a contact form and the user enters a bunch of information and if one field doesn't validate, he loses everything. How can I stop this from happening and make sure it is secure?

I've tried this, but believe I read somewhere it is not secure:

<input type="text" name="name" value="<?php echo $_POST['name']; ?>" />

回答1:


One issue is that if $_POST['name'] contains a ", then the value can 'escape' out and rewrite the page content.

Another is that with strict error checking switched on, accessing a non-existent array index will throw a Notice error.

Here is one safe way of re-filling the form data:

<input type="text" name="name" value="<?php echo isset($_POST['name']) ? htmlspecialchars($_POST['name']) : ''; ?>" />

I would personally recommend handling form display and validation through a framework, like Zend_Form within the Zend Framework. (You won't have to change everything else across just to use the form stuff) It makes writing safe and readable code much easier.



来源:https://stackoverflow.com/questions/8045295/re-fill-posted-form-data-via-php-securely

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!