Saturday, April 16, 2011

.htpasswd tutorial

Apache offers you to protect a folder and its subdirectories with .htaccess files. Learn how to protect your data in only two quick steps!


1. Create an .htpasswd file

Your .htpasswd file will contain the usernames and crypted password of the users who will be allowed in the directory you want to protect; htpasswd files typically look like this:

test:dGRkPurkuWmW2
foo:YXxOg72kVMUII

Blue parts represent user name, and red are password encrypted with a specific algorithm.

How to generate htpasswd file ? Just:

  1. create an empty file named .htpasswd
  2. use this online htpassword generator, type the users you want to add and copy the generated text
  3. paste the text in the empty file you created in the first step
  4. save it of course!
Voilà, your htpasswd file is ready!


2. Configure the .htaccess

Create a file named .htaccess at the root of the folder you want to protect, if it does not already exists. Paste this few lines in the file, then save it:

AuthName "Type a custom text here"
AuthType Basic
AuthUserFile /full/path/to/the/.htpasswd
AuthGroupFile /dev/null
require valid-user

You will have to specify the location of your .htpasswd file (red path) for this to work properly.



Congratulations! You just finished protecting your folder with htaccess and htpasswd files. Test it in your web browser!



Monday, April 11, 2011

PHP, MySQL and UTF-8 encoding

UTF-8 encoding allows the display of different charsets on the same Web page; using UTF-8, you can write with Latin, Chinese and Arab characters on the same page.
On the other hand, setting up a whole site to work with UTF-8 can be incredibly tedious if you do not know how to proceed. You have to configure the HTML code, PHP, Apache, and the MySQL databases you will need.
Here is a very simple tutorial to convert a whole website to UTF8 within minutes!

Apache

Send the charset in the HTTP headers; you can either add this in the .htaccess at the root of your website :

AddDefaultCharset UTF-8

Or include this in every PHP script:

header("Content-type: text/html; charset=UTF-8"); 

PHP Configuration

You will need to use multibyte string functions instead of the usual string functions (substr, etc.). You will have to edit your php.ini configuration file.

mbstring.language=UTF-8
mbstring.internal_encoding=UTF-8
mbstring.http_input=UTF-8
mbstring.http_output=UTF-8
mbstring.detect_order=
auto


Here are a the multibytes counterparts to the regular string functions:

RegularMultibyte
mailmb_send_mail
strlenmb_strlen
strposmb_strpos
strrposmb_strrpos
substrmb_substr
strtolowermb_strtolower
strtouppermb_strtoupper
substr_countmb_substr_count
eregmb_ereg
eregimb_eregi
ereg_replacemb_ereg_replace
eregi_replacemb_eregi_replace
splitmb_split
htmlentities($text)htmlentities($text, ENT_QUOTES, 'UTF-8')
htmlspecialchars($text)htmlspecialchars($text, ENT_QUOTES, 'UTF-8')
You can use the mb_convert_encoding and mb_detect_encoding functions to convert a string to its UTF-8 equivalent, which is very useful when you are using data from external files or parsed HTML pages.
Note that in multibyte regular expression, \w will match any accuentuated character, which can be very practical for words detection most non-english languages!

MySQL

When you create a database, use the following request:

CREATE DATABASE foo
CHARACTER SET
utf8 COLLATE utf8_bin;


By default, a table created in a UTF-8 database will use the UTF-8 charset. Similarly, text columns will by default inherit from the table they are in.
If you are using PHPMyAdmin, you can access those properties in the Operations tab corresponding to the MySQL database or table you want to change.
In your PHP code, don't forget this query for every connection to the database:

mysql_query("SET NAMES 'utf8'");


If are using MySQL from the command line, you have to specify the following option:
--default-character-set=utf8

HTML

You can put the following tag in the head section:

<meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
And don't forget to say form data will be sent using UTF8:

<form accept-charset="UTF-8">


Do not forget always to save your files in UTF-8 without BOM in the text editors! This is true for JavaScript, HTML, and PHP files. CSS files are less likely to contain special characters, but consistency never hurts.


Sunday, April 10, 2011

Relative to absolute URL conversion / PHP

function absolute_url($base_url, $relative_url){
 $base_url_info = parse_url($base_url);
 $base_url_path = explode("/", $base_url_info["path"]);
 $base_url_file = $base_url_path[count($base_url_path)-1];
 unset($base_url_path[count($base_url_path)-1]);
 $relative_url_path = explode("/", $relative_url);
 if (parse_url($relative_url, PHP_URL_SCHEME) != ""){
  return $relative_url;
 }else{
  switch($relative_url_path[0]){
   case ".":
    unset($relative_url_path[0]);
    return absolute_url
    ( $base_url
    , implode("/",$relative_url_path)
    );
    break;
   case "..":
    unset($relative_url_path[0]);
    return absolute_url
    (  str_replace($base_url_info["path"], "", $base_url)
     . implode("/", $base_url_path)
    , implode("/", $relative_url_path)
    );
    break;
   case "":
    return str_replace($base_url_info["path"], "", $base_url)
    .  $relative_url
    ;
    break;
   default:
    return str_replace($base_url_info["path"], "", $base_url)
    .  implode("/", $base_url_path)
    .  "/"
    .  $relative_url
    ;
    break;
  }
 }
}

Sunday, April 3, 2011