Wednesday, March 30, 2011

MySQL Commands List

MySQL Commands List

Here you will find a collection of basic MySQL statements that should prove useful for basic CRUDS operations (create, replace, update, delete, select).
There may be some issues with the kind of quotes you use around your data. If you are having difficulties, try using singe quotes (  '  ) or tricky quotes (  `  -- on keyboard key next to number 1) around your data. One or the other is bound to work.
The following statements are covered in this page:
CREATE DATABASE
CREATE DATABASE database_name ;
Will create a MySQL database.
You may have to ensure that the user you used to log in has enough privileges to create databases.
Some web hosting companies do not allow their customers to create databases -- as they have already created a couple databases for you. In this case you should already have enough privy's to create tables on the already created databases. Check with your hosting provider to see what the proper database names are and what the usernames and passwords are for the database(s).
Example:
CREATE DATABASE myMediaLibrary;


CREATE TABLE
Create a table on the database you are currently logged into.
Example:
CREATE TABLE IF NOT EXISTS `music` (
`id` int(8) unsigned NOT NULL,
`artist` text NOT NULL,
`album` text NOT NULL,
`title` text NOT NULL,
`track` text NOT NULL,
`year` varchar(8) NOT NULL,
PRIMARY KEY (`id`),
FULLTEXT KEY `artist` (`artist`),
FULLTEXT KEY `album` (`album`),
FULLTEXT KEY `title` (`title`),
FULLTEXT KEY `track` (`track`),
FULLTEXT KEY `year` (`year`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;
The table set up will be similar to:
idartistalbumtitletrackyear
      
NOTE: You may want to use AUTO INCREMENT to have MySQL automatically maintain the id field when you add a new item to the table. This is up to you on how you would like to maintain the id., which in this case is the "PRIMARY KEY" which needs to be unique and is what MySQL needs to keep things orderly:
CREATE TABLE IF NOT EXISTS `music` (
`id` int(8) unsigned NOT NULL AUTO_INCREMENT,
`artist` text NOT NULL,
`album` text NOT NULL,
`title` text NOT NULL,
`track` text NOT NULL,
`year` varchar(8) NOT NULL,
PRIMARY KEY (`id`),
FULLTEXT KEY `artist` (`artist`),
FULLTEXT KEY `album` (`album`),
FULLTEXT KEY `title` (`title`),
FULLTEXT KEY `track` (`track`),
FULLTEXT KEY `year` (`year`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
Refer to the MySQL documentation for more info on defining column data types.




INSERT STATEMENTS
INSERT INTO table_name ( `col_A`, `col_B`, `col_C`) VALUES ( `col_A_data`, `col_B_data`, `col_C_data`) ;
Inserts a row into the table using the data defined in the VALUES section.
As you can see, the column names are established within the first set of parenthesis, the order of the data in the second set of parenthesis must match the order of the column names defined int he first set of parentheses.
Example:
INSERT INTO music ( 'id', `artist`, `album`) VALUES ( '1', `the beatles`, `Abbey Road`);
Puts a row into the database tables as:
idartistalbumtitletrackyear
1the beatlesAbbey Road   

I've color coded each "pair" so you can see that the data "col_B_data' that is going to go into column name "col_B" must be second. if there is no data available for col_B, use empty single quotes as:
INSERT INTO table_name ( `col_A`, `col_B`, `col_C`) VALUES ( `col A data `, ``, `col C data`) ;
Example:
INSERT INTO music ( 'id', `artist`, `album`) VALUES ( '1', ``, `Abbey Road`);
Puts a row into the database tables as:
idartistalbumtitletrackyear
1 Abbey Road   



REPLACE STATEMENTS
REPLACE INTO table_name ( `col_A`, `col_B`) VALUES ( `col A data`, `col B data`) ;
Replaces items in a row.   This action is dependant on the "id" because all tables need to have at least one "unique" column. Meaning that one of the columns must be used to provide row data that is unique to each row. In this example, "id" is the unique row.
Example:
REPLACE INTO music ( 'id', `artist`, `album`) VALUES ( '1', `the beatles`, `abbey road`);
Puts a row into the database tables as:
idartistalbumtitletrackyear
1the beatlesAbbey Road   
As you can see, we've just replaced the empty "artist" with the new data of "the beatles"
REPLACE behaves much like INSERT except that if the "unique column" is supplied, the existing row will be updated, if the "unique column" is not provided, a new row will be added.
Example:
REPLACE INTO music ( `artist`, `album`) VALUES ( `the beatles`, `abbey road`);
Puts a new row into the database tables as:
idartistalbumtitletrackyear
1the beatlesAbbey Road   
 the beatlesAbbey Road   
NOTE: You may get errors when trying this kind of REPLACE, because usually the "unique column" can not be null (or empty). Some people like to set up the database where the "unique column" is automatically incremented by MySQL. This is kind of confusing and can lead to a headache. So just remember to include the "unique column" when using the REPLACE statement, or you'll get duplicate rows... OR use the UPDATE statement.



UPDATE STATEMENTS
UPDATE table_name SET col_B='new_data'  WHERE col_A='reference_data' ;
Will update a row's columns with the new values specified in the SET section and the row to update is specified by the WHERE section
Example:
UPDATE music SET title='Come Together' WHERE id=1;
Notice that quotes are not around the id-1. This is because you shouldn't have to use quotes when you are entering an integer.
idartistalbumtitletrackyear
1the beatlesAbbey RoadCome Together  
To update two or more columns, separate value pairs in the SET section with a comma
Example:
UPDATE music SET track='1', year='1969'  WHERE id=1;
Notice that quotes are not around the id-1. This is because you shouldn't have to use quotes when you are entering an integer.
idartistalbumtitletrackyear
1the beatlesAbbey RoadCome Together11969
It may seem frustrating to see that quotes were used around the "integers' in the SET section, but not in the WHERE section. Sometimes you may need to use them, but sometimes you do not. A llot of it depends on how the database was originally set up. If we initially set up the database where "track" and "year" did not use TEXT, but rather INT(11) we could probably get away with not using quotes around the data in the SET section. This issue also has to do with how MySQL is set up on your server.
Another thing to consider is that because we're using single quotes around the data that we are entering... what if your data contains a single quote? Such as:
UPDATE music SET title='Mike's Song' WHERE id=1;
As you can see, now there are three single quotes, and chances are MySQL will return an error. There are two things you can try.
1. Escape the single quote in the data as:
UPDATE music SET title='Mike\'s Song' WHERE id=1;
1. Use tricky quotes:
UPDATE music SET title=`Mike's Song` WHERE id=1;
Tricky quotes are just above the "tab" button on your keyboard.



SELECT STATEMENTS
SELECT * FROM table_name WHERE 1 ;
Returns all rows and all columns from table_name.. All rows is specified by the * (asterisk) symbol between SELECT and FROM.
Example:
SELECT * FROM music WHERE 1;
Returns:
idartistalbumtitletrackyear
1the beatlesAbbey RoadCome Together11969
2the beatlesAbbey RoadSomething21969
3the beatlesAbbey RoadMaxwell's Silver Hammer31969
4the beatlesAbbey RoadOh! Darling41969
5the beatlesAbbey RoadOctopus's Garden51969
6the beatlesAbbey RoadI Want You (She's So Heavy)61969
7the beatlesAbbey RoadHere Comes the Sun71969
8the beatlesAbbey RoadBecause81969
9the beatlesAbbey RoadYou Never Give Me Your Money91969
10the beatlesAbbey RoadSun King101969
11the beatlesAbbey RoadMean Mr. Mustard111969
12the beatlesAbbey RoadPolythene Pam121969
13the beatlesAbbey RoadShe Came in Through the Bathroom Window131969
14the beatlesAbbey RoadGolden Slumbers141969
15the beatlesAbbey RoadCarry That Weight151969
16the beatlesAbbey RoadThe End161969
17the beatlesAbbey RoadHer Majesty171969
18beatlesRubber SoulDrive My Car11969
19beatlesRubber SoulNorwegian Wood (This Bird Has Flown)21969
20beatlesRubber SoulYou Won't See Me31969
21beatlesRubber SoulNowhere Man41969
22beatlesRubber SoulThink for Yourself51969
23beatlesRubber SoulThe Word61969
24beatlesRubber SoulMichelle71969
25beatlesRubber SoulWhat Goes On81969
26beatlesRubber SoulGirl91969
27beatlesRubber SoulI'm Looking Through You101969
28beatlesRubber SoulIn My Life111969
29beatlesRubber SoulWait121969
30beatlesRubber SoulIf I Needed Someone131969
31beatlesRubber SoulRun for Your Life141969

SELECT * FROM table_name WHERE column_name IN ('data_1','data_2') ;
Returns all rows* and all columns that have matching data specified in the IN section. The data defined in the IN section must match exactly.
NOTE: All rows is specified by the * symbol between SELECT and FROM sections.
Example:
SELECT * FROM music WHERE track IN ('1','2');
Returns:
idartistalbumtitletrackyear
1the beatlesAbbey RoadCome Together11969
2the beatlesAbbey RoadSomething21969
18beatlesRubber SoulDrive My Car11969
19beatlesRubber SoulNorwegian Wood (This Bird Has Flown)21969



SELECT col_A_name, col_B_name FROM table_name WHERE col_B_name='search_term' ;
Returns only two columns from the matching row. The data defined int eh WHERE section must match exactly.
Example:
SELECT artist, album FROM music WHERE title='You Never Give Me Your Money';
Returns:
artistalbum
the beatlesAbbey Road

SELECT col_A_name, col_B_name FROM table_name WHERE col_B_name IN ('search_term_1','search_term_2') ;
Returns only two columns that match the data defined in the IN section.
Example:
SELECT title, album FROM music WHERE title='You Won\'t See Me', 'I\'m Looking Through You';
NOTE: Notice that the single quotes are "escaped" by using a back slash within the data. You may want to try using tricky quotes instead as:
SELECT album, date FROM music WHERE title=`You Won't See Me`, `I'm Looking Through You`;
Returns:
albumdate
Rubber Soul1969
Rubber Soul1969

SELECT * FROM table_name WHERE col_A_name RLIKE ('search_termA') OR col_B_name RLIKE ('search_termB') ;
This example will search through your table for matching words.
Will return all rows that contain either search_termA OR search_term_B -- the search term can be a single word or a phrase.
NOTE: This options is based on having MySQL set up to take advantage of FULLTEXT, which was established when we set up the table. Also note that some MySQL configurations will not return matches on words that are shorter than 4 characters. So for this example, MySQL may not return anything. So you may want to experiment with this kind of search with a larger word such as "Yourself."
NOTE 2: This is actually the old way to do things, but more reliable than the new method. The new method uses the MATCH statement, but requires some additional configuration on your MySQL installation / server. The method offered here is probably a little more reliable as of this writing.
NOTE TO SELF: If the year is 2013, you may have better luck using the MATCH statement. I'm sure I'll be dead by then, so your on your own. Check with the MySQL documentation.
Example:
SELECT * FROM music WHERE title RLIKE ('you') OR title RLIKE ('mustard') ;
Returns:
idartistalbumtitletrackyear
6the beatlesAbbey RoadI Want You (She's So Heavy)61969
9the beatlesAbbey RoadYou Never Give Me Your Money91969
20beatlesRubber SoulYou Won't See Me31969
27beatlesRubber SoulI'm Looking Through You101969
11the beatlesAbbey RoadMean Mr. Mustard111969


SELECT * FROM table_name WHERE col_A_name RLIKE ('search_termA') AND col_B_name RLIKE ('search_termB') ;
This example will search through your table for matching words.
Will return all rows that contain either search_termA OR search_term_B -- the search terms can be a single word or a phrase.

Example:
SELECT * FROM music WHERE title RLIKE ('you') AND title RLIKE ('me') ;
Returns:
idartistalbumtitletrackyear
9the beatlesAbbey RoadYou Never Give Me Your Money91969
20beatlesRubber SoulYou Won't See Me31969
NOTE: In this example, we are only searching within the same column, you can search different columns if you want, and you can also add more OR or AND statements, or you can mix and match OR and AND statements to refine the search..
Example:
SELECT * FROM music WHERE title RLIKE ('you') OR artist RLIKE ('the') AND track RLIKE ('1969');
SELECT * FROM table_name WHERE 1 ORDER BY RAND() LIMIT number_of_rows_to_return ;
Will select random rows and limit the number of rows returned to the number specified in the LIMIT section.
Example:
SELECT * FROM music WHERE 1 ORDER BY RAND() LIMIT 3;
Returns something similar to :
(Can't be too sure here, since MySQL is picking rows randomly, but you can see that only three rows are returned :)
idartistalbumtitletrackyear
25beatlesRubber SoulWhat Goes On81969
13the beatlesAbbey RoadShe Came in Through the Bathroom Window131969
5the beatlesAbbey RoadOctopus's Garden51969

SELECT DISTINCT col_A FROM table_name WHERE 1;
Will only return rows that have unique data in the column specified.
Perhaps the best way to explain this is through example.
If we do something like this:
SELECT DISTINCT year FROM music WHERE 1;
... we will only get one row and one column because all the rows in our table have "1969" set as the year. MySQL will pick the first unique row, all subsequent rows that have the same data in the specified column will not be returned, because the data in the other rows is not unique.
year
1969
If we do something like:
SELECT DISTINCT artist FROM music WHERE 1;
We'll only get two rows:
artist
the beatles
beatles
... because the for the first album, we used "the beatles" whereas the second album, we just used "beatles" -- so only the first row that MySQL encounters with unique data in the "artist" column will be returned... because the rest of the rows are not unique.

SELECT DISTINCT col_A,col_B FROM table_name WHERE 1 ORDER BY RAND() LIMIT number_of_rows_to_return ;
You can add the DISTINCT statement to any statement. So that only unique items are returned. Or another way to think about it is that no duplicate rows are returned. For example, if you used the RLIKE statement to look for a couple key words, MySQL may return the dame row two times, you can use the DISTINCT statement to filter only unique rows.
Example:
SELECT DISTINCT artist,album FROM music WHERE 1 ORDER BY RAND() LIMIT 5;
This example is deviating slightly from out standard "All beatles" table, in this example, we'll have to imaging that our tables contains a whole bunch of artists and albums.
So, with your imagination flying, the example above would return 5  rows, where both the artist and album are unique.
artistalbum
beatlesRubber Soul
the beatlesAbbey Road
Ray LamontagneTrouble
The Tragically HipWheat kings
The PoliceOutlandos d'Amour
NOTE: Yes, both "the beatles" and "beatles" albums would be returned because the entire data contained in the "artist" column is unique for both of these albums.
SELECT MAX(col_A) FROM table_name ;
Will return the highest value for that column. Usually you want to sue this on a column that references a number. For example, let's say you wanted to find the highest ID on your table so that you could determine the next ID to use fro a new item that you are inserting into your tables. You could use the MAX statement to find the largest ID number, then when you INSERT your new item, you can simply bump up the number by one.
Example:
SELECT MAX(id) FROM music;
Returns:
id
31
... because the highest ID in our table is 31. e.g. there are 31 items in our table as defined by the ID column.
If we ran something like:
SELECT MAX(track) FROM music ;
... we'd get:
track
17
... because 17 is the highest number in the "track" column



DELETE STATEMENTS
DELETE FROM table_name WHERE column_name='search_data';
Will remove rows that match the search data defined in the WHERE section
Example:
DELETE FROM music WHERE artist='the beatles';
Will remove all rows that have "the beatles" defined in the "artist" column.
DELETE FROM table_name WHERE column_name IN ('search_data_A', 'search_data_B');
Will remove all rows that match the search data defined in the IN section.
Example:
DELETE FROM music WHERE artist IN ('the beatles', 'beatles');
... will remove all the items from our example table

Monday, March 28, 2011

How to copy export mySQL user priviledges

 Copy/Export MySQL User Priviledges

I’m often asked how to copy or export MySQL Users from one machine to another. The following SQL query will show your users:
SELECT DISTINCT CONCAT (‘show grants for `’, user, ‘`@`’, host, ‘`;’) AS query FROM mysql.user;
In my case on my test server, this shows:
SHOW GRANTS FOR ‘root’@’127.0.0.1′;
SHOW GRANTS FOR ‘debian-sys-maint’@'localhost’;
SHOW GRANTS FOR ‘root’@'localhost’;
Now, I’ll need to execute each one of these as separate statements. The output of SHOW GRANTS FOR ‘root’@'localhost’; is:
GRANT ALL PRIVILEGES ON *.* TO ‘root’@'localhost’ IDENTIFIED BY PASSWORD ‘*XXX…XXX’ WITH GRANT OPTION;
Copy and paste each ‘GRANT’ statement to your new SQL server, with the hashed password intact and you should be ready to go.

Sunday, March 20, 2011

Lowercase and Uppercase in Perl

Lowercase and Uppercase in Perl
All the programming languages has functions/methods to convert case to upper or lowercase.
Perl has lc for lowercase, uc for uppercase, ucfirst for first character to be converted to uppercase. etc...
Let see some examples :
#!/usr/bin/perl -w
$name = "firat serkan atagun";
$ucname= uc ($name);
$lcname = lc ($name);
$ucfname = ucfirst ($name);

print $name."\n".$ucname."\n".$lcname."\n".$ucfname;

The output will be :
firat serkan atagun
firat serkan atagun
FIRAT SERKAN ATAGUN
Firat serkan atagun


uc
function is used for converting strings in to uppercase.
lc function is used for converting strings in to lowercase
ucfirst function is used for converting the first chracter of the string to uppercase.

Tuesday, March 15, 2011

Using the Perl rand() function

Using the Perl rand() function

Introduction

The rand() function is used to generate random numbers. By default it generates a number between 0 and 1, however you can pass it a maximum and it will generate numbers between 0 and that number.

Example 1. Between 0 and 1

To generate a random decimal number between 0 and 1, use rand() without any parameters.
#!/usr/bin/perl
  use strict;
  use warnings;

  my $random_number = rand();

  print $random_number . "\n";
This will give you a random number, like:
0.521563085335405

Example 2. A bigger range of numbers

Quite often you don't want a number between 0 and 1, but you want a bigger range of numbers. If you pass rand() a maximum, it will return a decimal number between 0 and that number. Our example below generates a random number between 0 and 100.
#!/usr/bin/perl
  use strict;
  use warnings;

  my $range = 100;

  my $random_number = rand($range);

  print $random_number . "\n";
The program will produce something like:
34.0500569277541

Example 3. A random integer

To generate a random integer, convert the output from rand to an integer, as follows:
#!/usr/bin/perl
  use strict;
  use warnings;

  my $range = 100;

  my $random_number = int(rand($range));

  print $random_number . "\n";
This program gives you an integer from 0 to 99 inclusive:
68

Example 4. With an offset

To generate a random number between, for example, 100 and 150, simply work out the range and add the minimum value to your random number.
#!/usr/bin/perl
  use strict;
  use warnings;

  my $range = 50;
  my $minimum = 100;

  my $random_number = int(rand($range)) + $minimum;

  print $random_number . "\n";

How to connect in Perl to MySQL database

 How to connect in Perl to MySQL database

#!/usr/bin/perl

# PERL MODULE
use Mysql;

# MYSQL CONFIG VARIABLES
# MYSQL CONFIG VARIABLES
$host = "localhost";
$database = "store";
$tablename = "inventory";
$user = "username";
$pw = "password";

# PERL CONNECT()
$connect = Mysql->connect($host, $database, $user, $pw);

# SELECT DB
$connect->selectdb($database);

# LISTTABLES()
@tables = $db->listtables;

# PRINT EACH TABLE NAME
@tables = $connect->listtables;
foreach $table (@tables) {
    print "$table
";
}


# DEFINE A MySQL QUERY
$sqlstr = "SELECT * FROM $tablename";
$sqlstr="select id, captcha_question from captcha where id='$captch_id' LIMIT 1";

# EXECUTE THE QUERY
$execute = $connect->query($sqlstr);

$rownumber = $execute->numrows();
$fieldnumber = $execute->numfields();

# PRINT THE RESULTS
print $rownumber."
";
print $fieldnumber."
";

# HTML TABLE
print "
"; # FETCHROW ARRAY while (@results = $execute->fetchrow()) {     print " "; } print "
id product quantity
"
    .$results[0]."
"
    .$results[1]."
"
    .$results[2]."
";



Monday, February 21, 2011

Advanced email in PHP

Advanced email in PHP


Note that just because an email message could be sent doesn't guarantee it will arrive at its destination. An email address can be valid (i.e. correctly formed) but not actually exist. For instance, you can successfully send a message to nonexistant.user@hotmail.com -- that is, the mail function will return true -- but the message will bounce because no such user exists. PHP provides no built-in means of detecting when this happens.
When you want to send a message to multiple recipients, you can just list their email addresses one after another, separated by commas, in the first parameter. For example:
mail('recipient1@some.net, recipient2@some.net',
    'An email to two people', 'Message goes here.');
?>
That about covers the basics of the mail function; now let's really get fancy and explore mail headers and what we can do with them!
HTML Email and Other Headers
So now you can send email from your PHP scripts. How exciting! Although I'm sure you're already drunk with power, would you like to know how to send HTML email too? Of course you would!
To understand HTML email, first you need to understand mail headers. Any given email message is made up of two parts: the headers and the message body. Here's how a simple message looks when your email program fetches it from your ISP:
Return-Path:   
Delivered-To: you@some.net   
Received: ...several lines like this...   
From: Sender   
To: You   
Subject: A Simple Message   
Date: Mon, 11 Feb 2002 16:08:19 -0500   
Organization: Sender's Company   
X-Mailer: Microsoft Outlook, Build 10.0.2616   
  
Hi there! Is this thing on?
Everything up to the blank line makes up the headers for this message. In actual fact, most email messages will have many more header lines than this; however, to keep our focus I trimmed this example down to the bare essentials.
As you can see, every line of the headers begins with the name of the header (From:, To:, Subject:, Date:, etc.), followed by some value. Most headers are standardized, and have a specific meaning, either to your mail program or to the mail servers that were responsible for getting the message to you. Non-standard headers exist as well, and they all begin with X- (e.g. X-Mailer:, a non-standard header, often appears to indicate the program that was used to send the message).
NOTE: If a header's value needs more than one line, additional lines should begin with a space. We'll see an example of this in the next section.
As soon as your mail program gets to a blank line, it knows the headers are over and the rest of the email is the message body, which it should display. In this case, the message body is the last line above.
PHP's mail function lets you specify your own headers, which it adds to those it generates automatically to get your message where it needs to go. For example, the following call to the mail function will add an X-Mailer: header to the outgoing message, identifying PHP 4.x as the sending program:

mail('recipient@some.net', 'Subject', 'Your message here.',   
    'X-Mailer: PHP 4.x');   
?>
This optional fourth parameter is most often used to specify a 'from' address other than the default specified in php.ini. Let's add a From: header to do just that:

mail('recipient@some.net', 'Subject', 'Your message here.',   
    "From: sender@some.net\nX-Mailer: PHP 4.x");   
?>
Note that since headers each appear on a single line, we must separate our two headers with a new line character (\n), which means I need to use double quotes around the header string (single quoted strings don't recognize special character codes like \n).
Additional headers also let you assign names to email addresses by specifying them in the format name . Here's our example again, but this time with names "The Sender" and "The Receiver" attached to the relevant addresses:

mail('recipient@some.net', 'Subject', 'Your message here.',   
    "To: The Receiver \n" .   
    "From: The Sender \n" .   
    "X-Mailer: PHP 4.x");   
?>
Notice that to do this, we've had to specify the To: header manually, since the first parameter of PHP's mail function doesn't support this more advanced address format. We must still list all the recipients of the message as bare email addresses in the first parameter, however.
The cc: and Bcc: headers provide the ability to send carbon copies and blind carbon copies of a message as well:

mail('recipient@some.net, someone@some.net, metoo@some.net',   
    'Subject', 'Your message here.',   
    "To: The Receiver \n" .   
    "From: The Sender \n" .   
    "cc: Interested \n" .   
    "Bcc: Me Too \n" .   
    "X-Mailer: PHP 4.x");   
?>
See how the email addresses of all recipients, be they actual addressees (To), carbon copies (Cc), or blind carbon copies (Bcc) are listed in the first parameter? This isn't documented anywhere, but in my testing I've found that it's absolutely vital if you want the message to get to everyone it's supposed to, especially on Windows servers where PHP's mail function is especially sensitive.
BUG ALERT: There are two apparent bugs in the PHP mail function, which I've observed as recently as PHP 4.1.0, that you must be aware of. First, the Cc:header must be typed 'cc:' (as above) or 'CC:', that is, in either all-caps or low-caps. The mixed case version (Cc:) should work, but it doesn't. Secondly, on Windows servers, the Bcc: header does not work as it should. Bcc: recipients do get a copy of the message, but the Bcc: headers are not stripped out of the message during sending, so recipients can see the Bcc: receivers by looking at those lines in the headers.
What does all this have to do with HTML email, you ask? Well, a few special headers will cause the message body to be interpreted as HTML by email clients that support it:

mail('recipient@some.net', 'Subject',   
    '
Your message here.
',   
    "To: The Receiver \n" .   
    "From: The Sender \n" .   
    "MIME-Version: 1.0\n" .   
    "Content-type: text/html; charset=iso-8859-1");   
?>
Note both the message in HTML format as well as the two new headers: Mime-Version: and Content-type:.
The MIME-Version: header indicates that the standard extended mail format will be used (MIME stands for Multipurpose Internet Mail Extensions), which allows for messages to be sent content types other than plain text. The Content-type: header then specifies that the message with contain HTML (and sets the character set to the standard for that format).
Mixed Messages
In life, it's rarely good to send mixed messages. At least, that's what my last girlfriend told me! When it comes to email, however, mixed messages offer a whole lot of power. A single email message can contain both text and HTML versions of your message. That makes it viewable in most any email program, and you don't sacrifice the power of HTML formatting for readers that are appropriately equipped.
Be aware that mixed messages have their weaknesses. First of all, since you're sending two versions of your message, the message will typically be a lot larger than it would be if you sent just one format or the other. Also note that old email programs that don't recognize the mixed message format may display both versions of the message as file attachments (one text, the other HTML).
Let's look at a simple example of a mixed email message, and then look at the PHP code to send it:
Date: Mon, 11 Feb 2002 16:08:19 -0500  
To: The Receiver  
From: The Sender  
Subject: A simple mixed message  
MIME-Version: 1.0  
Content-Type: multipart/alternative;  
             boundary="==Multipart_Boundary_xc75j85x"  
 
This is a multi-part message in MIME format.  
 
--==Multipart_Boundary_xc75j85x  
Content-Type: text/plain; charset="iso-8859-1"  
Content-Transfer-Encoding: 7bit  
 
This is the text portion of the mixed message.  
 
--==Multipart_Boundary_xc75j85x  
Content-Type: text/html; charset="iso-8859-1"  
Content-Transfer-Encoding: 7bit  
 
 
   
This is the HTML portion of the mixed message.
 
 
 
 
--==Multipart_Boundary_xc75j85x--
After the initial, standard headers at the top of the message, we have the MIME-Version: 1.0 header that enables the advanced email features we need. The Content-Type: header is where things start to get funky:
Content-Type: multipart/alternative;  
             boundary="==Multipart_Boundary_xxc75885"
We specify a content type of multipart/alternative, which is the special type that allows us to send a message with two or more alternative versions of the message (from which the recipient's email program will pick the most suitable for display). In addition, we use the Content-Type header to set a boundary string.
To keep the header lines short, this part of the header appears on a second line (as mentioned in a note above, the second line must begin with one or more spaces to indicate that it's a continuation of the previous line).
In this case, I chose "==Multipart_Boundary_xc75j85x" as the boundary string. There is no special significance to this string, other than it is unlikely to appear as part of the message itself. I used characters like equals signs and underscores, and semi-random strings of letters and numbers to help ensure this. We then use this string to divide up our message into parts.
The text "This is a multi-part message in MIME format." is included for the benefit of older mail programs, so that the user has some idea of why the email may not appear quite as expected. With that disclaimer out of the way, we use our boundary to mark the start of the first part of our message:
--==Multipart_Boundary_xc75j85x  
Content-Type: text/plain; charset="iso-8859-1"  
Content-Transfer-Encoding: 7bit  
 
This is the text portion of the mixed message.
Notice that we add two dashes (--) to the beginning of the boundary string when we actually use it.
After the first boundary, we begin the text version of the message. Each part of the message begins with a couple of headers to indicate its content type and encoding. In the case of the text part, the content type is text/plain (with the standard character set, iso-8859-1), and the encoding is 7bit (plain ASCII text). A blank line marks the end of the headers, which are followed by the message body.
The HTML version then follows:
--==Multipart_Boundary_xc75j85x  
Content-Type: text/html; charset="iso-8859-1"  
Content-Transfer-Encoding: 7bit  
 
 
   
This is the HTML portion of the mixed message.
 
 
The headers are almost identical to the text part, but this time we specify text/html as the content type. After the body of the HTML document, all that remains is the closing boundary, to which we add an extra two dashes on the end to mark the end of the message:
--==Multipart_Boundary_xc75j85x--
As you can see, mixed messages may look complicated, but they're actually pretty simple when you take a closer look. The only tricky part from the standpoint of a PHP developer who wants to send a message like this is the task of generating a boundary string.
Here's how I like to do it:
$semi_rand = md5(time());  
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";  
?>
We take the current Unix timestamp (the number of seconds since January 1, 1970), as given by time(), and use the MD5 algorithm to convert it to a semi-random string. This string is then used to make up part of the boundary string. Feel free to use whatever method you like to generate your boundary strings.
With all this in mind, you should be well-equipped to generate mixed messages in PHP. Since mixed messages are relatively uncommon, I'll leave the actual code for you to write as an exercise. This business of splitting up messages into parts with a boundary string is important, however, when it comes to file attachments -- the subject of our final section.
File Attachments
File attachments work just like mixed email, except that a different content type is used for the message as a whole (multipart/mixed instead ofmultipart/alternative), and there's a new Content-Disposition header that tells the email client how to handle each part of the message.
Let's write a PHP script that processes a form submission that contains an email message to be sent, possibly with a file attachment, and sends it out. I'll talk you through it line by line so that by the end you'll not only have a useful snippet of PHP code, but also an understanding of how file attachments work. You can download the script [2](and the form for it) if you want to try it out for yourself.
First, we grab the submitted values and place them in PHP variables. Most people have their servers set up to create global variables for submitted values automatically, but as of PHP 4.1 this is no longer the default, so we do it by hand just in case. Since we want to accept file attachments, it's safe to assume that the form will be submitted with a POST request:

// Read POST request params into global vars     
$to      = $_POST['to'];     
$from    = $_POST['from'];     
$subject = $_POST['subject'];     
$message = $_POST['message'];
File uploads in PHP 4.1 are placed in a special $_FILES array, so we fetch the values we need out of it:
// Obtain file upload vars     
$fileatt      = $_FILES['fileatt']['tmp_name'];     
$fileatt_type = $_FILES['fileatt']['type'];     
$fileatt_name = $_FILES['fileatt']['name'];
For the sake of brevity, we'll assume that the required parameters ($to and $from) now have valid values (email addresses) in them. Normally we would check their format with regular expressions.
Next, we use the $from value to begin building the extra headers for the email:
$headers = "From: $from";
Next we check the $fileatt variable, which may or may not contain the path and filename to an uploaded file attachment. We use PHP's is_uploaded_file function to find out:
if (is_uploaded_file($fileatt)) {     
 // Read the file to be attached ('rb' = read binary)     
 $file = fopen($fileatt,'rb');     
 $data = fread($file,filesize($fileatt));     
 fclose($file);
Having read in the data for the file attachment, we need to set up the message headers to send a multipart/mixed message:
 // Generate a boundary string     
 $semi_rand = md5(time());     
 $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";     
      
 // Add the headers for a file attachment     
 $headers .= "\nMIME-Version: 1.0\n" .     
             "Content-Type: multipart/mixed;\n" .     
             " boundary=\"{$mime_boundary}\"";
Now for the message body itself. This works just as we saw for the text part of a mixed message in the previous section:
 // Add a multipart boundary above the plain message     
 $message = "This is a multi-part message in MIME format.\n\n" .     
            "--{$mime_boundary}\n" .     
            "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .     
            "Content-Transfer-Encoding: 7bit\n\n" .     
            $message . "\n\n";
Now, to allow for binary file types, we need to use Base64 encoding to convert the (possibly binary) file attachment data to a text-only format suitable for sending by email. All email programs in popular use support Base64 encoding of file attachments, so this is the best way to go. Fortunately, PHP provides a function for Base64 encoding:
 // Base64 encode the file data     
 $data = chunk_split(base64_encode($data));
We now have everything we need to write the portion of the message that contains the file attachment. Here's the code:
 // Add file attachment to the message     
 $message .= "--{$mime_boundary}\n" .     
             "Content-Type: {$fileatt_type};\n" .     
             " name=\"{$fileatt_name}\"\n" .     
             "Content-Disposition: attachment;\n" .     
             " filename=\"{$fileatt_name}\"\n" .     
             "Content-Transfer-Encoding: base64\n\n" .     
             $data . "\n\n" .     
             "--{$mime_boundary}--\n";     
}
That completes the modifications necessary to accommodate a file attachment. We can now send the message with a quick call to mail:
// Send the message     
$ok = @mail($to, $subject, $message, $headers);     
if ($ok) {     
 echo "
Mail sent! Yay PHP!
";     
} else {     
 echo "Mail could not be sent. Sorry!
";     
}     
?>
And that's how we send emails with file attachments in PHP!
Summary
In this article, you probably learned a lot more about email than you ever wanted to know. That intimate knowledge about what makes email tick allowed us to do some pretty special things with PHP's deceptively simple mail function.
  • we learned how to manipulate email headers to play with the recipients of an email
  • we saw the headers that permit you to send HTML email
  • we combined the HTML and text versions of an email into a single, mixed message
and finally...
  • we brought it all together with a couple of new tricks to send email messages with file attachments.
If you're in the mood for a challenge, try expanding the file attachment script in this article to allow for multiple file attachments, or to allow for an HTML message. If you're an object oriented PHP type, try developing a class that encapsulates the functionality we explored in this article (there are a number of such scripts available on the Internet if you're after something ready-made).
If you're really interested, check out the RFC for MIME extensions (RFC 2045 [3]) to discover everything that email is capable of, then go wild!
[1] http://phpmailer.sourceforge.net/
[2] http://www.webmasterbase.com/examples/phpemail/phpemail.zip
[3] http://www.ietf.org/rfc/rfc2045.txt

Sunday, February 20, 2011

MySQL Security Checklist

MySQL Security Checklist
   
Background
   1.Read the MySQL security guidelines at http://dev.mysql.com/doc/mysql/en/Security.html.
   2.Visit http://www.mysql.com/products/mysql/ often, and check for updates.
   3.Know
      your bugs! Check vulnerability databases such as SecurityFocus and ICAT
      regularly for MySQL bugs, and (if you can stand the noise levels)
      subscribe to security mailing lists such as VulnWatch, BugTraq, and the
      MySQL mailing lists.
Operating System
   1.Deploy IPTables (Linux), an IPSec filtering ruleset (Windows), or some other host-based firewall software on your MySQL servers.
   2.Use a low-privileged mysql account to run the MySQL daemon. This is the default on some platforms, but not others.
   3.Run mysqld with the --chroot option.
   4.Ensure that the MySQL user cannot access files
      outside of a limited set of directories. Specifically, the MySQL user
      should be prohibited from reading operating system configuration files.
      In some cases you might want to prevent the MySQL user from being able
      to modify the MySQL configuration files.
   5.Ensure that MySQL data files (normally residing
      beneath the MySQL data directory) cannot be read by users other than
      the root or Administrator account, and the account that MySQL is
      running under.
   6.Plaintext credentials. Ensure that no user other
      than the MySQL user can read any MySQL configuration and log files. The
      files my.cnf, my.ini, and master.info commonly have plaintext usernames
      and passwords in them, and the query log file (if present) is likely to
      contain passwords.
   7.Turn off unnecessary services or daemons.
   8.Make sure you don't have anything in your .mysql_history file.
MySQL Users
   1.Set a "strong" password for the root@localhost account.
   2.Remove all non-root MySQL users.
   3.Rename the root MySQL user to something obscure.
   4.If remote connections are enabled, specify REQUIRE SSL in the GRANT statement used to set up the user.
   5.Create a MySQL user for each web application—or
      possibly for each role within each web application. For instance, you
      might have one MySQL user that you use to update tables, and another,
      lower-privileged user that you use to "select" from tables.
   6.Ensure that MySQL users are restricted by IP
      address as well as passwords. See section 5.4 of the MySQL manual, "The
      MySQL Access Privilege System," for more information.
   7.Don't give accounts privileges that they don't absolutely need, especially File_priv, Grant_priv, and Super_priv.
   8.Never give anyone (other than root or whatever you call your root account) access to the mysql.user table.
MySQL Configuration
   1.Enable logging via the --log option.
   2.Disable the LOAD DATA LOCAL INFILE command by adding set-variable=local-infile=0 to the my.cnf file.
   3.Remove any unused UDFs
   4.If you're using only local connections, disable TCP/IP connections via the --skip-networking option.
   5.Depending on your operating system, and how your
      data directory is configured, you might want to disallow the use of
      symbolic links via the skip-symbolic-links option.
   6.Remove the default test database.
   7.Ensure MySQL traffic is encrypted.
Routine Audit
   1.Check your logs.
   2.Enumerate users and use the "show grants" statement regularly to see what privileges are granted to which users.
   3.Periodically do a quick check on password hashes.