Code for online Ftp user management using PHP

Source: Internet
Author: User
Code leaders who use PHP to implement online management for Ftp users want me to plan a webpage design competition and Flash creation competition. online registration and uploading of works are required. I have implemented this requirement through FreeBSD + Apache + PHP + Mysql + FTP.

The idea of online registration and file upload is to use web forms to collect user-filled information and store it in the Mysql database, at the same time, 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. creating a system user is equivalent to creating an FTP user. FreeBSD is a UNIX operating system, it does not have user and group creation commands like useradd and groupadd in Linux. Instead, it uses pw commands and corresponding parameters to implement these commands, the command for creating a user as an 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 requires you to enter the administrator password. then, you can run the command as the administrator.

The main difficulty in implementing this step is how to use PHP to call the above system commands to create a user. In this example, the popen () function in PHP is used, this function executes commands 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 use fgets () and fgetss () in file operations () this example uses the fputs () function to enter the administrator password in the file. If an error occurs when the file is opened, the return value is false. 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. Finally, do not forget to use chmod + x/bin/ftponly to give this file executable attributes.

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.

Note 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 rejects the operation, the identity for running system commands in PHP is the identity for running Apache web servers. the initial user name and User Group are both nobody. Therefore, you must first create a wheel Group User www for apache to use, 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.

The following code creates the PHP source file checkin. php:

The code is as follows:

{$ Rootpasswd = "adminpassword"; // defines 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 file handle to $ fp
Fputs ($ fp, $ rootpasswd); // write the administrator password to the file $ fp, equivalent to entering the password to the system
Pclose ($ fp); // close the file
$ Creatdir = "su -- login root-c 'mkdir/home/". $ username. "'"; // create a command string for the user directory
$ Fp = popen ($ creatdir, "w"); // execute the command to create a 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 root directory of the user's website
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's own, and the user www running Apache initially.
$ 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 root directory of the website
$ 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 the Telnet permission ";}
Else {?>

Apply for an FTP account

Apply for an FTP account

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: 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.