我是PHP新手,所以在我学习的过程中,我发现可以直接转到domain.com/process.php这样的页面,该页面仍然可以运行。
如何防止用户直接访问页面并确保他们在提交表单后访问process.php?
发布于 2012-04-22 10:41:27
只需使用以下代码:
<?php
if(!isset($_POST['submit']))
{
echo 'Try accessing this page by pressing submit button'. '<br />';
echo "<a href='form.html'>Goto Form Page</a>";
exit();
}
?>
// here 'submit' is the name attribute within your input tag for submit button
/*
It simply means if submit button is not pressed, echo the following message and
exit don't run anything else. You can also use die('Your message here')
instead of exit();
*/发布于 2012-04-22 10:34:37
也许你就是在找这个?也许有另一种或更好的方法可以做到这一点,但我不确定。
if(isset($_POST['submit']))
{
///process form
}在大多数情况下,这应该可以工作,尽管在某些情况下可能不会,在这种情况下,您可以添加一个隐藏字段:
<input type='hidden' name='submit' />隐藏字段应始终提交。
发布于 2012-04-22 10:40:25
使用session阻止用户直接访问process.php。与在index.php中使用session_start(); $_SESSION['access']="true";和在process.php页面中一样,
session_start();
<?php
if(isset($_SESSION['access'])){
?>
<form >.....your process form here....
<?php
}else{
echo "Direct access deny";
die;
}即使这可以是自动化的,首先转到索引页面,然后自动处理页面,你可以使用验证码来避免它。
https://stackoverflow.com/questions/10264724
复制相似问题