Home  >  Article  >  Backend Development  >  Code to implement online management of Ftp users using PHP_PHP tutorial

Code to implement online management of Ftp users using PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:57:15860browse

The leader asked me to plan a web design competition and a Flash creation competition, which required online registration and uploading of works. I achieved this requirement through FreeBSD+Apache+PHP+Mysql+FTP.

The idea of ​​realizing online registration and uploading works is to use a web form to collect the information filled in by the user and store it in the Mysql database. At the same time, create an FTP upload account with the user's registered name and create the user's corresponding directory.

The FTP server is provided by the system by default. It uses the username and password of the system user. Creating a system user is equivalent to creating an FTP user. FreeBSD is an operating system that belongs to the UNIX camp. It does not have useradd and groupadd like Linux. The command to create users and groups is replaced by the pw command plus the corresponding parameters. The command to create a user as an administrator on Freebsd is

echo | pw useradd < username> [-g][groupname] [-s][shelldir][-h 0]

The parameter g specifies the user group, and the parameter s specifies the user's shell.

If you log in as a normal user, you must also use the su command. The calling method is

su root –c 'echo | pw useradd [-g] [groupname] [-s][shelldir][-h 0] '

After execution, the system will ask for the administrator password. Enter the password to execute this command as an administrator.

The main difficulty in implementing this step is how to call the above system commands through PHP to create a user. This example is implemented using the popen() function in PHP, which executes the command to open the file. The syntax is int popen(string command, string mode). The file it opens can only be one-way, can only be read or written. The corresponding "string mode" is 'r' or 'w', "string command" It is the command string. You can use the fgets(), fgetss() and fputs() functions to operate the file. In this example, the fputs() function is used to enter the administrator password into the file. If an error occurs when opening the file, a false value will be returned. In the last function, remember to call pclose() to close it.

Let’s plan the FTP user group. In advance, we first use pw groupadd ftpuser to create the ftpuse group, so that users who apply online will be members of this group. For security reasons, we should not give FTP users Telnet permissions, so we have to create a shell specifically for them so that they cannot log in to the system normally through Telnet. The method is as follows: First create a file /bin/ftponly

#!/bin/csh

/bin/cat << XX

You can ony use this username to login ftp server!

And you can not use it to telnet to this system! XX

sleep 10

The information between XX in this file is displayed to users who log in with telnet. The information will be displayed for 10 seconds and then it will exit automatically. Finally, don’t forget to use chmod +x /bin/ftponly to give this file the executable attribute.

Then add "/bin/ftponly" to the /bin/shell file. In subsequent commands, we can use the -s parameter in pw to assign this shell to the FTP user.

Finally, one thing to note is that the su command can only be used by user members of the wheel management group. When PHP calls the su command, it must also be run as a member of the wheel group, otherwise the system refuses to run. The identity under which PHP runs system commands is the identity under which the Apache web server runs. The initial user name and user group are both nobody, so you must first create a user www in the wheel group for Apache to use, and then change the user in Apache's configuration file httpd.conf For www, the group is wheel, restart Apache, and you can run it as a new user.

Now you can create the PHP source file checkin.php, the code is as follows:

Copy the code The code is as follows:

{ $rootpasswd="adminpassword"; / /Define administrator password
$creatuser="su --login root -c 'echo ".$userpasswd." | pw useradd ".$username." -s /bin/ftponly -g ftpuser –s /bin/ ftponly -h 0' "; //This is the string used to create a user using the su and pw commands
$fp=popen($creatuser,"w"); //Call the popen() function to execute the string The command in returns the text handle to $fp
fputs($fp,$rootpasswd); //Write the administrator password to the file $fp, which is equivalent to entering the password to the system
pclose($fp);/ /Close the file
$creatdir="su --login root -c 'mkdir /home/".$username."'";//Command string to create the user directory
$fp=popen($creatdir ,"w");//Execute the command to create the user directory
fputs($fp,$rootpasswd); //Enter the administrator password
pclose($fp);
$creatdir="su -- login root -c 'mkdir /home/".$username."/public_html'";
$fp=popen($creatdir,"w"); //Execute the command to create the user website root directory
fputs( $fp,$rootpasswd); //Enter the administrator password
pclose($fp);
$creatdir="su --login root -c 'chown ".$username." /home/".$ username."'"; //Change the owner of the user directory to the user himself, initially to the user www who runs Apache.
$fp=popen($creatdir,"w"); //Execute the command
fputs($fp,$rootpasswd); //Enter the administrator password
pclose($fp);
$creatdir="su --login root -c 'chown ".$username." /home/".$username."/public_html'"; //Change the ownership of the website root directory
$fp=popen($ creatdir,"w");
fputs($fp,$rootpasswd);
pclose($fp);
echo "Congratulations".$username.", your FTP account application has been successful! Please log in to FTP. Please note that you do not have Telnet permissions";}
else{?>


Apply for FTP account< /title> <br><meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <br></head> <br><body bgcolor="#FFFFFF "> <br><div align="center"> <br><p>Apply for FTP account</p> <br><form method=POST action="<?echo $PHP_SELF; ?>"> <br><table width="36%" border="0"> <br><tr> <br><td width="40%"> <br>< ;div align="right">Account name:</div> <br></td> <br><td width="60%"> <br><input type="text" name="username"> <br></td> <br></tr> <br><tr> <br><td width="40%"> <br><div align="right">Password:</div> <br></td> <br><td width="60%"> <br><input type="password" name=" userpasswd"> <br></td> <br></tr> <br><tr> <br><td colspan="2"><input type="submit" name= "Submit" value="Application"></td> <br></tr> <br></table> <br></form> <br></div><? }?> <br></body> <br></html> <br> </p> <p align="left"></p> <div style="display:none;"> <span id="url" itemprop="url">http://www.bkjia.com/PHPjc/317865.html</span><span id="indexUrl" itemprop="indexUrl">www.bkjia.com</span><span id="isOriginal" itemprop="isOriginal">true</span><span id="isBasedOnUrl" itemprop="isBasedOnUrl">http: //www.bkjia.com/PHPjc/317865.html</span><span id="genre" itemprop="genre">TechArticle</span><span id="description" itemprop="description">The leader asked me to plan a web design contest and a Flash creation contest, which required online registration and uploading of works. I achieved this requirement through FreeBSD+Apache+PHP+Mysql+FTP. Implemented in...</span> </div> <div class="art_confoot"></div></div><div class="nphpQianMsg"><div class="clear"></div></div><div class="nphpQianSheng"><span>Statement:</span><div>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</div></div></div><div class="nphpSytBox"><span>Previous article:<a class="dBlack" title="How to hide the copyright below X-Space personal space. Hide the title of X-Space personal space. How to hide the copyright of X-Space personal space_PHP tutorial" href="http://m.php.cn/faq/311369.html">How to hide the copyright below X-Space personal space. Hide the title of X-Space personal space. How to hide the copyright of X-Space personal space_PHP tutorial</a></span><span>Next article:<a class="dBlack" title="How to hide the copyright below X-Space personal space. Hide the title of X-Space personal space. How to hide the copyright of X-Space personal space_PHP tutorial" href="http://m.php.cn/faq/311371.html">How to hide the copyright below X-Space personal space. Hide the title of X-Space personal space. How to hide the copyright of X-Space personal space_PHP tutorial</a></span></div><div class="nphpSytBox2"><div class="nphpZbktTitle"><h2>Related articles</h2><em><a href="http://m.php.cn/article.html" class="bBlack"><i>See more</i><b></b></a></em><div class="clear"></div></div><ul class="nphpXgwzList"><li><b></b><a href="http://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/2.html" title="All expression symbols in regular expressions (summary)" class="aBlack">All expression symbols in regular expressions (summary)</a><div class="clear"></div></li></ul></div></div><div class="nphpFoot"><div class="nphpFootBg"><ul class="nphpFootMenu"><li><a href="http://m.php.cn/"><b class="icon1"></b><p>Home</p></a></li><li><a href="http://m.php.cn/course.html"><b class="icon2"></b><p>Course</p></a></li><li><a href="http://m.php.cn/wenda.html"><b class="icon4"></b><p>Q&A</p></a></li><li><a href="http://m.php.cn/login"><b class="icon5"></b><p>My</p></a></li><div class="clear"></div></ul></div></div><div class="nphpYouBox" style="display: none;"><div class="nphpYouBg"><div class="nphpYouTitle"><span onclick="$('.nphpYouBox').hide()"></span><a href="http://m.php.cn/"></a><div class="clear"></div></div><ul class="nphpYouList"><li><a href="http://m.php.cn/"><b class="icon1"></b><span>Home</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/course.html"><b class="icon2"></b><span>Course</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/article.html"><b class="icon3"></b><span>Article</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/wenda.html"><b class="icon4"></b><span>Q&A</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/dic.html"><b class="icon6"></b><span>Dictionary</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/course/type/99.html"><b class="icon7"></b><span>Manual</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/xiazai/"><b class="icon8"></b><span>Download</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/faq/zt" title="Topic"><b class="icon12"></b><span>Topic</span><div class="clear"></div></a></li><div class="clear"></div></ul></div></div><div class="nphpDing" style="display: none;"><div class="nphpDinglogo"><a href="http://m.php.cn/"></a></div><div class="nphpNavIn1"><div class="swiper-container nphpNavSwiper1"><div class="swiper-wrapper"><div class="swiper-slide"><a href="http://m.php.cn/" >Home</a></div><div class="swiper-slide"><a href="http://m.php.cn/article.html" class="hover">Article</a></div><div class="swiper-slide"><a href="http://m.php.cn/wenda.html" >Q&A</a></div><div class="swiper-slide"><a href="http://m.php.cn/course.html" >Course</a></div><div class="swiper-slide"><a href="http://m.php.cn/faq/zt" >Topic</a></div><div class="swiper-slide"><a href="http://m.php.cn/xiazai" >Download</a></div><div class="swiper-slide"><a href="http://m.php.cn/game" >Game</a></div><div class="swiper-slide"><a href="http://m.php.cn/dic.html" >Dictionary</a></div><div class="clear"></div></div></div><div class="langadivs" ><a href="javascript:;" class="bg4 bglanguage"></a><div class="langadiv" ><a onclick="javascript:setlang('zh-cn');" class="language course-right-orders chooselan " href="javascript:;"><span>简体中文</span><span>(ZH-CN)</span></a><a onclick="javascript:;" class="language course-right-orders chooselan chooselanguage" href="javascript:;"><span>English</span><span>(EN)</span></a><a onclick="javascript:setlang('zh-tw');" class="language course-right-orders chooselan " href="javascript:;"><span>繁体中文</span><span>(ZH-TW)</span></a><a onclick="javascript:setlang('ja');" class="language course-right-orders chooselan " href="javascript:;"><span>日本語</span><span>(JA)</span></a><a onclick="javascript:setlang('ko');" class="language course-right-orders chooselan " href="javascript:;"><span>한국어</span><span>(KO)</span></a><a onclick="javascript:setlang('ms');" class="language course-right-orders chooselan " href="javascript:;"><span>Melayu</span><span>(MS)</span></a><a onclick="javascript:setlang('fr');" class="language course-right-orders chooselan " href="javascript:;"><span>Français</span><span>(FR)</span></a><a onclick="javascript:setlang('de');" class="language course-right-orders chooselan " href="javascript:;"><span>Deutsch</span><span>(DE)</span></a></div></div><script> var swiper = new Swiper('.nphpNavSwiper1', { slidesPerView : 'auto', observer: true,//修改swiper自己或子元素时,自动初始化swiper observeParents: true,//修改swiper的父元素时,自动初始化swiper }); </script></div></div><!--顶部导航 end--><script>isLogin = 0;</script><script type="text/javascript" src="/static/layui/layui.js"></script><script type="text/javascript" src="/static/js/global.js?4.9.47"></script></div><script src="https://vdse.bdstatic.com//search-video.v1.min.js"></script><link rel='stylesheet' id='_main-css' href='/static/css/viewer.min.css' type='text/css' media='all'/><script type='text/javascript' src='/static/js/viewer.min.js?1'></script><script type='text/javascript' src='/static/js/jquery-viewer.min.js'></script><script>jQuery.fn.wait = function (func, times, interval) { var _times = times || -1, //100次 _interval = interval || 20, //20毫秒每次 _self = this, _selector = this.selector, //选择器 _iIntervalID; //定时器id if( this.length ){ //如果已经获取到了,就直接执行函数 func && func.call(this); } else { _iIntervalID = setInterval(function() { if(!_times) { //是0就退出 clearInterval(_iIntervalID); } _times <= 0 || _times--; //如果是正数就 -- _self = $(_selector); //再次选择 if( _self.length ) { //判断是否取到 func && func.call(_self); clearInterval(_iIntervalID); } }, _interval); } return this; } $("table.syntaxhighlighter").wait(function() { $('table.syntaxhighlighter').append("<p class='cnblogs_code_footer'><span class='cnblogs_code_footer_icon'></span></p>"); }); $(document).on("click", ".cnblogs_code_footer",function(){ $(this).parents('table.syntaxhighlighter').css('display','inline-table');$(this).hide(); }); $('.nphpQianCont').viewer({navbar:true,title:false,toolbar:false,movable:false,viewed:function(){$('img').click(function(){$('.viewer-close').trigger('click');});}}); </script></body></html>