2010-06-08 7 views
6

मैं एक सुरक्षित निर्देशिका जहां केवल .htpasswd पर उपयोगकर्ता का उपयोग कर सकते है, लेकिन कभी कभी यह उपयोगकर्ता की आवश्यकता है पासवर्ड या उपयोगकर्ता नाम बदलने के लिए, अपने उपयोगकर्ता नाम उसे स्वयंphp का उपयोग कर .htpasswd को संपादित करने के लिए कैसे?

sample users 
kevien : kka 
mike : mike 

के लिए एक विशिष्ट उपयोगकर्ता नाम पासवर्ड को संपादित और कहते हैं कि मैं चाहता हूँ जाने XYZ

को kevien बदलने के लिए और एक ही बात पासवर्ड को

उत्तर

4

इस ऑप्टिकल फाइबर केबल सिर्फ एक नमूना है, जो आपके मौजूदा फ़ाइल को पढ़ने जाएगा, दिए गए उपयोगकर्ता नाम खोजने के लिए और बदलने के लिए या तो यह उपयोगकर्ता नाम का पासवर्ड है।

कृपया ध्यान रखें कि यह कोड सुरक्षित नहीं है और आपको अभी भी उपयोगकर्ता नाम और पासवर्ड का विश्लेषण करने की आवश्यकता होगी ताकि यह आपकी फ़ाइल को तोड़ न सके।

$username = $_POST['user']; 
    $password = $_POST['pass']; 
    $new_username = $_POST['newuser']; 
    $new_password = $_POST['newpass']; 
    $action = $_POST['action']; 
    //read the file into an array 
    $lines = explode("\n", file_get_contents('.htpasswd')); 

    //read the array and change the data if found 
    $new_file = ""; 
    foreach($lines as $line) 
    { 
     $line = preg_replace('/\s+/','',$line); // remove spaces 
     if ($line) { 
      list($user, $pass) = split(":", $line, 2); 
      if ($user == $username) { 
       if ($action == "password") { 
        $new_file .= $user.':'.$new_password."\n"; 
       } else { 
        $new_file .= $new_username.':'.$pass."\n"; 
       } 
      } else { 
       $new_file .= $user.':'.$pass."\n"; 
      } 
     } 
    } 

    //save the information 
    $f=fopen(".htpasswd","w") or die("couldn't open the file"); 
    fwrite($f,$new_file); 
    fclose($f); 
+0

अधिभावी के लिए संभव है?क्या यह नहीं कहता है "अगर फ़ाइल में संग्रहीत उपयोगकर्ता इनपुट द्वारा दिए गए उपयोगकर्ता से मेल नहीं खाता है तो फ़ाइल में उपयोगकर्ता को पासवर्ड जोड़ने के बजाय"? – Qullbrune

+0

यदि उपयोगकर्ता पहले से ही फ़ाइल में से किसी के साथ मेल नहीं खाता है तो यह कुछ भी नहीं बदलेगा और यह शीर्ष पर आता है 'ओएनसी यह सिर्फ एक नमूना है' इसका मतलब है अनचाहे, लेकिन तर्क वहां है और आप हमेशा टेस्ट फाइल बना सकते हैं और परीक्षण कर सकते हैं स्व। – Prix

4

न चला जाता है। इसके बजाय डेटाबेस में अपने authdb को स्टोर करें mod_auth_mysql।

+0

वेज़क्वेज़-अब्राम आप पर किसी भी ट्यूटोरियल है कि कैसे लागू करने के लिए यह – Mahmoud

3

Googled "php htpasswd उत्पन्न", इस लेख मिला: How to create a password for a .htpasswd file using PHP

$password = crypt($clearTextPassword, base64_encode($clearTextPassword)); 

तो मुझे लगता है आप, file_get_contents साथ फ़ाइल की सामग्री में पढ़ा एक साहचर्य सरणी में उसे पार्स प्रासंगिक प्रविष्टियों (संशोधित पासवर्ड को एन्क्रिप्ट चाहते हैं जैसा कि ऊपर दिखाया:

कुंजी लाइन प्रतीत हो रहा है), सरणी को स्ट्रिंग में वापस लिखें, और फ़ाइल को वापस लिखने के लिए file_put_contents का उपयोग करें।

यह सबसे निश्चित रूप से नहीं मानक अभ्यास है, तथापि है। डेटाबेस के लिए नौकरी की तरह लगता है। यदि आप एक संपूर्ण डेटाबेस सर्वर स्थापित करने के बारे में अजीब महसूस करते हैं, और आपका होस्ट इसका समर्थन करता है, तो SQLite एक अच्छा विकल्प हो सकता है।

+0

मैं एक पूरी तरह कार्यात्मक वर्ग पाया, लेकिन इसके साथ समस्या यह है कि यदि उपयोगकर्ता मौजूद इस पर यह लिखना नहीं है, यह एक और लाइन, वहाँ मैं अच्छी तरह से एक ही उपयोगकर्ता लेकिन अनेक पासवर्ड 'http हो रहा जोड़ता है: // www .weberdev.com/get_example-4178 .html' – Mahmoud

+0

@ महमूद - कितना दिलचस्प है। मैं आपकी जरूरतों को पूरा करने के लिए या अपने स्वयं के समाधान लिखने के लिए कोड को संशोधित करने में भाग्य की कामना करता हूं। – Matchu

+0

मैं केवल वहाँ विचार उपयोग कर रहा हूँ, नहीं कोड में ही, मैं एक समाधान बनाना चाहते हैं, लेकिन मैं पासवर्ड एन्कोडिंग के बाद अटक कर रहा हूँ, यह खोज और इस कोड को वास्तव में सही काम करता है पाठ – Mahmoud

6

मैं तहखाने alghoritms के सभी प्रकार के उपयोग करने के लिए समारोह संशोधित किया है। MD5 तरह अपाचे पैदा करने के लिए

/* 
Function change password in htpasswd. 
Arguments: 
$user > User name we want to change password to. 
$newpass > New password 
$type > Type of cryptogrphy: DES, SHA, MD5. 
$salt > Option: Add your custom salt (hashing string). 
      Salt is applied to DES and MD5 and must be in range 0-9A-Za-z 
$oldpass > Option: Add more security, user must known old password to change it. 
      This option is not supported for DES and MD5 without salt!!! 
$path > Path to .htaccess file which contain the password protection. 
      Path to password file is obtained from this .htaccess file. 
*/ 

function changePass($user,$newpass,$type="SHA",$salt="",$oldpass="",$path=".htaccess") { 
    switch ($type) { 
    case "DES" : 
    $salt = substr($salt,0,2); //Salt must be 2 char range 0-9A-Za-z 
    $newpass = crypt($newpass,$salt); 
    if ($oldpass != null) $oldpass = crypt($oldpass,$salt); 
    break; 

    case "SHA" : 
    $newpass = '{SHA}'.base64_encode(sha1($newpass, TRUE)); 
    if ($oldpass != null) $oldpass = '{SHA}'.base64_encode(sha1($oldpass, TRUE)); 
    break; 

    case "MD5" : 
    $salt = substr($salt,0,8); //Salt must be max 8 char range 0-9A-Za-z 
    $newpass = crypt_apr1_md5($newpass, $salt); 
    if ($oldpass != null) $oldpass = crypt_apr1_md5($oldpass, $salt); 
    break; 

    default : 
    return false; 
    break; 
    } 

    $hta_arr = explode("\n", file_get_contents($path)); 

    foreach($hta_arr as $line) { 
    $line = preg_replace('/\s+/','',$line); // remove spaces 
    if ($line) { 
     $line_arr = explode('"', $line); 
     if (strcmp($line_arr[0],"AuthUserFile") == 0) { 
     $path_htaccess = $line_arr[1]; 
     } 
    } 
    } 
    $htp_arr = explode("\n", file_get_contents($path_htaccess)); 

    $new_file = ""; 
    foreach($htp_arr as $line) { 
    $line = preg_replace('/\s+/','',$line); // remove spaces 
    if ($line) { 
     list($usr, $pass) = explode(":", $line, 2); 
     if (strcmp($user,$usr) == 0) { 
     if ($oldpass != null) { 
      if ($oldpass == $pass) { 
      $new_file .= $user.':'.$newpass."\n"; 
      } else { 
      return false; 
      } 
     } else { 
      $new_file .= $user.':'.$newpass."\n"; 
     } 
     } else { 
     $new_file .= $user.':'.$pass."\n"; 
     } 
    } 
    } 
    $f=fopen($path_htaccess,"w") or die("couldn't open the file"); 
    fwrite($f,$new_file); 
    fclose($f); 
    return true; 
} 

फंक्शन:: किसी इसे उपयोगी मिल सकता है

function crypt_apr1_md5($plainpasswd,$salt=null) { 
    $tmp = ""; 
    if ($salt == null) $salt = substr(str_shuffle("abcdefghijklmnopqrstuvwxyz"), 0, 8); 
    $len = strlen($plainpasswd); 
    $text = $plainpasswd.'$apr1$'.$salt; 
    $bin = pack("H32", md5($plainpasswd.$salt.$plainpasswd)); 
    for($i = $len; $i > 0; $i -= 16) { $text .= substr($bin, 0, min(16, $i)); } 
    for($i = $len; $i > 0; $i >>= 1) { $text .= ($i & 1) ? chr(0) : $plainpasswd{0}; } 
    $bin = pack("H32", md5($text)); 
    for($i = 0; $i < 1000; $i++) { 
     $new = ($i & 1) ? $plainpasswd : $bin; 
     if ($i % 3) $new .= $salt; 
     if ($i % 7) $new .= $plainpasswd; 
     $new .= ($i & 1) ? $bin : $plainpasswd; 
     $bin = pack("H32", md5($new)); 
    } 
    for ($i = 0; $i < 5; $i++) { 
     $k = $i + 6; 
     $j = $i + 12; 
     if ($j == 16) $j = 5; 
     $tmp = $bin[$i].$bin[$k].$bin[$j].$tmp; 
    } 
    $tmp = chr(0).chr(0).$bin[11].$tmp; 
    $tmp = strtr(strrev(substr(base64_encode($tmp), 2)), 
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/", 
    "./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"); 
    return "$"."apr1"."$".$salt."$".$tmp; 
} 
+0

कोड बहुत अच्छा है, केवल एक सुझाव है कि $ tmp = "" डालें; पहले: के लिए ($ i = 0; $ i <5; $ i ++) {// ताकि अपरिभाषित नोटिस संदेश न हो। –

-1

शायद ज़रुरत पड़े किसी को सिर्फ एक काम स्क्रिप्ट की तलाश में है, यहाँ एक समाधान है।

यह स्क्रिप्ट में मामूली परिवर्तन के साथ Kavoir द्वारा यहाँ प्रकाशित किया जाता है: जैसे http://www.kavoir.com/backyard/showthread.php?28-Use-PHP-to-generate-edit-and-update-htpasswd-and-htgroup-authentication-files

<?php 
/* 
$pairs = array(
    'username' = 'password', 
); 
*/ 

// Algorithm: SHA1 

class Htpasswd { 

private $file = ''; 

public function __construct($file) { 
    if (file_exists($file)) { 
     $this -> file = $file; 
    } else { 
     return false; 
    } 
} 

private function write($pairs = array()) { 
    $str = ''; 
    foreach ($pairs as $username => $password) { 
     $str .= "$username:{SHA}$password\n"; 
    } 
    file_put_contents($this -> file, $str); 
} 

private function read() { 
    $pairs = array(); 
    $fh = fopen($this -> file, 'r'); 
    while (!feof($fh)) { 
     $pair_str = str_replace("\n", '', fgets($fh)); 
     $pair_array = explode(':{SHA}', $pair_str); 
     if (count($pair_array) == 2) { 
      $pairs[$pair_array[0]] = $pair_array[1]; 
     } 
    } 
    return $pairs; 
} 

public function addUser($username = '', $clear_password = '') { 
    if (!empty($username) && !empty($clear_password)) { 
     $all = $this -> read(); 
     // if (!array_key_exists($username, $all)) { 
      $all[$username] = $this -> getHash($clear_password); 
      $this -> write($all); 
    // } 
    } else { 
     return false; 
    } 
} 

public function deleteUser($username = '') { 
    $all = $this -> read(); 
    if (array_key_exists($username, $all)) { 
     unset($all[$username]); 
     $this -> write($all); 
    } else { 
     return false; 
    } 
} 

public function doesUserExist($username = '') { 
    $all = $this -> read(); 
    if (array_key_exists($username, $all)) { 
     return true; 
    } else { 
     return false; 
    } 
} 

private function getHash($clear_password = '') { 
    if (!empty($clear_password)) { 
     return base64_encode(sha1($clear_password, true)); 
    } else { 
     return false; 
    } 
} 

} 

आप इस स्क्रिप्ट का उपयोग कर सकते हैं:

$htp = new Htpasswd('.htpasswd'); 
$htp -> addUser('username1', 'clearpassword1'); // this will add or edit the user 
$htp -> deleteUser('username1'); 
// check if a certain username exists 
if ($htp -> doesUserExist('username1')) { 
// user exists 
} 
संबंधित मुद्दे