Code _php tutorial for implementing FTP user's online management with PHP

Source: Internet
Author: User
The leader wants me to plan a web design contest and the Flash creation contest, the request must realize the online registration and the upload work. By freebsd+apache+php+mysql+ftp I realized that requirement.

The idea of online registration and uploading works is to use Web Forms to collect user-filled data stored in the MySQL database, and create an FTP upload account with the user's registered name and create a corresponding directory for that user.

FTP Server is the system default, with the user name and password of the system user, create the system user is equal to create the FTP user, FreeBSD is the UNIX camp operating system, it does not like Linux useradd and Groupadd, such as the creation of user and group commands, It is replaced by the PW command plus the corresponding parameters to achieve, on the FreeBSD as an administrator to create a user's command is

Echo | PW Useradd [-G] [GroupName] [-S] [Shelldir] [-H 0]

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

If an ordinary user logged in, you must also use the SU command, the method to invoke is

Su Root–c ' echo | PW Useradd [-G] [GroupName] [-S] [Shelldir] [-H 0] '

After execution, the system will be asked to enter the administrator password, enter the password to execute this command as an administrator.

The main difficulty in implementing this step is how to invoke the above system commands through PHP to create a user, this example is implemented using PHP's Popen () function, which executes the instruction open file, the syntax is int popen (String command, String mode), its open file can only be one-way, can only read or write only, the corresponding "string mode" is ' R ' or ' W ', "string command" is the order string, in the operation of the file can use Fgets (), FGETSS () and fputs () function, this example uses the Fputs () function to enter the administrator password into the file. If an error occurs in the file, it will return a value of false, and the last function should remember to call Pclose () off.

Let's plan the group of FTP users, first we use PW Groupadd ftpuser to create the Ftpuse group, so that users of the online application are members of this group. For security reasons, we should not give FTP users telnet permissions, so we also have to create a shell for them, so that they can not telnet to the normal system, the method is as follows: first create a file/bin/ftponly

#!/bin/csh

/bin/cat << XX

Can ony use this username to login ftp server!

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

Sleep 10

The file between xx is displayed to the user with Telnet login to see the information. The message shows that it automatically exits in 10 seconds. Finally, don't forget to use chmod +x/bin/ftponly to give this file an executable property.

Then add "/bin/ftponly" to the/bin/shell file, and in a later command we can use the-s parameter in PW to assign the shell to the FTP user.

Finally, it is important to note that the SU command is only available to the user members of the wheel management group, and when PHP calls the SU command, it must also run as a member of the wheel group, or the system will refuse to run, and the identity of the PHP Run System command is the identity of the Apache Web server running. The initial user name and user group are nobody, so first set up a user of the wheel group WWW for Apache to use, Then change the Apache configuration file httpd.conf in the user www,group to wheel, restart Apache, you can run as a new user.

Below you can create PHP source file checkin.php, the code is as follows:
Copy CodeThe 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 the 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 handle to the $FP
Fputs ($fp, $ROOTPASSWD); Write admin password to file $fp, equivalent to enter password to system
Pclose ($FP);//Close file
$creatdir = "su--login root-c ' mkdir/home/". $username. "'"; /create a command string for the 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 a command to create a user site 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, starting with the user who runs Apache www.
$FP =popen ($creatdir, "w"); Execute command
Fputs ($fp, $ROOTPASSWD); Enter the administrator password
Pclose ($FP);
$creatdir = "su--login root-c ' Chown". $username. "/home/". $username. " /public_html ' "; Change the attribution of the site root directory
$FP =popen ($creatdir, "w");
Fputs ($fp, $ROOTPASSWD);
Pclose ($FP);
echo "Congratulations". $username. ", your FTP account has been successfully applied! Please log on to FTP, please note that you do not have Telnet permission ";"
Else{?>


<title>Request an FTP account</title>




Request an FTP account






http://www.bkjia.com/PHPjc/317865.html www.bkjia.com true http://www.bkjia.com/PHPjc/317865.html techarticle the leader wants me to plan a web design contest and the Flash creation contest, the request must realize the online registration and the upload work. By freebsd+apache+php+mysql+ftp I realized that requirement. To achieve in the ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.