首页 > 解决方案 > 错误 PHP 通知:未定义变量:第 57 行 /opt/bitnami/apps/wordpress/htdocs/wp-content/plugins/Lost-password/lost-passwords.php 中的 _SESSION

问题描述

我对 php 非常陌生,并且是那种如何遵循教程并尽力做到最好的人,但是我在 WooCommerce 上的自定义重置密码表单上遇到了一些麻烦......

我已经按照@Polar在另一个页面答案上的 WooCommerce 丢失密码表单上给出的所有步骤进行操作。但我无法获得重置密码表单,当我激活调试模式时,出现以下错误:

“PHP 通知:未定义变量:…/wp-content/plugins/Lost-password/lost-passwords.php 第 54 行中的 _SESSION”

这是在第 54 行写的:

unset( $_SESSION[ "csx-show-reset-form" ] );

我可以输入用户电子邮件并实际收到重置密码的电子邮件,但是当我点击链接时,它再次显示“插入您的电子邮件”页面。

这是我正在使用的代码:

<?php

/*
Plugin Name:  Lost Password
Version: 1.0
Description: Personalized Shortcode for recovering password.
Text Domain: FA-Lost-Password
*/

/**
* Crea el shortcode [lost_password_form] para el formulario de los password
*/
 
function wc_custom_lost_password_form( $atts ) {
    if ( !empty( $_COOKIE[ "csx-reset-link-set" ] ) && isset( $_COOKIE[ "csx-reset-link-set" ] ) && $_COOKIE[ "csx-reset-link-set" ] === "true" ) { // WPCS: input var ok, CSRF ok.
        return wc_get_template( 'myaccount/lost-password-confirmation.php' );
    } elseif ( !empty( $_SESSION[ "csx-show-reset-form" ] ) && isset( $_SESSION[ "csx-show-reset-form" ] ) && $_SESSION[ "csx-show-reset-form" ] === "true" ) {
        $rp_id = $_SESSION[ "csx-id" ];
        $rp_key = $_SESSION[ "csx-key" ];
        if ( isset( $_COOKIE[ 'wp-resetpass-' . COOKIEHASH ] ) && 0 < strpos( $_COOKIE[ 'wp-resetpass-' . COOKIEHASH ], ':' ) ) { // @codingStandardsIgnoreLine
            list( $rp_id, $rp_key ) = array_map( 'wc_clean', explode( ':', wp_unslash( $_COOKIE[ 'wp-resetpass-' . COOKIEHASH ] ), 2 ) ); // @codingStandardsIgnoreLine
            $userdata = get_userdata( absint( $rp_id ) );
            $rp_login = $userdata ? $userdata->user_login : '';
            $user = WC_Shortcode_My_Account::check_password_reset_key( $rp_key, $rp_login );

            // Reset key / login is correct, display reset password form with hidden key / login values.
            if ( is_object( $user ) ) {
                return wc_get_template(
                    'myaccount/form-reset-password.php',
                    array(
                        'key' => $rp_key,
                        'login' => $rp_login,
                    )
                );
            }
        }
    }

    // Show lost password form by default.
    return wc_get_template(
        'myaccount/form-lost-password.php',
        array(
            'form' => 'lost_password',
        )
    );
}
add_shortcode( 'lost_password_form', 'wc_custom_lost_password_form' );

//Handling query
function csx_process_query() {

    if ( isset( $_GET[ 'reset-link-sent' ] ) && $_GET[ 'reset-link-sent' ] === "true" ) {
        setcookie( 'csx-reset-link-set', "true", time() + ( 300 * 1 ), "/" ); //Allow to submit email for reset after 5 minutes only.
        unset( $_SESSION[ "csx-show-reset-form" ] );
    }

    if ( isset( $_GET[ 'show-reset-form' ] ) && $_GET[ 'show-reset-form' ] === "true" ||
        isset( $_GET[ 'key' ] ) && isset( $_GET[ 'id' ] ) ) {
        $_SESSION[ "csx-show-reset-form" ] = "true";
        setcookie( 'csx-reset-link-set', "", time() - 3600, "/" );
    }

    //Set session and cookie if key and id are existed
    if ( isset( $_GET[ 'key' ] ) && isset( $_GET[ 'id' ] ) ) {
        $_SESSION[ "csx-key" ] = $_GET[ 'key' ];
        $_SESSION[ "csx-id" ] = $_GET[ 'id' ];

        $value = sprintf( "%s:%s", wp_unslash( $_GET[ 'id' ] ), wp_unslash( $_GET[ 'key' ] ) );
        WC_Shortcode_My_Account::set_reset_password_cookie( $value );
    }

    //Unset session and cookie after successfully changed the password.
    if ( isset( $_GET[ 'new-password-created' ] ) && $_GET[ 'new-password-created' ] === "true" ) {
        setcookie( 'wp-resetpass-' . COOKIEHASH, "", time() - 3600 );
        unset( $_SESSION[ "csx-show-reset-form" ] );
        unset( $_SESSION[ "csx-reset-link-set" ] );
        unset( $_SESSION[ "csx-id" ] );
        unset( $_SESSION[ "csx-key" ] );
    }
}
add_action( 'init', 'csx_process_query' );

//Redirect to custom lost password on request
function csx_redirections() {
    if ( isset( $_GET[ 'reset-link-sent' ] ) || isset( $_GET[ 'show-reset-form' ] ) ||
        isset( $_GET[ 'key' ] ) && isset( $_GET[ 'id' ] ) ) {
        wp_redirect( home_url() . '/password' );
        exit;
    }
}
add_action( 'template_redirect', 'csx_redirections' );

/**
*  Redirecccionar al usuario cuando la contraseña fue cambiada exitosamente
*/

function woocommerce_new_pass_redirect( $user ) {
    wc_add_notice( __( '¡Su Contraseña ha sido actualizada correctamente!', 'woocommerce' ), 'success' );
    wp_redirect( home_url() . "/mi-cuenta/?new-password-created=true" );
    exit;
}
add_action( 'woocommerce_customer_reset_password', 'woocommerce_new_pass_redirect' );

我会感谢你能给我的任何帮助。

谢谢

标签: phpwoocommercecustom-wordpress-pages

解决方案


推荐阅读