Archive for April, 2007

Using mysqldump to export a database

mysqldump is a useful utility for dumping the contents of an entire database to an SQL file.

CODE:
  1. mysqldump --user=dbusername --password=dbpassword db_name_here> db_name_here.sql

By default the --opt option is enabled, which is a shorthand option for specifying the below options. According to the documentation, using this option "... should give you a fast dump operation and produce a dump file that can be reloaded into a MySQL server quickly".

CODE:
  1. --add-drop-table
  2. --add-locks
  3. --create-options
  4. --disable-keys
  5. --extended-insert
  6. --lock-tables
  7. --quick
  8. --set-charset

No Comments

PHP/CURL

cURL is used to interact with remote URLs without needing a user to initiate the process (e.g. by clicking on a form submit button). cURL is useful for submitting HTTP POST/PUT/DELETE requests when dealing with web services. PHP has inbuilt cURL support since PHP 4.0.2 using cURL's libcurl library. Find out more about PHP/CURL -- using libcurl with PHP here.

A fullset of cURL options and what they mean can be found in
PHP's manual entry for curl_setopt().

PHP:
  1. //Contains encoded string to pass along for basic authentication purposes
  2. $auth_token = base64_encode($username . '-' . $password);
  3.  
  4. //Target URL - the URL you want to submit a form to
  5. $target_url = 'http://www.remotesite.com/post_target.php';
  6.  
  7. //Create  a new cURL handle
  8. //Passing the target URL to curl_init allows you to bypass the call curl_setopt($ch, CURLOPT_URL, $target_url);
  9. $ch = curl_init($target_url);
  10.  
  11. //Tell the handler that the info is to be sent using an HTTP POST request
  12. curl_setopt($ch, CURLOPT_POST, true);
  13.  
  14. //Set other relevant headers.  Place each header as an array element
  15. //An alternative to building the Authorization header is to use curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
  16. $headers = array('Authorization: Basic ' . $auth_token,
  17.                  'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3');
  18. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  19.  
  20. //Pass the POST fields - be sure to urlencode your value strings (hint: http_build_query() will do this for you; PHP5)
  21. //Below we assume values have already been posted to this script and kept in $_POST.  We have validated the submission and
  22. // are now posting the same values to a remote URL
  23. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($_POST));
  24.  
  25. //When we execute the handle, we want curl_exec() to return to a string rather than directly outputting it
  26. curl_setopt($ch, CURLOPT_RETURNTRANSER, true);
  27.  
  28. //Don't use a cached connection - explicitly create a new one
  29. curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
  30.  
  31. //Fail if cannot connect to the target server within 5 seconds
  32. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  33.  
  34. //If the target server returns a redirect request using the "Location:" header directive, then follow it.
  35. //To prevent recursive redirects, only do a max of 5 follows
  36. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  37. curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
  38.  
  39. //Let's now execute the handler
  40. //Because CURLOPT_RETURNTRANSFER is true, we need to capture the return value of curl_exec()
  41. $response_data = curl_exec($ch);
  42.  
  43. //Was there an error?
  44. //curl_errno() returns the error code
  45. //curl_error() returns a clear text message for the last cURL operation
  46. if (curl_errno($ch)> 0){
  47.     die('There was a cURL error: ' . curl_error($ch));
  48. } else {
  49.     //Close the handler and release resources
  50.     curl_close($ch);
  51. }
  52.  
  53.  
  54. //Now do something with your data
  55. return myProcessingFunction($data);

No Comments

Rewriting URLs to force www or non-www URLs

CODE:
  1. #Activate the rewrite engine if it isn't so already
  2. RewriteEngine on
  3. #intercept all HTTP requests to the site that do not have a 'www.' at the beginning of the Domain name
  4. #and then force a redirect to the same page address only this time, including the 'www.'
  5. RewriteCond %{HTTP_HOST} !^www\..* [NC]
  6. RewriteRule ^(.*) http://www.%{HTTP_HOST}/$1 [R=301]
  7. #The inverse of the above rule - intercept HTTP requests with a 'www.' at the start and redirect to the non-www version
  8. # Ensure that your serverName directive is set up to the non-www name of your domain
  9. RewriteCond %{HTTP_HOST} ^www\..* [NC]
  10. RewriteRule ^(.*) http://%{SERVER_NAME}/$1 [R=301]

No Comments

Sending a plain text e-mail

Sending a plain-text e-mail through PHP is a simple process

PHP:
  1. mail ($to, $subject, $message, $headers);

The $to parameter can look like:

example@mysite.com
example@mysite.com, user@mysite.com
Example Sr <example@mysite.com>, Example Jr <user@mysite.com>

Common headers to use in your messages:

PHP:
  1. $headers =  "From: yourname@yoursite.com\r\n
  2.              Reply-To: replyhere@yoursite.com\r\n
  3.              Cc: watcher@yoursite.com\r\n
  4.              Bcc: spy@yoursite.com\r\n
  5.              X-Mailer: YourApplicationNameHere\r\n"

No Comments

htpasswd

htpasswd is a command line tool for creating and updating user authentication files. These files are primarily used by apache to authenticate HTTP users.

Creating a password file

CODE:
  1. htpasswd -c /path/to/.htpasswd username

Adding a user & password

CODE:
  1. htpasswd -b /path/to/.htpasswd username password

No Comments