Php Command Line programming example

Source: Internet
Author: User

There are two constants in the command line. stdout and stdin represent the standard output and the standard input respectively. They are used to implement the interaction of the command line. #! /Usr/bin/PHP-q <? PHP/*** php Command Line writing Introduction * @ author rains rains31@gmail.com * @ copyright http://rains.cn * // how to judge Program Is it running in command line mode? // The simplest method is: $ _ server ['request _ method'] in the command line program is null. // For Web requests, the post or get method is generally used, while the command line does not have a request method. Define ('user _ dir ','. /data'); If (empty ($ _ server ['request _ method']) {// obtain the command line parameters, is an array $ ARGs = $ _ server ['argv']; If (isset ($ _ server ['_']) & basename ($ _ server ['_'])! = 'Php') {$ cmd = $ _ server ['_'];} else { $ Cmd = 'php '. array_shift ($ ARGs);}/*** obtain the option parameter */$ option = array_shift ($ ARGs); Switch ($ option) {Case '': case '-H': Case' -- help': $ MSG. = "Usage: \ t $ cmd option [ID] \ r \ n \ tavailable options: \ r \ n"; $ MSG. = "\ t-h \ tshow this help. \ r \ n "; $ MSG. = "\ t-A \ Tadd user \ r \ n"; $ MSG. = "\ t-D name \ tdelete user \ r \ n"; $ MSG. = "\ t-P name \ tmodify password \ r \ n"; $ MSG. = "\ t-L \ tlist all users. \ r \ n "; break; Case '-a': $ name = Read_name (); $ pass = read_pass (); $ MSG = save_user (Array ('name' => $ name, 'pass' => $ pass); break; case '-d': $ name = array_shift ($ ARGs); $ MSG = del_user ($ name); break; Case'-P ': $ name = array_shift ($ ARGs); $ MSG = modify_user ($ name); break; Case '-l': $ files = glob (user_dir. '/*. dat '); foreach ($ files as $ file) {$ MSG. = basename ($ file ,'. dat '). "\ r \ n";} $ MSG. = count ($ files ). "user (s) \ r \ n"; break; default: $ MSG = "inv Alid option '$ option', please try '-H' for instructions. \ r \ n "; break;} fwrite (stdout, $ MSG); exit;} function read_name () {$ MSG = 'plese input your prefered name (1-16 chars, 0-9 A-Z A-Z _ and-): '; fwrite (stdout, $ MSG ); while (preg_match ('/^ [-_ 0-9a-za-z] {1, 16} $/', ($ name = trim (fgets (stdin, 16), $ matches) = false) {fwrite (stdout, "invalid name! \ R \ n ". $ MSG);} return trim ($ name ); } Function read_pass () {$ MSG = "Please set your password (1-16 chars):"; fwrite (stdout, $ MSG ); while ($ pass = trim (fgets (stdin, 16) = '') {fwrite (stdout," invalid password! \ R \ n ". $ MSG);} return trim ($ pass);} function save_user ($ Param = array ()) {if ($ Param ['name'] = ''| $ Param ['pass'] ='') {$ MSG = 'user name or password is empty! ';} $ User_data = user_dir. '/'. $ Param ['name']. '. dat '; If (file_exists ($ user_data) {$ MSG = "sorry, user {$ Param ['name']} already exists! ";} Else {$ fp = fopen ($ user_data, 'w'); If (! $ FP) {$ MSG = "Sorry, some error occurred while writing user data. ";} else {If (fwrite ($ FP, MD5 ($ Param ['pass']) {$ MSG =" Congratulations! User {$ Param ['name']} has been accepted. ";} else {$ MSG =" Sorry, some error occurred while writing user data. ";}} fclose ($ FP);} return $ MSG;} function del_user ($ name) {If (! Preg_match ('/^ [-_ 0-9a-za-z] {} $/', $ name, $ matches) {return 'invalid user name '. $ name;} $ user_data = user_dir. '/'. $ name. '. dat '; If (! File_exists ($ user_data) {return "sorry, user {$ name} doesn't exist! ";} Else {$ MSG =" are you sure to delete user $ name? Press Y to confirm or any other key to cancel: "; if (confirm ($ MSG )! = 'Y') {return "canceled! \ R \ n ";}} if (unlink ($ user_data) {return" User $ name has been successfully deleted! ";} Else {return" delete failed! ";}} Function confirm ($ MSG ='') {fwrite (stdout, $ MSG); Return fgetc (stdin);} function modify_user ($ name) {$ user_data = user_dir. '/'. $ name. '. dat '; If (! File_exists ($ user_data) {return "sorry, user {$ name} doesn't exist! ";}$ MSG =" Please enter old password (1-16 chars): "; fwrite (stdout, $ MSG); While ($ pass = trim (fgets (stdin, 16) = '') {fwrite (stdout," invalid password! \ R \ n ". $ MSG);} $ fp = fopen ($ user_data, 'r + '); If (! $ FP) {return "Sorry, some error occurred while reading user data.";} else {$ old_pass = fread ($ FP, 32); If (MD5 ($ pass )! = $ Old_pass) {return "old password $ old_pass! = $ Pass doesn't match ";}$ MSG =" Please enter new password (1-16 chars): "; fwrite (stdout, $ MSG ); while ($ pass = trim (fgets (stdin, 16) = '') {fwrite (stdout," invalid password! \ R \ n ". $ MSG);} rewind ($ FP); If (fwrite ($ FP, MD5 ($ pass) {$ MSG =" New Password accepted! "; } Else {$ MSG = "Sorry, some error occurred while writing user data. ";}fclose ($ FP) ;}return $ MSG ;}> you can continue to implement the following web functions. However, there is no need for two functions to share one file. Command line. It is best to put it outside the web directory to avoid leakage of confidential information due to configuration errors.

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.