Code for online Ftp user management using PHP

Source: Internet
Author: User

The leader asked me to plan a Web 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 <passwd> | pw useradd <username> [-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 <passwd> | pw useradd <username> [-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:Copy codeThe Code is as follows: <? If ($ username! = "") And ($ userpasswd! = "") // Determines whether a form has been submitted.
{$ 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 {?>
<Html>
<Head>
<Title> apply for an FTP account </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>
<Body bgcolor = "# FFFFFF">
<Div align = "center">
<P> apply for an FTP account </p>
<Form method = POST action = "<? Echo $ PHP_SELF;?> ">
<Table width = "36%" border = "0">
<Tr>
& Lt; td width = "40%" & gt;
<Div align = "right"> Account name: </div>
</Td>
& Lt; td width = "60%" & gt;
<Input type = "text" name = "username">
</Td>
</Tr>
<Tr>
& Lt; td width = "40%" & gt;
<Div align = "right"> password: </div>
</Td>
& Lt; td width = "60%" & gt;
<Input type = "password" name = "userpasswd">
</Td>
</Tr>
<Tr>
<Td colspan = "2"> <input type = "submit" name = "Submit" value = "Apply"> </td>
</Tr>
</Table>
</Form>
</Div> <?}?>
</Body>
</Html>

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.