search
HomeBackend DevelopmentPHP Tutorial用PHP实现Ftp用户的在线管理_PHP

实现在线报名和上传作品的思路是利用网页表单收集用户填写的资料存储到Mysql数据库内,同时以该用户的注册名创建一个FTP上传账号并创建该用户的相应目录。
  Ftp服务器是系统默认带的,采用系统用户的用户名和密码,创建系统用户就等于创建FTP用户,FreeBSD是属于UNIX阵营的操作系统,它没有象Linux一样的useradd和groupadd等创建用户和组的命令,其取而代之的是pw命令加上相应参数来实现,在Freebsd上以管理员的身份创建一个用户的命令是
复制代码 代码如下:
  echo | pw useradd [-g][groupname] [-s][shelldir][-h 0]

  参数g指定用户组,参数s指定用户的shell。
  如果是普通用户登陆的话还必须用到su命令,调用的方法是
复制代码 代码如下:
  su root –c ‘echo | pw useradd [-g][groupname] [-s][shelldir][-h 0] '

  执行之后系统会要求输入管理员密码,输入密码就可以以管理员的身份执行这个命令了。
  实现这步操作的主要难点在于如何通过PHP来调用上面这些系统命令去创建一个用户,本例是使用PHP中的popen()函数来实现的,该函数执行指令打开文件,语法是int popen(string command, string mode),其打开的文件只能是单向的,只能读或只能写,对应的“string mode”为'r'或'w',“string command” 就是命令字符串,在对文件的操作上可使用 fgets()、fgetss()与fputs()函数,本例用fputs()函数往文件里面输入管理员密码。若是开档发生错误将返回 false 值,最后函数要记得调用pclose()关闭。
  下面我们来规划一下FTP用户的组,事先我们先用pw groupadd ftpuser创建ftpuse组,让在线申请的用户为这个组的成员。为了安全起见,我们不应该给FTP用户Telnet权限,所以我们还要专门给他们创建一个shell,使得他们不能通过Telnet正常登陆系统,方法如下:先创建一个文件/bin/ftponly
复制代码 代码如下:
#!/bin/csh
  /bin/cat   You can ony use this username to login ftp server!
  And you can not use it to telnet to this system! XX
  sleep 10

该文件中XX之间就是显示给用telnet登陆的用户看的信息。信息显示10秒钟就自动退出了。最后不要忘记用chmod +x /bin/ftponly给这个文件可执行属性。
  然后在/bin/shell文件中添加“/bin/ftponly”,在以后的命令里面我们就可以利用pw中-s参数把这个shell指定给FTP用户了。
  最后还要注意一个问题,su命令仅仅是wheel管理组的用户成员才可以使用,当PHP调用su命令的时候也必须以wheel组成员的身份运行,否则系统拒绝运行,而PHP运行系统命令的身份就是Apache Web服务器运行的身份,初始的用户名和用户组都是nobody,所以先得建立一个wheel组的用户www供apache使用,接着更改Apache的配置文件httpd.conf中的user为www,group为wheel,重新启动Apache,就可以以新用户身份运行了。
下面可以创建PHP源文件checkin.php了,代码如下:
复制代码 代码如下:
{ $rootpasswd="adminpassword"; //定义管理员密码
$creatuser ="su --login root -c 'echo ".$userpasswd." | pw useradd ".$username." -s /bin/ftponly -g ftpuser –s /bin/ftponly -h 0' "; //这是利用su和pw命令创建用户的用到的字符串
$fp=popen($creatuser,"w"); //调用popen()函数执行字符串中的命令,返回文句柄给$fp
fputs($fp,$rootpasswd); //写入管理员密码到文件$fp,相当于输入密码给系统
pclose($fp);//关闭文件
$creatdir="su --login root -c 'mkdir /home/".$username."'";//创建用户目录的命令字符串
$fp=popen($creatdir,"w");//执行命令创建用户目录
fputs($fp,$rootpasswd); //输入管理员密码
pclose($fp);
$creatdir="su --login root -c 'mkdir /home/".$username."/public_html'";
$fp=popen($creatdir,"w"); //执行命令创建用户网站根目录
fputs($fp,$rootpasswd); //输入管理员密码
pclose($fp);
$creatdir="su --login root -c 'chown ".$username." /home/".$username."'"; //改变用户目录的所有者为用户自己,初始为运行Apache的用户www。
$fp=popen($creatdir,"w"); //执行命令
fputs($fp,$rootpasswd); //输入管理员密码
pclose($fp);
$creatdir="su --login root -c 'chown ".$username." /home/".$username."/public_html'"; //改变网站根目录的归属
$fp=popen($creatdir,"w");
fputs($fp,$rootpasswd);
pclose($fp);
echo "恭喜".$username.",您的的FTP账号已经申请成功!请到FTP上登陆,请注意,您没有Telnet权限";}
else{?>


申请FTP账号




Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

Simple Guide: Sending Email with PHP ScriptSimple Guide: Sending Email with PHP ScriptMay 12, 2025 am 12:02 AM

PHPisusedforsendingemailsduetoitsbuilt-inmail()functionandsupportivelibrarieslikePHPMailerandSwiftMailer.1)Usethemail()functionforbasicemails,butithaslimitations.2)EmployPHPMailerforadvancedfeatureslikeHTMLemailsandattachments.3)Improvedeliverability

PHP Performance: Identifying and Fixing BottlenecksPHP Performance: Identifying and Fixing BottlenecksMay 11, 2025 am 12:13 AM

PHP performance bottlenecks can be solved through the following steps: 1) Use Xdebug or Blackfire for performance analysis to find out the problem; 2) Optimize database queries and use caches, such as APCu; 3) Use efficient functions such as array_filter to optimize array operations; 4) Configure OPcache for bytecode cache; 5) Optimize the front-end, such as reducing HTTP requests and optimizing pictures; 6) Continuously monitor and optimize performance. Through these methods, the performance of PHP applications can be significantly improved.

Dependency Injection for PHP: a quick summaryDependency Injection for PHP: a quick summaryMay 11, 2025 am 12:09 AM

DependencyInjection(DI)inPHPisadesignpatternthatmanagesandreducesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itallowspassingdependencieslikedatabaseconnectionstoclassesasparameters,facilitatingeasiertestingandscalability.

Increase PHP Performance: Caching Strategies & TechniquesIncrease PHP Performance: Caching Strategies & TechniquesMay 11, 2025 am 12:08 AM

CachingimprovesPHPperformancebystoringresultsofcomputationsorqueriesforquickretrieval,reducingserverloadandenhancingresponsetimes.Effectivestrategiesinclude:1)Opcodecaching,whichstorescompiledPHPscriptsinmemorytoskipcompilation;2)DatacachingusingMemc

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.