PHP CookBook Free Open Book

PHP CookBook

Previous Section Next Section

Recipe 18.21 Escaping Shell Metacharacters

18.21.1 Problem

You need to incorporate external data in a command line, but you want to escape out special characters so nothing unexpected happens; for example, you want to pass user input as an argument to a program.

18.21.2 Solution

Use escapeshellarg( ) to handle arguments:

system('ls -al '.escapeshellarg($directory));

Use escapeshellcmd( ) to handle program names:

system(escapeshellcmd($ls_program).' -al');

18.21.3 Discussion

The command line is a dangerous place for unescaped characters. Never pass unmodified user input to one of PHP's shell-execution functions. Always escape the appropriate characters in the command and the arguments. This is crucial. It is unusual to execute command lines that are coming from web forms and not something we recommend lightly. However, sometimes you need to run an external program, so escaping commands and arguments is useful.

escapeshellarg( ) surrounds arguments with single quotes (and escapes any existing single quotes). To print the process status for a particular process:

system('/bin/ps '.escapeshellarg($process_id));

Using escapeshellarg( ) ensures that the right process is displayed even if it has an unexpected character (e.g., a space) in it. It also prevents unintended commands from being run. If $process_id contains:

1; rm -rf /

then:

system("/bin/ps $process_id")

not only displays the status of process 1, but it also executes the command rm -rf /. However:

system('/bin/ps '.escapeshellarg($process_id)) 

runs the command /bin/ps 1; rm -rf, which produces an error because "1-semicolon-space-rm-space-hyphen-rf" isn't a valid process ID.

Similarly, escapeshellcmd( ) prevents unintended command lines from execution. This code runs a different program depending on the value of $which_program:

system("/usr/local/bin/formatter-$which_program");

For example, if $which_program is pdf 12, the script runs /usr/local/bin/formatter-pdf with an argument of 12. But, if $which_program is pdf 12; 56, the script runs /usr/local/bin/formatter-pdf with an argument of 12, but then also runs the program 56, which is an error. To successfully pass the arguments to formatter-pdf, you need escapeshellcmd( ):

system(escapeshellcmd("/usr/local/bin/formatter-$which_program"));

This runs /usr/local/bin/formatter-pdf and passes it two arguments: 12 and 56.

18.21.4 See Also

Documentation on system( ) at http://www.php.net/system, escapeshellarg( ) at http://www.php.net/escapeshellarg, and escapeshellcmd( ) at http://www.php.net/escapeshellcmd.

    Previous Section Next Section
    Index: [SYMBOL][A][B][C][D][E][F][G][H][I][J][K][L][M][N][O][P][Q][R][S][T][U][V][W][X][Z]


         Main Menu
    Main Page
    Table of content
    Copyright
    Preface
    Chapter 1. Strings
    Chapter 2. Numbers
    Chapter 3. Dates and Times
    Chapter 4. Arrays
    Chapter 5. Variables
    Chapter 6. Functions
    Chapter 7. Classes and Objects
    Chapter 8. Web Basics
    Chapter 9. Forms
    Chapter 10. Database Access
    Chapter 11. Web Automation
    Chapter 12. XML
    Chapter 13. Regular Expressions
    Chapter 14. Encryption and Security
    Chapter 15. Graphics
    Chapter 16. Internationalization and Localization
    Chapter 17. Internet Services
    Chapter 18. Files
    18.1 Introduction
    Recipe 18.2 Creating or Opening a Local File
    Recipe 18.3 Creating a Temporary File
    Recipe 18.4 Opening a Remote File
    Recipe 18.5 Reading from Standard Input
    Recipe 18.6 Reading a File into a String
    Recipe 18.7 Counting Lines, Paragraphs, or Records in a File
    Recipe 18.8 Processing Every Word in a File
    Recipe 18.9 Reading a Particular Line in a File
    Recipe 18.10 Processing a File Backward by Line or Paragraph
    Recipe 18.11 Picking a Random Line from a File
    Recipe 18.12 Randomizing All Lines in a File
    Recipe 18.13 Processing Variable Length Text Fields
    Recipe 18.14 Reading Configuration Files
    Recipe 18.15 Reading from or Writing to a Specific Location in a File
    Recipe 18.16 Removing the Last Line of a File
    Recipe 18.17 Modifying a File in Place Without a Temporary File
    Recipe 18.18 Flushing Output to a File
    Recipe 18.19 Writing to Standard Output
    Recipe 18.20 Writing to Many Filehandles Simultaneously
    Recipe 18.21 Escaping Shell Metacharacters
    Recipe 18.22 Passing Input to a Program
    Recipe 18.23 Reading Standard Output from a Program
    Recipe 18.24 Reading Standard Error from a Program
    Recipe 18.25 Locking a File
    Recipe 18.26 Reading and Writing Compressed Files
    Recipe 18.27 Program: Unzip
    Chapter 19. Directories
    Chapter 20. Client-Side PHP
    Chapter 21. PEAR
    Colophon
    Index


    More Books
    PHP Hacks
    Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
    The Koran (Holy Qur'an)
    Macromedia Flash 8 Bible
    Search Engine Optimization for Dummies
    YouTube Traffic
    PHP 5 for Dummies
    Harry Potter and The Chamber of Secrets
    Harry Potter and the Sorcerer's Stone
    The Pilgrim's Progress
    Wireless Hacks
    Flash Hacks. 100 Industrial-Strength Tips & Tools
    PayPal Hacks. 100 Industrial-Strength Tips and Tools
    Amazon Hacks
    Pdf Hacks
    The Da Vinci Code
    Google Hacks
    The Holy Bible
    Windows XP For Dummies
    Harry Potter and the Half-Blood Prince
    Seo Book
    Upgrading and Repairing Networks
    Macromedia Dreamweaver 8 UNLEASHED
    Windows XP Annoyances
    Windows XP Hacks
    Microsoft Windows XP Power Toolkit
    Teach Yourself MS Office In 24Hours
    iPod & iTunes Missing Manual
    PC Hacks 100 Industrial-Strength Tips and Tools
    PC Overclocking, Optimization, and Tuning - 2th Edition
    PC Hardware In A Nutshell 3rd Edition
    PC Hardware in a Nutshell, 2nd Edition
    Upgrading and Repairing PCs
    Google for Dummies
    MySQL Cookbook
    Teach Yourself Macromedia Flash 8 In 24 Hours
    PHP CookBook
    Sams Teach Yourself JavaScript in 24 Hours
    PHP5 Manual
    Free Games Paper Airplanes
    500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele