简体   繁体   中英

php __DIR__ not executing consistently

This code works perfectly. IF i first have an error in my form - in this case using an email to register, that has already been used. The error message is displayed in register.php. Then if I correct the error, the Include DIR works fine, and the login.php page is displayed. However, if I use an email to register that has not been used (meaning, no errors in my form the first time), it simply ignores the include DIR . '/templates/login.php' and stops. No errors in the debug log, just my logging. The user is added.

The form is a simple post, with one input, the email address, which i use for registering a user. the logging shows the resolved directory correct in both instances, which looks like this C:\\wamp\\www\\bglswp\\wp-content\\plugins\\bgls-players/templates/login.php

The include DIR for the form register.php works consistently.

Been pulling my challenged hairline out for 2 days ;-/. Any help is greatly appreciated.

public function register_player() {

    // rem to secure form,, hidden + nonce

    if ( $_POST ) {
        $errors = array();

        // security validations
        $this-> bgls_validate_post();

        $user_login = ( isset ( $_POST['email'] ) ?  $_POST['email'] : '' );
        $user_email = ( isset ( $_POST['email'] ) ? $_POST['email'] : '' );

        // Validating user data, leaving the duplicative logic until i see email as user works
        if ( empty( $user_login ) ) {
            array_push( $errors, 'Please enter a username.' );
        }    
        if ( empty( $user_email ) ) {
            array_push( $errors, 'Please enter e-mail.' );
        }

        //make sure email is valid email and not previously used
        if ( !empty($user_email) && !is_email( $user_email ) ) {
        array_push( $errors, 'Please enter valid email.') ; }
            elseif ( email_exists( $user_email ) ){
                array_push( $errors, 'User with this email already registered.');
           }   
        // make sure no funny stuff in the email  
        // then see make sure not previously used       
        $strict = 'true';   
        $sanitized_user_login = sanitize_user( $user_login, $strict );

        if ( empty( $sanitized_user_login ) || !validate_username($user_login ) ){
            array_push( $errors, 'Invalid username.' );   
            }
        elseif ( username_exists( $sanitized_user_login ) ) {
            array_push( $errors, 'Username already exists.' );
        }

        // if no errors  generate a password,  and insert the user in the wp db
        if ( empty( $errors ) ) {

            //$user_pass = wp_generate_password();
            $user_pass = 'password1' ;  // test code  remove this

            $user_id = wp_insert_user( array
                    ('user_login' => $sanitized_user_login, 
                    'user_email' => $user_email,
                    'role' => 'player',  // users can only register as players  
                    'user_pass' => $user_pass)     
                    );
                    $this-> log_hra($user_id . 'after wp insert 1');  // added logging

            if ( !$user_id ) {

                array_push( $errors, 'Registration failed.' ); 
            }   else    {

                    $activation_code = 'player';
                    update_user_meta( $user_id, 'activation_code', $activation_code );
                    update_user_meta( $user_id, 'activation_status', 'inactive' );

                    //wp_new_user_notification( $user_id, $user_pass, $activation_code );
                    $success_message = "Registration completed successfully. Please check your email for activation link.";
                    }               

            if ( !is_user_logged_in() ) {
                $this-> log_hra( __DIR__ . '/templates/login.php');  //added logging 
                include __DIR__ . '/templates/login.php';
                exit;
             }
        }  //   /if empty errors    

    }   //  /if post

    // if the user is first registering, present a blank register template
    if ( !is_user_logged_in() ) {
        include __DIR__ . '/templates/register.php';
    exit;
    }
}  // end register_player

You missed the else when there is errors:

        // ...
        }  //   end if empty errors

        // else errors
        else {
            // This is the missing part
            include __DIR__ . '/templates/register.php';
            exit;
        }

    }   //  end if post

    // else no post
    // if the user is first registering, present a blank register template
    if ( !is_user_logged_in() ) {
        include __DIR__ . '/templates/register.php';
        exit;
    }
}  // end register_player

The else is not mandatory as you end the previous if with exit .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM