10.07.2015 Views

Beginning Web Development With Perl : From Novice to ... - Nabo

Beginning Web Development With Perl : From Novice to ... - Nabo

Beginning Web Development With Perl : From Novice to ... - Nabo

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

70CHAPTER 3 ■ DATABASES AND PERLTroubleshooting Database InteractionMany a <strong>Perl</strong> DBI developer has been perplexed by database connectivity. Troubleshootinga database connection is no different from troubleshooting another bit of code. Well, it maybe a little different, because troubleshooting a database connection is easier! Most serverswill have a command-line interface (CLI) in<strong>to</strong> the database or another means for you <strong>to</strong> testyour connection apart from the program itself.Here are some troubleshooting tips for working with database connectivity issues ina <strong>Perl</strong> program:• If a connection <strong>to</strong> a database isn’t working, the first thing you should do is examinethe amount of error reporting that you have coming from the database. Be sure thatPrintError and RaiseError are enabled, and use the or die() construct as much aspossible.• One of the most helpful debugging and troubleshooting techniques is the frequent useof simple print statements <strong>to</strong> help determine where the problem might lie. In the contex<strong>to</strong>f a SQL statement, it’s very helpful <strong>to</strong> print out the statement exactly as it’s about<strong>to</strong> be run on the database. Doing so can help you <strong>to</strong> see that a parameter isn’t beingfilled in correctly or might help you spot another error.• On some systems, you can enable logging of all queries for certain database serverssuch as MySQL. Be careful in doing so, however, as this can lead <strong>to</strong> a massive amount oflogging and can also log potentially sensitive information, depending on what’s s<strong>to</strong>redin your database.• Databases that have a CLI are especially helpful. Connect <strong>to</strong> the CLI as the same userthat you’re using for the program, and attempt <strong>to</strong> execute the same SQL statement <strong>to</strong>see if it works. You would be surprised how many issues can be solved by connecting <strong>to</strong>the CLI <strong>to</strong> imitate the program’s behavior in an interactive way. Refer <strong>to</strong> the documentationfor your database server <strong>to</strong> determine the syntax for connecting <strong>to</strong> the CLI.Security Considerations with Data AccessAccessing a database from a <strong>Perl</strong> program, especially one that’s on the <strong>Web</strong>, presents its own se<strong>to</strong>f security risks and challenges. You most definitely don’t want <strong>to</strong> allow unauthorized access <strong>to</strong>the database and the data it contains. This section looks at some of the risks involved and givessuggestions for mitigating those risks.S<strong>to</strong>red CredentialsOne of the most obvious risks in connecting <strong>to</strong> a database from a program is that the credentials(username and password) are s<strong>to</strong>red in the program itself or within a file that the program canread for those values. Unfortunately, there’s no truly effective method for mitigating this risk. Ifyou’re running the program on a shared server, such as some web hosting provider’s server, youneed <strong>to</strong> ensure that other users cannot read the program’s code <strong>to</strong> see the password. This is usuallyeasier said than done. However, many web hosting providers now have users running in

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!