Use PHP to implement online Ftp user management

Source: Internet
Author: User
The leader asked me to plan a web design competition and Flash creation competition, and requested that I register online and upload my work. I implemented this request through FreeBSD + Apache + PHP + Mysql + FTP. The idea of implementing online registration and uploading works is to use web forms for receiving

The leader asked me to plan a web design competition and Flash creation competition, and requested that I register online and upload my work. I implemented this request through FreeBSD Apache + PHP + Mysql FTP.

The idea of online registration and file upload is to use web forms to collect user-filled materials and store them in the Mysql database, create an FTP upload account with the user's registration name and create the corresponding directory for the user.

By default, the FTP server uses the user name and password of the system user. when a system user is created, the FTP user is created. FreeBSD is a UNIX-based control system, it does not have user and group creation commands like useradd and groupadd in Linux. it is replaced by pw commands and corresponding parameters, the command for creating a user as a administrator on Freebsd is:

Echo | Pw useradd [-G] [groupname] [-s] [shelldir] [-h 0]

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

The su command must be used for normal user login.

Su root-c 'echo | Pw useradd [-G] [groupname] [-s] [shelldir] [-h 0]'

After the command is executed, the system requests the password of the Administrator. then, the administrator can execute the command as the administrator.

An important difficulty in implementing this step is how to use PHP to call the above system commands to create a user. This example is implemented by using the popen () function in PHP, the function executes the command to open the file. The syntax is int popen (string command, string mode). The opened file can only be one-way and can only be read or written, the corresponding "string mode" is 'r' or 'W', and "string command" is the command string. you can apply fgets () and fgetss () to file control () in this example, the fputs () function is used to input the administrator password to the file. If an error occurs when the file is opened, the value of false is returned. remember to call pclose () to close the function.

Next, let's plan the FTP user group. in advance, we first create the ftpuse group with pw groupadd ftpuser so that the online user applied for is a member of this group. For the sake of security, we should not grant the FTP user The Telnet permission. Therefore, we need to create a shell for them so that they cannot log on to the system 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

In this file, XX is the information displayed to the user logging on via telnet. The information is displayed and automatically exits in 10 seconds. Do not forget to use chmod x/bin/ftponly to give this file executable properties.

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

Finally, pay attention to the title. The su command can be applied only to the user members of the wheel governance group. When PHP calls the su command, it must also run as a member of the wheel group, otherwise, the system will refuse to run, while the identity of the PHP System Command is the identity of the Apache web server running. the initial user name and User Group are both nobody, therefore, you must first create a user www in The wheel Group for apache applications, and then change the Apache configuration file httpd. in conf, the user is www, the group is wheel, and Apache is restarted to run as a new user.

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.