WordPress登录后重定向

时间:2018-10-11 作者:Metal Zer0

下面的代码允许我根据我创建的自定义表对用户进行身份验证:

 function authentication($emailAdd, $password) {
            include_once(\'././wp-includes/class-phpass.php\');

        global $wpdb, $formErr;

        if(1 > count($formErr->get_error_messages())){
            $hasher = new PasswordHash(8, TRUE);

            $table = $wpdb->prefix.\'finusers\';
            $results=$wpdb->get_row("SELECT * FROM $table WHERE email_address=\'$emailAdd\'");
            $hashed_pwd = $results->password;
            //echo $hashed_pwd;

            if($hasher->CheckPassword($password, $hashed_pwd)){

                //redirection has to happen here

            }else{
                echo "Please check email address of password";
            }
        }
 }
我的问题是如何将用户重定向到特定页面?

1 个回复
SO网友:Krzysiek Dróżdż

我假设在打印任何输出之前调用了您的函数,否则重定向将不起作用。

将用户重定向到给定URL的最简单方法是使用:

wp_redirect( $url );
exit;
因此,您的代码可能如下所示:

function authentication($emailAdd, $password) {
            include_once(\'././wp-includes/class-phpass.php\');

        global $wpdb, $formErr;

        if(1 > count($formErr->get_error_messages())){
            $hasher = new PasswordHash(8, TRUE);

            $table = $wpdb->prefix.\'finusers\';
            $results=$wpdb->get_row("SELECT * FROM $table WHERE email_address=\'$emailAdd\'");
            $hashed_pwd = $results->password;
            //echo $hashed_pwd;

            if($hasher->CheckPassword($password, $hashed_pwd)){

                wp_redirect( $url );  // change to correct URL
                exit;

            }else{
                echo "Please check email address of password";
            }
        }
 }
还有,你的代码有很多错误和问题,尽管。。。我真的不会在任何网站上使用它,我想。。。至少不是当前形式。。。请参见下面代码中的我的注释

function authentication($emailAdd, $password) {
        // using ././ won\'t work. It should be ../../. And even that is risky, because the structure may be changed - you should use ABSPATH instead.
        include_once(\'././wp-includes/class-phpass.php\');

        global $wpdb, $formErr;

        // so if there is exactly one error, everything is fine and user can be logged in? ;)
        if(1 > count($formErr->get_error_messages())){
            $hasher = new PasswordHash(8, TRUE);

            $table = $wpdb->prefix.\'finusers\';
            // Why no escaping? What if $emailAdd will contain special characters to perform SQL injection?
            // Another problem is that you query all rows, why no limit?
            $results=$wpdb->get_row("SELECT * FROM $table WHERE email_address=\'$emailAdd\'");
            // And what if there are no such rows? Line below will cause error
            $hashed_pwd = $results->password;
            //echo $hashed_pwd;

            if($hasher->CheckPassword($password, $hashed_pwd)){

                wp_redirect( $url );  // change to correct URL
                exit;

            }else{
                echo "Please check email address of password";
            }
        }
 }

结束

相关推荐

‘wp_LOGIN’操作挂钩不适用于wp-login.php文件

Wordpress 4.9.5我正在进行演示,以检查登录后调用的一些操作或函数,所以我准备了一个插件。这是代码。<?php /* Plugin Name: Test Login Plugin URI: http://wordpress.org/plugins/ Description: This is not just a plugin, Version: 1.0 */ function your_function() {&