Use PHP script to modify Linux or Unix system passwords

Source: Internet
Author: User
Tags php book php programming php script

This article describes how to use a PHP script to modify a Linux or Unix system password.

Required Tools and Installation: You must install the following tools and software:

– The shell script that modifies the password;

–sudo access rights;

–apache or LIGHTTPD Web server;

–php Service-side program.

Step 1: Install the shell foot that can modify the user's password the script could actually be used to modify the password of the Linux user (already in Linux and FreeBSD tests).

Example: Shell script code #!/bin/sh # exec Expect-f "$0″${1+" $@} set password [lindex $argv 1] Spawn passwd [lindex $argv 0] SL Eep 1 Expect "Assword:" Send "$passwordr" expect "Assword:" Send "$PASSWORDR" expect EOF run shell script (download link): $ chpasswd US Ername password Download the script and then copy it to your Web root or other location of the Web server (user readable): $ CP chpasswd/var/www/or if you use LIGHTTPD Web server: $ cp chpass Wd/home/lighttpd

Step 2: Execute the command as root by sudo the Apache or LIGHTTPD Web server will immediately use non-root permissions when it enters the background.  This is a good way to prevent password changes, just as the passwd command requires root permission to modify the password of other user accounts. Typically, Apache 2 uses www-data users, lighttpd use LIGHTTPD users (both regular users, not root users). Log in using the root user and execute the following command: # Visudo Now your Web server allows you to execute password modification scripts (CHPASSWD). If you use the Apache Web server, execute the following command: Www-data all=nopasswd:/var/www/chpasswd or, if you use the LIGHTTPD Web server, execute the following command: httpd ALL=NOP ASSWD:/home/lighttpd/chpasswd Save and exit files.

Step 3. Create a PHP based interface now you need to write a PHP script. Here is a PHP script instance. You can modify it according to your needs. At the very least you need to properly set up the shell script location. Open the PHP script and find the Shellscript line: $shellscript = "sudo/home/lighttpd/chpasswd"; modify Shellscript to point to the correct location. The source code of PHP is downloaded from here:

Step 4: Run the script in your Web browser address bar Input network address-https://mydomain.com/changepassword.php. You will see the username and password hint: If the password changes successfully, you will get the confirmation prompt: For some reasons, if the password modification failed, you can refer to the following prompts for more detailed error message:

Step 5: Secure

Never run the above script directly through the HTTP protocol. Instead, the HTTPS protocol is used.

Put the script into a password-protected directory.

  Never trust user input. The above PHP script is just an example. In a real-world production environment, you need to consider more robust user input validation. Discussing the security of PHP programming is beyond the scope of this article. You can refer to a good PHP book or use your favorite search engine to search for relevant websites.

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.