dtrpv60860 2016-02-05 17:58
浏览 32

有限的用户和密码。 htpasswd并在php网站上隐藏表单

I have a secret login page (useradmin.php) where users can add and delete users to have access to another secret website (listmembers.php). Now I want to limit the users from adding more users then 8 in the useradmin site. If there are 8 users then I want to hide the add form on that page and only show the users with a message that in order to add another user they need to first delete a current users.

Can I do this in the php file itself which is not connected to a database or do I have to do this in the .htaccess or .htpasswd file?

Here is the php for the useradmin.php

<?php

// list users

$filename = ".htpasswd";

if (!empty($_GET["usr"])) { 
  $usr = $_GET["usr"];
} else {
  $usr = "";
}

if (!empty($_GET["pw"])) $pw = $_GET["pw"];

if (!empty($_GET["radera"])) {
  $radera = $_GET["radera"];
} else {
  $radera = 0;
}

echo "<h2>Administration av användare och lösenord</h2>";

if (strlen($usr)>2) {
  //append new user to end of file
  $handle = fopen($filename, "a");
  $userdata = $usr.":";
  $userdata = $userdata.crypt($pw)."
";
  fputs($handle,$userdata);
  fclose($handle);
}

if ($radera>0) {
  // Delete user 
  $rad = 1;

  //read old file
  $handle = fopen($filename, "r");
  $buffer[$rad] = fgets($handle, 4096);

  while (!feof($handle)) {
    $rad++;
    $buffer[$rad] = fgets($handle, 4096);   
  }

  $maxrad = $rad;
  fclose($handle);

  //write back data from buffer exept deleted user 
  $handle = fopen($filename, "w");
  $rad = 1;  

  while ($rad < $maxrad) {
    if ($rad != $radera) {
      fputs($handle,$buffer[$rad]);
    }
    $rad++;
  }

  fclose($handle);
}

//List users
$handle = fopen($filename, "r");
$rad = 1; 

echo "<table border=1 >";

$buffer[$rad] = fgets($handle, 4096);

while (!feof($handle)) {
  echo "<tr><td>";   
  $user[$rad] = substr($buffer[$rad], 0, strpos($buffer[$rad],":"));
  echo $user[$rad];   
  echo "</td><td>";
  echo "<a href=\"useradmin.php?radera=$rad\">RADERA</a>";    
  echo "</td></tr>";
  $rad++;     
  $buffer[$rad] = fgets($handle, 4096);
}

echo "</table>";
fclose($handle);

// limit users to 8

?>

<h2>Lägg till ny användare</h2>

<h3 style="color:red">OBS! Antalet användare är begränsat till max 8!<br>Du måste ta bort en användare för att kunna lägga till en ny.</h3>

<FORM action="useradmin.php" method="GET" >

Login:    <input type=text name="usr"><br>

Lösenord: <input type="text" name="pw"><br>

       <INPUT type=submit value="UPPDATERA">

</form> 
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?