Home > Article > Backend Development > Code to implement online management of Ftp users using PHP_PHP tutorial
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. Apply for FTP account
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
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
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:
{ $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{?>