2010-09-21 6 views
7
var $validate = array(
    'password' => array(
     'passwordlength' => array('rule' => array('between', 8, 50),'message' => 'Enter 8-50 chars'), 
     'passwordequal' => array('checkpasswords','message' => 'Passwords dont match') 
) 
); 

function checkpasswords() 
{ 
    return strcmp($this->data['Airline']['password'],$this->data['Airline']['confirm password']); 
} 

यह कोड काम नहीं कर रहा है और अगर वे मेल खाते हैं तो भी त्रुटि संदेश देता है। साथ ही जब मैं एक संपादन करता हूं तो मुझे फॉलोइंग त्रुटि मिलती है क्योंकि कोई पासवर्ड फ़ील्ड नहीं है। क्या कोई फिक्सकेकेपीपी पासवर्ड सत्यापन

Undefined index: password [APP/models/airline.php, line 25] 
+1

है '$ this-> datadata' इरादा? यदि नहीं, तो आपकी समस्या है। – Stephen

+0

मैंने अतिरिक्त डेटा को निकालने के लिए उपरोक्त कोड को अभी भी तय किया है, मुझे त्रुटि मिलती है – aWebDeveloper

+0

क्या मैं डेटा पोस्ट करने वाले HTML फॉर्म देख सकता हूं? – Stephen

उत्तर

5

यहाँ गलती

'passwordequal' => array('checkpasswords','message' => 'Passwords dont match') 

है मैं

'passwordequal' => array('rule' =>'checkpasswords','message' => 'Passwords dont match') 

भी करने के लिए इसे बदल strcmp समारोह भी गलतियों के रूप में यह 0 (यानी झूठी) वापसी होगी उपरोक्त कोड में हर समय था

if(strcmp($this->data['Airline']['password'],$this->data['Airline']['confirm_password']) ==0) 
{ 
    return true; 
} 
return false; 
+5

ओह, भयानक अनावश्यकता! इस तरह के मामले में आपको 'वापसी strcmp (...) == 0' का उपयोग करना होगा। – deceze

12

क्या आप AuthComponent का उपयोग कर रहे हैं? ध्यान रखें कि इसमें सभी आने वाले पासवर्ड फ़ील्ड हैं (लेकिन "पासवर्ड पुष्टि नहीं" फ़ील्ड, debug($this->data) के साथ जांचें), इसलिए फ़ील्ड कभी भी समान नहीं होंगे। चेक करने के लिए Read the manual and use AuthComponent::password


कहा करने के बाद कि, यहाँ कुछ मैं का उपयोग करें:

public $validate = array(
    'password' => array(
     'confirm' => array(
      'rule' => array('password', 'password_control', 'confirm'), 
      'message' => 'Repeat password', 
      'last' => true 
     ), 
     'length' => array(
      'rule' => array('password', 'password_control', 'length'), 
      'message' => 'At least 6 characters' 
     ) 
    ), 
    'password_control' => array(
     'notempty' => array(
      'rule' => array('notEmpty'), 
      'allowEmpty' => false, 
      'message' => 'Repeat password' 
     ) 
    ) 
); 

public function password($data, $controlField, $test) { 
    if (!isset($this->data[$this->alias][$controlField])) { 
     trigger_error('Password control field not set.'); 
     return false; 
    } 

    $field = key($data); 
    $password = current($data); 
    $controlPassword = $this->data[$this->alias][$controlField]; 

    switch ($test) { 
     case 'confirm' : 
      if ($password !== Security::hash($controlPassword, null, true)) { 
       $this->invalidate($controlField, 'Repeat password'); 
       return false; 
      } 
      return true; 

     case 'length' : 
      return strlen($controlPassword) >= 6; 

     default : 
      trigger_error("Unknown password test '$test'."); 
    } 
} 

ऐसा निम्न कारणों से खराब है:

  • फार्म के लिए तंग युग्मन है, हमेशा के लिए एक क्षेत्र password_control उम्मीद उपस्थित रहें। यदि आपके पास आपके डेटा में कोई नहीं है, तो आपको फील्ड श्वेतसूची का उपयोग करने या सत्यापन अक्षम करने की आवश्यकता है, यानी $this->User->save($this->data, true, array('field1', 'field2'))
  • मैन्युअल रूप से पासवर्ड को एथकंपोनेंट करता है (क्योंकि मॉडल से घटकों तक कोई साफ़ पहुंच नहीं है)। यदि आप AuthComponent में उपयोग किए गए एल्गोरिदम को बदलते हैं, तो आपको इसे यहां भी बदलना होगा।

यह कहकर, यह पारदर्शी रूप से मान्य करता है और नियंत्रक में कोई अतिरिक्त कोड की आवश्यकता के बिना पासवर्ड और पासवर्ड नियंत्रण फ़ील्ड दोनों के लिए उचित त्रुटि संदेश उत्पन्न करता है।

2

प्रमाणीकरण का उपयोग कर केकेपीएचपी 2.x उपयोगकर्ताओं के लिए आप ध्यान दें कि "AuthComponent अब स्वचालित रूप से हर पासवर्ड को प्राप्त नहीं कर सकता है।" अर्थात। उपरोक्त समाधान 2.x के लिए समस्या को हल करने का सही तरीका नहीं हो सकता है। http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#hashing-passwords

1

यहाँ मेरा समाधान है:

आप एक विधि नामित मैच बनाने के लिए करना चाहिए (आप आपको क्या पसंद है यह नाम कर सकते हैं):

public function match($check, $with) { 
    // Getting the keys of the parent field 
    foreach ($check as $k => $v) { 
     $$k = $v; 
    } 

    // Removing blank fields 
    $check = trim($$k); 
    $with = trim($this->data[$this->name][$with]); 

    // If both arent empty we compare and return true or false 
    if (!empty($check) && !empty($with)) { 
     return $check == $with; 
    } 

    // Return false, some fields is empty 
    return false; 
} 

और $ सत्यापित करें विधि इस तरह होना चाहिए:

public $validate = array(
    'password' => array(
     'match' => array(
      'rule' => array('match', 'password2'), 
      'message' => 'Passwords doesnt match', 
     ), 
    ), 
); 

कहाँ password2 क्षेत्र अपनी पहली password क्षेत्र की तुलना करना है

मुझे इसे साझा करने में खुशी है!: डी

3

मान्य पासवर्ड, पुराने पासवर्ड और पुष्टि पासवर्ड के लिए

class Adminpassword extends AppModel 
{ 


    public $name   = 'Admin'; 
      public $primaryKey = 'id'; 
      public $validate = array(
       'oldpassword' => array(
         array(
         'rule' => 'notEmpty', 
         'required' => true, 
         'message' => 'Please Enter Current password' 
         ), 
         array(
         'rule' =>'checkcurrentpasswords', 
         'message' => 'Current Password does not match' 
         ) 
       ), 
       'password' => array(
         array(
           'rule' => 'notEmpty', 
           'required' => true, 
           'message' => 'Please Enter password' 
         ), 
         array(        
         'rule' => array('minLength', 6), 
         'message' => 'Passwords must be at least 6 characters long.', 
         ) 
       ), 
       'cpassword' => array(
         array(
         'rule' => 'notEmpty', 
         'required' => true, 
         'message' => 'Please Enter Confirm password' 
         ), 
         array(
           'rule' => 'checkpasswords', 
           'required' => true, 
           'message' => 'Password & Confirm Password must be match.' 
         ) 
       ) 
      ); 

    function checkpasswords()  // to check pasword and confirm password 
    { 
     if(strcmp($this->data['Adminpassword']['password'],$this->data['Adminpassword']['cpassword']) == 0) 
     { 
      return true; 
     } 
     return false; 
    } 
    function checkcurrentpasswords() // to check current password 
    { 
     $this->id = $this->data['Adminpassword']['id']; 
     $user_data = $this->field('password');  
     //print_r(Security::hash($this->data['Adminpassword']['oldpassword'], 'sha1', true)); 
     if ($user_data == (Security::hash($this->data['Adminpassword']['oldpassword'], 'sha1', true))) 
     { 
      return true; 
     } 
     else 
     { 
     return false; 
     } 
    } 

} 
+0

इस समाधान ने मेरी मदद की और यह स्पष्ट है – daniherculano

संबंधित मुद्दे