Home >Backend Development >PHP Tutorial >Online management Code for online management of Ftp users using PHP
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. Copy the code The code is as follows: 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.
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 to create users and groups. command, which 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 are an ordinary user logging in, 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. This function 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', and "string command" is the command string. You can use the fgets(), fgetss() and fputs() functions to operate on files. 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 can 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 XX in this file is displayed to Information viewed by users logged in using 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 future 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, and PHP runs system commands. The identity 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 the Apache configuration file httpd.conf to www and the group to 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 the 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 command in the string and return 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 user directory
$fp=popen($creatdir,"w");//Execute command to create 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 has been applied successfully! Please log in to FTP. Please note that you do not have Telnet permissions";}
else{?>
The above has introduced the code for online management using PHP to implement online management of Ftp users, including the content of online management. I hope it will be helpful to friends who are interested in PHP tutorials.