Bannière NyroBlog, par deuJeff
Image par deuJeff - ?

Tag : Travail

Howto: Transfer a website

When I wrote this post in French, I just transfered 2 of my website.

Before I started to do it, I looked for an howto like this one to be sure to forget nothing. As I didn't find it, I'm writing it for the others!


To be sure everybody understand what we're talking about here, let me explain the problem.
When we want publish a website, we need 2 things: a domain name and a server. The server contains all the websites files (HTML, PHP, Images, etc...)

The domain name is the URL you use to access to our website; nyrodev.info for mu blog for example. This domain name should redirect your visitor to your server to access the files. Your server is reachable through an IP address. The link between your domain name et the IP address is done with the DNS. DNS are online computers which everybody can ask to know the IP address related to a domain name.

The server is here to stock your files. It's basically a computer with some special softwares. A server can contains multiple websites. That mean more than 1 domain name can point to the same server. The choice of the right files in the server will be done via many softwares well parametered for each website. I won't speak about this part here. Most of time the dedicated server provided administration tools to configure automatically every software during the website creation.

My goal is to change the server. I have to transfer the files (images, pages html, etc...), the eventual database and more. At the end, we'll change the IP address where point the domain name to use the new server.

Next you'll see some actions to do that in the good order to don't have problems, by loosing at least at visitors during the transfer time.

In the next of this post:
  • domain.com: The domain name to transfer
  • Server A: old server, where the website is at the beginning
  • Server B: new server

1. Prepare the Basics

The first step of this transfer is to prepare the server B to receive the new website. Create in your administration to get some disk space to receive your files. You can give him directly domain.com to save time. Don't forget to set the mail parameters, FTP, etc... It doesn't cause any troubles[1] as domain.com still points on the old server.

Then you can create the email and email alias that you have on the server A to don't lose any email. Regarding the databases, create them without the tables. This will be done in an other part.

2. File Transfer

For now we can transfer the static files. Don't transfer now the uploaded or dynamic files because the current website is still available in the old server, and some upload can still occurs.

If you have a SSH access on the server A, why don't create a tar.gz archive to group all the files in one compressed, to gain some time?
Go to the directory containing your files, and to create the archive:
Retrieve the file by FTP and send it to the server B, where you should also have a SSH access. In the directory where you uploaded this archive, decompress the file:
tar xvzf DIRECTORY.tar.gz
These files will probably contains some database configurations, .htaccess files with absolute path, etc... Many configurations which will be probably different in the new server. Edit them now.

If you don't have a SSH access, simply retrieve normally your files by FTP. You can make the change in the configurations files before transfer them..

Moreover don't forget about the chmod of the directories where your script has to write some files.

Right now, your website should be ready to use in the new server by simply adding the database content.

3. Temporary Website Closure

To be sure to loose nothing during the transfer, we'll disable the whole website in the server A, just before transferring the database and the dynamic files.

We'll start by closing the whole website by simply using one .htaccess file, denying everything and redirecting to a single text page to explain about the moving. The .htaccess is:
Deny from all

    Allow from all

ErrorDocument 403 http://www.domain.com/transfer.php
And you can create the file transfer.php to explain it's temporary and won't during more than 24 hours.

4. Transfer the Last Elements

Once the website is close, we can transfer the dynamic files. You're sure they won't be new in the old server. You can use the same technique used in the 2nd step.

Now the Database transfer.

If you don't have a SSH access, simply use phpMyAdmin to export your database from the server A to import it in the server B.

If you have a SSH access, it could be better to proceed differently for 2 reasons: the phpMyAdmin export is sometimes buggy, and if your database is too big, phpMyAdmin will give you some timeout errors.

We need only 2 command lines. The first to export the database in a text file in the server A:
mysqldump --host=localhost --user=USER --password=PASS --add-drop-table DATABASENAME > base.sql
Then transfer this base.sql file to the server B and execute this command:
mysql -h localhost -u USER --password=PASS DATABASENAME < base.sql
Your website should be now totally ready in the server B. If you can test it to be sure everything is OK before the final transfer.

5. Edit the DNS

Finally you can do the modification in your domain name to point it to the server B, by editing the IP address in your DNS. And wait for testing. If you want test quicker if everything is good, I strongly recomand to use the DNS server from Open DNS on your computer —why not keep them later?. The gain with Open DNS is they are very fast to update, and you can force it to refresh the cache. Do it for your domain name 15 to 20 minutes later your change.

If they're going to the new IP, refresh your DNS on your computer. For Windows: Start > Execute > cmd. Then type:
ipconfig /flushdns

And finally test on your favorite bowser after clearing your cache too. It should be OK.

During the next few days, you have to retrieve your emails in the both server as some DNS will not be up to date everywhere.

Voilà, I hope this howto will be usefull for you. For sure you have different solution for every step. I simply showed which I used and they worked perfectly for me.
If you have any suggestions, remarks, feedback, don't hesitate to comment here!

Thanks to Niko who advised me during my transfers.

[1] : The only problem you can have —and I experienced— is: If in the server B you have some website or anything else which want send email to domain.com. Actually the server B thought domain.com is hosted by itself. That mean it don't search for the IP where to send the email because it is itself! It's not really a problem if you know that. As you'll finally retrieve your emails in the server B, you'll finally get these emails. Use simply the webmail to check during the transfer.

Version Française de ce billet

Howto update a website to reload the page through Ajax

I deployed in a website an Ajax reload for every pages. This website use a small audio player and the sound was stopped each time a new page was requested. That wasn't so graceful. The result now is exactly the same website reloading the pages through Ajax, without changing accessibility: I didn't modify ehe HTML source.

I did it quickly bu using jQuery. As this method could be useful for others, I will describe step by step how I made it.
The website which I worked on was pretty small: Amandine Café. Nothing better to train himself in a small example and then apply it in big projects

Current Website Analysis

The first thing to do is to navigate in the whole website to deduct the fixed zone and dynamic zone to reload. You have to identify which links will reload which zones, and what will be happen regarding the reloaded page. For instance you have to worry about the possible javascript control which could be a problem. Write down them and we'll take care of that at the end.
This part was pretty fast for me as the website don't use sub navigation and contains only 5 pages. I found 2 zones delimited by 3 div identified with the id header, page, and footer. Only the div#page vary regarding the pages. That will be only this content that I will update with Ajax.

PHP Side Preparation

Before dealing with the javascript code, we'll update the PHP code of our pages. When an Ajax load is made, we won't reload the full page, but only the content to update. For more simplicity and flexibility, we'll use the same URL than the real links. That's mean we have to be able to provide only the requested content in PHP, without the head page, or the fixed div. If you don't want –can't– do that, take a look to the little bonus at the bottom of this post...
The easiest way to do that is to put the fix part in separate files, named randomly header.php and footer.php. These files will be include in every pages; that's may already the case, using the include function. The thing is now to don't show these parts when the request is an Ajax request. Pretty easy, we'll write a PHP function: isAjax. The file includes will occur only if isAjax is false. In others words:
if (!isAjax())

The isAjax function is, as you can see, very very hard:
function isAjax() {
    return array_key_exists('HTTP_X_REQUESTED_WITH', $_SERVER);
Actually jQuery –also Prototype, I don't know about the others– add a HTTP_X_REQUESTED_WITH element in the request that you can retrieve in the $_SERVER global variable.
We're done with the PHP preparation pages, which are now able to provide light version of your pages especially for the Ajax request.

Javascript Integration

We're moving now on the real Ajax request programmation. To keep the website totally accessible, which will works exactly in the same way without Javascript, We won't modify any current HTML sources. We'll just include jQuery in all the pages and one other javascript file containing the upcoming code.
The script schema will be:
  1. On the page load, intercept all links pointed to .php pages
  2. Add an onclick function to make the Ajax request and stop the page change
  3. On the click, call the page through Ajax with the same URL and then integrate the result where we want.
  4. Stop the click propagation
With jQuery, we're writing that very simply:
$(function() {
    $('a[href*=.php]').click(function() {
return false;
Very short, isn't it?
Of course this code won't work perfectly in every case. In my example I have to update the body id according to the requested page; that's mean the navigation bar link will going brown as expected by the graphic designer. To do so we can use the third parameter of the load function which will be called once the Ajax load is done. Here is how looks like my function:
$(function() {
    $('a[href*=.php]').click(function() {
        $('#page').load(this.href, null, function() {
        $('body').attr('id', this.href.substring(this.href.indexOf('.')));
return false;
If you read the code in the live site, that's not at all like that because I occured other problems.

Little Bonus

If you can't–don't want– deploy the PHP cutting for the Ajax request, don't stop right now! Firstly it could be possible to reload the whole page. This solution will be very sad and you'll probably occurs many others problems.
jQuery will help. Indeed, in the URL provided to the load function, you can specifiy a HTML selector to filter the result and show only the part you want to.
In my example the call could become simply:
$('#page').load(this.href+' #page');

I dissuade to use this technique as that's mean you will reload the whole page. So we loose the Ajax utility to reduce data transfer between clients and server.

If it's not that simple?

Give proof of imagination!
As I already thought about this question, I'll give you some example soon. This post is yet consistent and you can start to enjoy. Finally I hope you won't wait me to do that... Or if you have a website where do you want deploy this kind of solution, let me know and I will use it as my example.

Express Yourself!

How did you find this post? Interesting, boring, totally useless, can do better?
Do you have some questions? Suggestions?
Did you read some English mistakes?
Use the comments!

Version française de ce billet

Backup and Restore MySQL Database through command line

When you want backup a MySQL database, you don't have many solutions.

You can use PhpMyAdmin to backup the database but you will figured out that the export doesn't work so good: there is some information lacks like the Foreign key or other things like that. You'll also occur timeout problem when trying to backup -and restore- huge database.

You can also read a fex pages in the MySQL doc and you'll find Bash command to do exactly what you want. In only 1 line you'll be able to backup you're whole database in one text file.

Here is the backup command:

mysqldump --user=USER --password=PASS --add-drop-table DATABASENAME

This command show you all the MySQL query to execute to retrieve your entire database: the CREATE instructions, adding the constraints if needed and of course, the INSERT lines. The option --add-drop-table add the instruction to delete the table just before their creation: very useful to don't worry about cleaning the database before the backup.

If you want save more than one database, you have to replace DATABASENAME by --databases DATABASENAME1 DATABASENAME2 DATABASENAME3.

And if you want save all the database in you server, you can use --all-databases instead. Be sure the user used in the command line have access to all the databases you want back up.

Right now the command show you the query. Even if you're Flash Gordon, you can't do anything with that. A simple pipe to a file will save everything for you:

mysqldump --user=USER --password=PASS --add-drop-table DATABASENAME > DUMP.sql

To restore the database -or create it in an other server-, you have simply to upload the file saved just before and run this command in the same place of this file:

mysql -u USER --password=PASS DATABASENAME < DUMP.sql

PHP/MySQL: Howto paged with a random order

The problem is simple: we have to list randomly data from a MySQL table, by creating a paging.

Let's start with the basis: A normal paging. To do so, you use the LIMIT parameter in the MySQL query:

SELECT * FROM user LIMIT 20,10

Where we show the second page for the users with 10 elements by page.

You will probably order the result to be easier to use. For example:


At this point, everything was pretty easy and you probably already knew that.

Now we want to randomly order the result. Intuitively, you will do something like:


Which is not totally wrong. The problem with this solution will occur when changing the page, the order will be different -for sure, it's random. By changing the page you will probably see some recurrent data, and the visitor will never see all the result by reading all the pages. That's not expected.

The solution consists to generate a random number in PHP, stores it in a session variable, and finally use it in the MySQL query inside the RAND parenthesis. And it's finish. Therefore the random number used is every time the same for the visitor session, and the global order will stay the same in the differents pages.

The PHP code to generate and stored the random number:

$rand = $_SESSION['rand'];
if (empty($rand)) {
$rand = "0.".rand();
$_SESSION['rand'] = $rand;

Of course, you have to open the session with session_start() at the top of your PHP script before every out or header send -or you can use ob_start().

Finally the MySQL query becomes:


Voilà, you can make pagination with random order.

Version française de ce billet

Chips, Twix et Mix

Des news d'ici et d'ailleurs :

  • Prism sort en version 0.9
  • Si vous chercher l'extension ultime pour rechercher vos messages dans Thunderbird, Seek est votre ami
  • Firefox sort en beta 4. Encore une beta 5 avant laversion finale. A noter les améliorations de mémoire.
  • IE8 sort en beta
  • Les résultats des navigateurs en beta à l'acid test 3
  • Vous travaillez sur une version mobile d'un site ? Lisez ceci pour connaitre les bonnes pratiques
  • jQuery organise un worldwide sprint ce week-end spécialement pour jQuery UI
  • On parle de plus en plus de Piwik
  • TinyMCE sort en version 3.0.5
  • Netvibes évolue vers le réseau social, sous le nom de Ginger. Ma page est ici, et devrait se remplir ce week-end.
  • Marre de créer vos htaccess à la main ? La solution en ligne.
  • Après l'Air Guitar, Canal Plus lance l'Air Fuck : filmez-vous et gagner un scooter (wahou, un scooter, j'en ai toujours rêver...)
  • Les LoKataires, amis et artistes que j'adore, ont maintenant leur clip, simplement nommé le LoKaclip (Bravo à Pitch pour le montage !)