PHP/MySQL Database/DBM — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 10:06, 26 мая 2010
Calculating password length with DBM
<source lang="html4strict">
<?php $data_file = "/tmp/users.db"; $total_length = 0; if (! ($dbh = dba_open($data_file,"r","gdbm"))) {
die("Can"t open database $data_file");
} $k = dba_firstkey($dbh); while ($k) {
$total_length += strlen(dba_fetch($k,$dbh)); $k = dba_nextkey($dbh);
} print "Total length of all passwords is $total_length characters."; dba_close($dbh); ?>
</source>
Storing structured data in a DBM database
<source lang="html4strict">
<?php $dbh = dba_open("users.db","c","gdbm") or die($php_errormsg); if ($exists = dba_exists($_POST["username"], $dbh)) {
$serialized_data = dba_fetch($_POST["username"], $dbh) or die($php_errormsg); $data = unserialize($serialized_data);
} else {
$data = array();
} if ($_POST["new_password"]) {
$data["password"] = $_POST["new_password"];
} $data["last_access"] = time(); if ($exists) {
dba_replace($_POST["username"],serialize($data), $dbh);
} else {
dba_insert($_POST["username"],serialize($data), $dbh);
} dba_close($dbh); ?>
</source>
Tracking users and passwords with a DBM database
<source lang="html4strict">
<?php $user = $_SERVER["argv"][1]; $password = $_SERVER["argv"][2]; $data_file = "/tmp/users.db"; $dbh = dba_open($data_file,"c","gdbm") or die("Can"t open db $data_file"); if (dba_exists($user,$dbh)) {
print "User $user exists. Changing password.";
} else {
print "Adding user $user.";
} dba_replace($user,$password,$dbh) or die("Can"t write to database $data_file"); dba_close($dbh); ?>
</source>