用于条件重定向的插件

时间:2012-10-15 作者:Arun

比如说,我在wordpress中创建了page1和page2,在page1中有一个超链接可以将我带到page2。如何通过在地址栏中键入page2 url,确保访问者点击了该超链接并访问了page2,而不是直接访问page2。

如果有人直接来到第2页,我希望他们自动重定向到第1页。在过去,我能够用一些javascript和后来的ASP来破解这个问题。如果我没记错的话,我使用了querystring和url重定向。

wordpress中的简单方法是什么?不幸的是,在wordpress插件目录中,我得到的只是404和301的插件,请在这里帮助我。

2 个回复
最合适的回答,由SO网友:kaiser 整理而成
if ( ! isset( $_SERVER[\'HTTP_REFERER\'] ) AND \'your_page1\' !== $_SERVER[\'HTTP_REFERER\'] )
{
    exit( wp_redirect( $target, 301 ) );
}
SO网友:Adam

As a complimentary addition to Kaiser\'s answer you can secure things further by using a nonce value. (numbers used once)

Step 1 - create your link with nonce value

Page A (the referring page where your users are coming from)

<?php
    $url = "http://example.com/destination-slug/";
    $nonce_url = wp_nonce_url( $url, "secure_this_page_nonce");
    echo "<a href=\'".$nonce_url."\'>Click me!</a>";
?>

This will create a link like,

http://example.com/destination-slug/?_wpnonce=8c9xa6e33e

Step 2 - verify the nonce value passed from link

Page B (where you users are going once they click the above link)

<?php
if (!wp_verify_nonce($_GET[\'_wpnonce\'],\'secure_this_page_nonce\')) {

   //optionally redirect users back to the page you want them to come from
   $previous_page = \'http://example.com/previous-page/\';
   wp_redirect( $previous_page, 301)
   exit;

} else {

  //continue with normal processing of page

}
?>

This checks for the existence of a nonce via the $_GET parameter _wpnonce and whether the action/name secure_this_page_nonce is set which equates to this portion of the URL as shown in Step 1 ?_wpnonce=8c9xa6e33e.

If the nonce value is not present and fails its conditional check then you can redirect the user back to the previous page.

If it passes the conditional check then the user has originated from clicking our link on Page A, in which case we render the page as normal.

Good luck!

结束

相关推荐

需要将wp_reDirect放在哪里才能使其工作?

我在mysite/post\\u id/edit上有一个前端帖子编辑页面,正在尝试将非作者的用户重定向回帖子。以下是我正在使用的代码: <?php global $current_user; get_currentuserinfo(); if($post->post_author != $current_user->ID): wp_redirect( the_permalink() ); exit; ?>