Example: Using PHP to implement the online management of FTP users

Source: Internet
Author: User
Tags exit in ftp implement mkdir mysql administrator password mysql database root directory

The leader wants me to plan a webpage design contest and the Flash creation contest, the request must realize the on-line registration and the uploading work. I realized the requirement through FREEBSD+APACHE+PHP+MYSQL+FTP.

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

FTP Server is the system by default, using the user name and password of the system user, create the system user is equal to create the FTP user, FreeBSD is the operating system belonging to the Unix camp, it does not have like Linux useradd and Groupadd and so on to create the user and group's command, Instead, the PW command is implemented with the corresponding parameters, and the command to create a user as an administrator on FreeBSD is

echo | PW Useradd [-g][groupname] [-s][shelldir][-h 0]

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

If the normal user logged in, you must also use the SU command, the method is called

Su Root–c ' echo | PW Useradd [-g][groupname] [-s][shelldir][-h 0] '

After execution, the system will ask for an administrator password and enter the password to execute the 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 the Popen () function in PHP, which executes the instruction to open the file, the syntax is int popen (String command, String mode), its open file can only be one-way, can only read or write, the corresponding "string mode" is ' R ' or ' W ', "string command" is the command string, in the operation of the file can be used fgets (), FGETSS () and fputs () function, this example uses the Fputs () function to enter the administrator password in the file. If an error in the open file will return a value of false, the last function should remember to call Pclose () off.

Here we plan the FTP user group, we first use PW Groupadd ftpuser to create the Ftpuse group, so that the online application users for the group members. For security reasons, we should not give FTP users telnet permission, so we also have to create a shell for them, so that they can not log through Telnet normal system, the following method: First create a file/bin/ftponly

#!/bin/csh
/bin/cat << XX
Can ony use this username to login ftp server!
And you can don't use it to telnet to this system! Xx
Sleep 10

Between xx in this file is displayed to the user who logged in Telnet to see the information. The information will automatically exit in 10 seconds. Finally, do not forget to use chmod +x/bin/ftponly to give this file executable properties.

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

Finally also pay attention to a problem, Su command is only wheel management group of users can use, when the PHP call su command must also be wheel group members, or the system refused to run, and PHP run System command is the identity of the Apache Web server running identity, The initial user name and user group are nobody, so you have to set up a wheel group of users www for Apache use, Then change the user in the Apache profile httpd.conf to Www,group as wheel, restart Apache, and you can run as a new user.

The following can create the PHP source file checkin.php, the code is as follows:

<?if ($username!= "") and ($userpasswd!= "")//To determine if a form submitted information
{$rootpasswd = "AdminPassword";//define admin password
$creatuser = "su--login root-c ' echo". $userpasswd. "| PW Useradd ". $username."-s/bin/ftponly-g ftpuser–s/bin/ftponly-h 0 ' "; This is the use of the SU and PW commands to create a user's used string
$FP =popen ($creatuser, "w"); Call the Popen () function to execute the command in the string, returning the text handle to the $FP
Fputs ($fp, $ROOTPASSWD); Write admin password to file $fp, equivalent to input 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 Create user directory
Fputs ($fp, $ROOTPASSWD); Enter Administrator password
Pclose ($FP);
$creatdir = "su--login root-c ' mkdir/home/". $username. " /public_html ' ";
$FP =popen ($creatdir, "w"); Execute command Create user Web site root directory
Fputs ($fp, $ROOTPASSWD); Enter Administrator password
Pclose ($FP);
$creatdir = "su--login root-c ' Chown". $username. "/home/" $username. "'"; Change the user directory to the owner of the user himself, initially to run Apache user www.
$FP =popen ($creatdir, "w"); Execute command
Fputs ($fp, $ROOTPASSWD); Enter Administrator password
Pclose ($FP);
$creatdir = "su--login root-c ' Chown". $username. "/home/". $username. " /public_html ' "; Change the ownership of a site's 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> apply for FTP account </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<body bgcolor= "#FFFFFF" >
<div align= "center" >
<p> apply for FTP account </p>
<form method=post action= ". echo $PHP _self?> ">
<table width= "36%" border= "0" >
<tr>
&LT;TD width= "40%" >
<div align= "Right" > account name:</div>
</td>
&LT;TD width= "60%" >
<input type= "text" name= "username" >
</td>
</tr>
<tr>
&LT;TD width= "40%" >
<div align= "Right" > Password:</div>
</td>
&LT;TD width= "60%" >
<input type= "Password" name= "USERPASSWD" >
</td>
</tr>
<tr>
&LT;TD colspan= "2" ><input type= "Submit" name= "Submit" value= "Application" ></td>
</tr>
</table>
</form>
</div><?}? >
</body>


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.