我有一个简单的表单,想要验证发布的值是来自该表单的目录,而不是来自外部源。
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="POST">
<input type="text" name="post" id="post" />
<input type="submit" name="submit" id="submit" />
</form>
我是否需要在会话中存储内容?一个简单的例子非常有帮助。感谢。
答案 0 :(得分:3)
创建表单时,您可以使用:
<?php
session_start(); // don't forget that you need to call before output (place first, or use ob_start()
$_SESSION['formhash'] = md5(date('Y-m-d H:i:s').'2fiaSFI#T8ahugi83okkj');
?>
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="POST">
<input type="text" name="post" id="post" />
<input type="hidden" name="hash" id="hash" value="<?php echo $_SESSION['formhash']; ?>" />
<input type="submit" name="submit" id="submit" />
</form>
您需要检查某人是否发布了帖子请求具有正确的哈希值。你可以使用:
<?php
session_start(); // don't forget that you need to call before output (place first, or use ob_start()
if (isset($_SESSION['formhash']) && isset($_POST['hash']) && $_SESSION['formhash']==$_POST['hash']) {
// treat $_POST
}
?>