阻止/阻止直接访问感谢页面

时间:2018-02-02 18:29:36

标签: php wordpress wordpress-theming

如何阻止/阻止直接访问感谢页面,只有在从一个表单(在不同的页面中)中重定向时才能访问?

我应该在下一个链接

中插入此方法

https://wordpress.stackexchange.com/questions/290234/prevent-block-direct-access-to-a-thank-you-page/290237

add_action('template_redirect', function(){
  if ( ! is_page(12345)) {// ID of the thank you page
    return;
  }
  if (wp_get_referer() == 'URL_OF_FORM') {// coming from the form, so all is fine
    return;
  }

  // we are on thank you page
  // visitor is not coming from form
  // so redirect to home
  wp_redirect( get_home_url() );
}

非常感谢!

2 个答案:

答案 0 :(得分:0)

让我们说你的第一页是page1.php并包含以下代码

<form action="page2.php" method="post">
<input type="text" name="blabla">
<input type="submit">
</form>

现在,当用户输入表单值时,将重定向到page2.php,然后在这里使用代码查看用户是否已通过表单或输入链接。你可以通过php中的isset()

来实现这一点
<?php
if(isset($_POST['blabla']))
{
//process your code
}
else
{
echo "Session Expired<br>";
echo "Click <a href='./page1.php'>here</a> to fill the form";
}
?>

答案 1 :(得分:0)

只需在感谢页面上添加以下代码顶部即可。这是最有效的&amp;紧凑的代码,以满足您的要求。

<?php
if(empty($_POST)):
wp_redirect( home_url() ); 
exit;
endif;
?>

注意:wp_redirect()不会自动退出。始终在exit;功能后使用wp_redirect()

相关问题