Controlla il nome utente corretto nel modulo di accesso personalizzato
-
-
Infretta,`username_exists ()`può aiutartiin qualchemodo?In a rush, can `username_exists()` help you somehow?
- 0
- 2011-04-13
- Ashfame
-
3 risposta
- voti
-
- 2011-04-13
Il codice che hapubblicatoin queltutorial (molto carino aproposito) èilmodulo delmodulo "reimpostapassword"integrato che reindirizza a login.phpin caso dierrore,mapuoi cambiarloe crearne unotuobasato sul originalee aggiungilo allapagina delmodello,modifica:
<form method="post" action="<?php echo site_url('wp-login.php?action=lostpassword', 'login_post') ?>" class="wp-user-form"> <div class="username"> <label for="user_login" class="hide"><?php _e('Username or Email'); ?>: </label> <input type="text" name="user_login" value="" size="20" id="user_login" tabindex="1001" /> </div> <div class="login_fields"> <?php do_action('login_form', 'resetpass'); ?> <input type="submit" name="user-submit" value="<?php _e('Reset my password'); ?>" class="user-submit" tabindex="1002" /> <?php $reset = $_GET['reset']; if($reset == true) { echo '<p>A message will be sent to your email address.</p>'; } ?> <input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>?reset=true" /> <input type="hidden" name="user-cookie" value="1" /> </div> </form>
a:
<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>" class="wp-user-form"> <div class="username"> <label for="user_login" class="hide"><?php _e('Username or Email'); ?>: </label> <input type="text" name="user_login" value="" size="20" id="user_login" tabindex="1001" /> </div> <div class="login_fields"> <?php do_action('login_form', 'resetpass'); ?> <input type="submit" name="user-submit" value="<?php _e('Reset my password'); ?>" class="user-submit" tabindex="1002" /> <?php if (isset($_POST['reset_pass'])) { global $wpdb; $username = trim($_POST['user_login']); $user_exists = false; if (username_exists($username)) { $user_exists = true; $user_data = get_userdatabylogin($username); } elseif (email_exists($username)) { $user_exists = true; $user = get_user_by_email($username); } else { $error[] = '<p>' . __('Username or Email was not found, try again!') . '</p>'; } if ($user_exists) { $user_login = $user->user_login; $user_email = $user->user_email; // Generate something random for a password... md5'ing current time with a rand salt $key = substr(md5(uniqid(microtime())), 0, 8); // Now insert the new pass md5'd into the db $wpdb->query("UPDATE $wpdb->users SET user_activation_key = '$key' WHERE user_login = '$user_login'"); //create email message $message = __('Someone has asked to reset the password for the following site and username.') . "\r\n\r\n"; $message .= get_option('siteurl') . "\r\n\r\n"; $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n"; $message .= __('To reset your password visit the following address, otherwise just ignore this email and nothing will happen.') . "\r\n\r\n"; $message .= get_option('siteurl') . "/wp-login.php?action=rp&key=$key\r\n"; //send email meassage if (FALSE == wp_mail($user_email, sprintf(__('[%s] Password Reset'), get_option('blogname')), $message)) $error[] = '<p>' . __('The e-mail could not be sent.') . "<br />\n" . __('Possible reason: your host may have disabled the mail() function...') . '</p>'; } if (count($error) > 0) { foreach ($error as $e) { echo $e . '<br/>'; } } else { echo '<p>' . __('A message will be sent to your email address.') . '</p>'; } } ?> <input type="hidden" name="reset_pass" value="1" /> <input type="hidden" name="user-cookie" value="1" /> </div> </form>
The code he posted in that tutorial (very nice BTW) post's the form to the build-in "reset password" module which redirects to the login.php on error, but you can change that and build your own based on the original and add it to the template page, change:
<form method="post" action="<?php echo site_url('wp-login.php?action=lostpassword', 'login_post') ?>" class="wp-user-form"> <div class="username"> <label for="user_login" class="hide"><?php _e('Username or Email'); ?>: </label> <input type="text" name="user_login" value="" size="20" id="user_login" tabindex="1001" /> </div> <div class="login_fields"> <?php do_action('login_form', 'resetpass'); ?> <input type="submit" name="user-submit" value="<?php _e('Reset my password'); ?>" class="user-submit" tabindex="1002" /> <?php $reset = $_GET['reset']; if($reset == true) { echo '<p>A message will be sent to your email address.</p>'; } ?> <input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>?reset=true" /> <input type="hidden" name="user-cookie" value="1" /> </div> </form>
to:
<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>" class="wp-user-form"> <div class="username"> <label for="user_login" class="hide"><?php _e('Username or Email'); ?>: </label> <input type="text" name="user_login" value="" size="20" id="user_login" tabindex="1001" /> </div> <div class="login_fields"> <?php do_action('login_form', 'resetpass'); ?> <input type="submit" name="user-submit" value="<?php _e('Reset my password'); ?>" class="user-submit" tabindex="1002" /> <?php if (isset($_POST['reset_pass'])) { global $wpdb; $username = trim($_POST['user_login']); $user_exists = false; if (username_exists($username)) { $user_exists = true; $user_data = get_userdatabylogin($username); } elseif (email_exists($username)) { $user_exists = true; $user = get_user_by_email($username); } else { $error[] = '<p>' . __('Username or Email was not found, try again!') . '</p>'; } if ($user_exists) { $user_login = $user->user_login; $user_email = $user->user_email; // Generate something random for a password... md5'ing current time with a rand salt $key = substr(md5(uniqid(microtime())), 0, 8); // Now insert the new pass md5'd into the db $wpdb->query("UPDATE $wpdb->users SET user_activation_key = '$key' WHERE user_login = '$user_login'"); //create email message $message = __('Someone has asked to reset the password for the following site and username.') . "\r\n\r\n"; $message .= get_option('siteurl') . "\r\n\r\n"; $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n"; $message .= __('To reset your password visit the following address, otherwise just ignore this email and nothing will happen.') . "\r\n\r\n"; $message .= get_option('siteurl') . "/wp-login.php?action=rp&key=$key\r\n"; //send email meassage if (FALSE == wp_mail($user_email, sprintf(__('[%s] Password Reset'), get_option('blogname')), $message)) $error[] = '<p>' . __('The e-mail could not be sent.') . "<br />\n" . __('Possible reason: your host may have disabled the mail() function...') . '</p>'; } if (count($error) > 0) { foreach ($error as $e) { echo $e . '<br/>'; } } else { echo '<p>' . __('A message will be sent to your email address.') . '</p>'; } } ?> <input type="hidden" name="reset_pass" value="1" /> <input type="hidden" name="user-cookie" value="1" /> </div> </form>
-
sembrafantastico,ti farò sapere sefunziona.looks great, I'll let you know if it works.
- 0
- 2011-04-14
- Pippin
-
Ok,hofunzionato allagrande,conpochemodifiche.C'erano unpaio dierrori di sintassie ilgeneratore di chiavimd5nonfunzionava,quindi hopreso quello da wp-login.php.Ho solo unproblema ora.Quando qualcunofa clic sull'URLnell'e-mailper creare unanuovapassword,viene reindirizzato almodulopredefinito,quindi ora devo creare unmodulo ancheper quello.Ok, I've gotten this to work great, with just a few changes. There were a couple of syntax errors and the md5 key generator didn't work, so I took the one from wp-login.php. I just have one problem now. When someone clicks the url in the email to create a new password, they're redirected to the default form, so now I need to create a form for that too.
- 1
- 2011-04-15
- Pippin
-
@pippin:il codice delgeneratore di chiavimd5 qui è quelloper wp-login.php,e per quanto riguardail reindirizzamentoprova ad aggiungere `& redirect_to=$ _ SERVER ['REQUEST_URI']` al linknell'email cheinvii.@pippin: the the md5 key generator code here is the one for wp-login.php, and as for the redirection try adding `&redirect_to=$_SERVER['REQUEST_URI']` to the link in the email you send.
- 0
- 2011-04-15
- Bainternet
-
Ilmio collegamento ora assomiglia a questo `$messaggio.=Get_option ('siteurl')."/wp-login.php?action=rp&key=$key&login=$user_login&redirect_to=$ _ SERVER ['REQUEST_URI'] \ r \n"; `,ma la cosa strana è che quando aggiungo & redirect,ilmessaggionon viene ricevutoinviato ...Inoltre,non dovrò creare unnuovomoduloper consentire all'utente diinserire lanuovapassworde simili?My link now looks like this `$message .= get_option('siteurl') . "/wp-login.php?action=rp&key=$key&login=$user_login&redirect_to=$_SERVER['REQUEST_URI']\r\n";`, but what's weird is that when I add the &redirect, the message doesn't get sent . . . Also, won't I need to make a new form for the user to enter their new password and such in?
- 0
- 2011-04-15
- Pippin
-
Sì,me neero dimenticato,dovresti creareiltuomoduloper reimpostare lapassworde probabilmente sostituireil link che vieneinviato all'utente all'URL corrente,e inbase alla chiave controlla se è l'utentee mostragliilmodulo.Yeah i forgot about that, you would need to create your own form to reset the password and probably replace the link that is sent to the user to the current url, and based on the key check if its the user and show him the form.
- 0
- 2011-04-15
- Bainternet
-
riga 17 `get_user_by ('login',$ user_login);`poiché `get_userdatabylogin` è stato deprecato ...line 17 `get_user_by('login', $user_login);` as the `get_userdatabylogin` has been deprecated...
- 0
- 2012-05-15
- Val
-
Mi rendo conto che questa risposta èpiuttosto vecchia,sono unpo 'insicuro sulla sua affidabilità.Anche seimmagino che dovrebbefunzionare,preferireinon scherzare con SQLpoichépotrebbe cambiarein futuro.D'altraparte,non riesco atrovare dettagli sulle alternative.I realize this answer is quite old, I'm a bit unsure about its reliability. While I guess it should work, I'd rather not mess with SQL since it may change in the future. On the other hand, I can't find details on alternatives.
- 0
- 2013-02-25
- Christian
-
"non scherzare con SQL"?non èpiù copiato dalfile core" not mess with SQL " ? its copied from the core file nothing more
- 0
- 2013-02-25
- Bainternet
-
- 2012-12-07
Ecco una versione aggiornata del codice da @bainternet conglierrori di sintassi corretti,il suggerimento di @Vale ilgeneratore di chiavi da wp-login.php 3.4.2:
global $wpdb; $username = trim($_POST['user_login']); $user_exists = false; // First check by username if ( username_exists( $username ) ){ $user_exists = true; $user = get_user_by('login', $username); } // Then, by e-mail address elseif( email_exists($username) ){ $user_exists = true; $user = get_user_by_email($username); }else{ $error[] = '<p>'.__('Username or Email was not found, try again!').'</p>'; } if ($user_exists){ $user_login = $user->user_login; $user_email = $user->user_email; $key = $wpdb->get_var($wpdb->prepare("SELECT user_activation_key FROM $wpdb->users WHERE user_login = %s", $user_login)); if ( empty($key) ) { // Generate something random for a key... $key = wp_generate_password(20, false); do_action('retrieve_password_key', $user_login, $key); // Now insert the new md5 key into the db $wpdb->update($wpdb->users, array('user_activation_key' => $key), array('user_login' => $user_login)); } //create email message $message = __('Someone has asked to reset the password for the following site and username.') . "\r\n\r\n"; $message .= get_option('siteurl') . "\r\n\r\n"; $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n"; $message .= __('To reset your password visit the following address, otherwise just ignore this email and nothing will happen.') . "\r\n\r\n"; $message .= network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login') . "&redirect_to=".urlencode(get_option('siteurl'))."\r\n"; //send email meassage if (FALSE == wp_mail($user_email, sprintf(__('[%s] Password Reset'), get_option('blogname')), $message)) $error[] = '<p>' . __('The e-mail could not be sent.') . "<br />\n" . __('Possible reason: your host may have disabled the mail() function...') . '</p>'; } if (count($error) > 0 ){ foreach($error as $e){ echo $e . "<br/>"; } }else{ echo '<p>'.__('A message will be sent to your email address.').'</p>'; }
Here is an updated version of the code from @bainternet with the syntax errors corrected, the suggestion by @Val and the key generator from wp-login.php 3.4.2:
global $wpdb; $username = trim($_POST['user_login']); $user_exists = false; // First check by username if ( username_exists( $username ) ){ $user_exists = true; $user = get_user_by('login', $username); } // Then, by e-mail address elseif( email_exists($username) ){ $user_exists = true; $user = get_user_by_email($username); }else{ $error[] = '<p>'.__('Username or Email was not found, try again!').'</p>'; } if ($user_exists){ $user_login = $user->user_login; $user_email = $user->user_email; $key = $wpdb->get_var($wpdb->prepare("SELECT user_activation_key FROM $wpdb->users WHERE user_login = %s", $user_login)); if ( empty($key) ) { // Generate something random for a key... $key = wp_generate_password(20, false); do_action('retrieve_password_key', $user_login, $key); // Now insert the new md5 key into the db $wpdb->update($wpdb->users, array('user_activation_key' => $key), array('user_login' => $user_login)); } //create email message $message = __('Someone has asked to reset the password for the following site and username.') . "\r\n\r\n"; $message .= get_option('siteurl') . "\r\n\r\n"; $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n"; $message .= __('To reset your password visit the following address, otherwise just ignore this email and nothing will happen.') . "\r\n\r\n"; $message .= network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login') . "&redirect_to=".urlencode(get_option('siteurl'))."\r\n"; //send email meassage if (FALSE == wp_mail($user_email, sprintf(__('[%s] Password Reset'), get_option('blogname')), $message)) $error[] = '<p>' . __('The e-mail could not be sent.') . "<br />\n" . __('Possible reason: your host may have disabled the mail() function...') . '</p>'; } if (count($error) > 0 ){ foreach($error as $e){ echo $e . "<br/>"; } }else{ echo '<p>'.__('A message will be sent to your email address.').'</p>'; }
-
- 2015-11-13
Stavo ancora riscontrandoproblemi con la chiave di ripristino chenonfunzionava correttamente,il collegamentonell'emailmi reindirizzava allapagina di reimpostazione dellapassword standard conilparametro URL cheindica unproblema con la chiave,quindi ho seguitopiù da vicinoil wp-login .phpe includeva l'oggetto $ wp_hasher,questo ha risoltoilproblemae la reimpostazione dellapasswordnell'email orafunziona
if (($_SERVER['REQUEST_METHOD'] === (string) 'POST') && (isset($_POST['reset_pass']))) { // Acccess global properties global $wpdb, $wp_hasher; // Variables $error_pass_reset = array(); $username = (string) trim($_POST['user_login']); $user_exists = (bool) false; // ---- USERNAME OR EMAIL EXISTS ---- // if (username_exists($username)) { $user_exists = (bool) true; $user = (object) get_user_by('login', $username); } // end if else if (email_exists($username)) { $user_exists = (bool) true; $user = (object) get_user_by('email', $username); } // end else if else { $error_pass_reset[] = '<p>Username or Email was not found, please try again.</p>'; } // end else // ---- USER EXISTS ---- // if ($user_exists === (bool) true) { // Variables $user_login = (string) $user -> user_login; $user_email = (string) $user -> user_email; // Generate password reset key if (empty($key)) { $key = (string) wp_generate_password(20, false); do_action('retrieve_password_key', $user_login, $key); // Create the $wp_hasher object if (empty($wp_hasher)) { require_once(ABSPATH . WPINC . '/class-phpass.php'); $wp_hasher = new PasswordHash(8, true); } // Reset key with hasher applied (MD5 has string output) $hashed = (string) time() . ':' . $wp_hasher -> HashPassword($key); // Insert the new key into the database $wpdb -> update( $wpdb -> users, array( 'user_activation_key' => $hashed ), array( 'user_login' => $user_login ) ); } // end if // Email message $message = (string) 'Someone requested that the password be reset for the following account:' . "\r\n\r\n" . get_option('siteurl') . "\r\n\r\n" . 'Username: ' . $user_login . "\r\n\r\n" . 'If this was a mistake, just ignore this email and nothing will happen.' . "\r\n\r\n" . 'To reset your password, visit the following address:' . "\r\n\r\n" . get_option('siteurl') . '/wp-login.php?action=rp&key=' . $key . '&login=' . $user_login . "\r\n"; // Send email if ((bool) false === wp_mail($user_email, get_option('blogname') . ' Password Reset', $message)) { $error_pass_reset[] = '<p>The e-mail could not be sent at this time.</p>' . "\n"; } // end if } // end if // Send the rest password email do_action('login_form', 'resetpass'); } // end if (($_SERVER['REQUEST_METHOD'] === (string) 'POST') && (isset($_POST['reset_pass'])))
I was still encountering issues with the reset key not working properly, the link in the email would redirect me to the standard password reset page with the URL parameter indicating a problem with the key, so I more closely followed the wp-login.php file and included the $wp_hasher object, this fixed the problem and the password reset in the email now works
if (($_SERVER['REQUEST_METHOD'] === (string) 'POST') && (isset($_POST['reset_pass']))) { // Acccess global properties global $wpdb, $wp_hasher; // Variables $error_pass_reset = array(); $username = (string) trim($_POST['user_login']); $user_exists = (bool) false; // ---- USERNAME OR EMAIL EXISTS ---- // if (username_exists($username)) { $user_exists = (bool) true; $user = (object) get_user_by('login', $username); } // end if else if (email_exists($username)) { $user_exists = (bool) true; $user = (object) get_user_by('email', $username); } // end else if else { $error_pass_reset[] = '<p>Username or Email was not found, please try again.</p>'; } // end else // ---- USER EXISTS ---- // if ($user_exists === (bool) true) { // Variables $user_login = (string) $user -> user_login; $user_email = (string) $user -> user_email; // Generate password reset key if (empty($key)) { $key = (string) wp_generate_password(20, false); do_action('retrieve_password_key', $user_login, $key); // Create the $wp_hasher object if (empty($wp_hasher)) { require_once(ABSPATH . WPINC . '/class-phpass.php'); $wp_hasher = new PasswordHash(8, true); } // Reset key with hasher applied (MD5 has string output) $hashed = (string) time() . ':' . $wp_hasher -> HashPassword($key); // Insert the new key into the database $wpdb -> update( $wpdb -> users, array( 'user_activation_key' => $hashed ), array( 'user_login' => $user_login ) ); } // end if // Email message $message = (string) 'Someone requested that the password be reset for the following account:' . "\r\n\r\n" . get_option('siteurl') . "\r\n\r\n" . 'Username: ' . $user_login . "\r\n\r\n" . 'If this was a mistake, just ignore this email and nothing will happen.' . "\r\n\r\n" . 'To reset your password, visit the following address:' . "\r\n\r\n" . get_option('siteurl') . '/wp-login.php?action=rp&key=' . $key . '&login=' . $user_login . "\r\n"; // Send email if ((bool) false === wp_mail($user_email, get_option('blogname') . ' Password Reset', $message)) { $error_pass_reset[] = '<p>The e-mail could not be sent at this time.</p>' . "\n"; } // end if } // end if // Send the rest password email do_action('login_form', 'resetpass'); } // end if (($_SERVER['REQUEST_METHOD'] === (string) 'POST') && (isset($_POST['reset_pass'])))
-
Questa risposta conil codice wp_hashermi è stataestremamente utilepoiché ho creato unmodello dipassword dimenticatapersonalizzato.This answer with the wp_hasher code was extremely useful to me as I have created a custom forgotten password template.
- 0
- 2017-01-31
- Neelam Khan
Ho utilizzatoiltutorial di Jeff Starper creareilmiomodulo di accessopersonalizzato http://digwp.com/2010/12/login-register-password-code/.Funzionabenissimo,ma ho unproblema.Nelmodulo di reimpostazione dellapassword,se qualcunoinserisceilproprionome utentein modoerrato (inmodo chenon venga verificato),viene respinto al wp-login.php? Action=lostpasswordpredefinito conilmessaggio dierrore.
Esiste unmodoper reindirizzare allamiapagina dierrore?
Grazie!