26
Sep
2011

Closing Tag in PHP

If file consists only of php code (without HTML code), then the best practice is to omit closing tag (?>).

Although closing tag rarely causes any problems (the most common - accidentally putting space character after "?>" can causes sending a HTTP headers and then if you want to send headers manually for example for the redirect purposes, it's simple doesn't work 'cause headers has already been sent), nevertheless giving up to use closing tag is a rather good practice in php.

Generally speaking, if closing tag is not mandatory, so why we need to use it?

So, in case

  1. <?php
  2. echo 'Hello, world!';
  3. ?>
  4. <html>something</html>

we must using closing tag for keeping apart php and html code.

But in this case

  1. <?php
  2. echo 'Hello, world!';

closing tag should be omitted.

And in this case

  1. <html>something</html>
  2. <?php
  3. echo 'Hello, world!';
  4. ?>

you can omit closing tag or not on your own discretion. Though, it's better not to omit "?>" so the code become more clear to understand.

Category: 
0
21
Sep
2011

Frequently Used Server Control Commands

Apache

Check status, start, stop, restart and reload:

  1. /etc/init.d/apache2 status
  2. sudo /etc/init.d/apache2 start
  3. /etc/init.d/apache2 stop
  4. sudo /etc/init.d/apache2 restart
  5. sudo /etc/init.d/apache2 reload

Enable/disable modules:

  1. sudo a2enmod module
  2. sudo a2dismod module

Enable/disable virtual hosts:

  1. sudo a2ensite test.my
  2. sudo a2dissite test.my

MySQL

Start, stop, restart:

  1. sudo service mysql start
  2. sudo service mysql stop
  3. sudo service mysql restart
Tags: 
Category: 
0
20
Sep
2011

How to Import Large MySQL Dumps

To import sql dumps it's commonly used PhpMyAdmin. However, if the dump is too large, there some problems can appear such as file size limit, exceeding script execution time, memory shortage and so on.

So the best solution in this case (and the proper one) is to use mysql client utility via the console. For import sql dump we should use "source" statement.

  1. tulvit@tulvit-desktop:~$ mysql -uroot -proot;
  2. Welcome to the MySQL monitor. Commands end with ; or \g.
  3. mysql> use my_db;
  4. Database changed
  5. mysql> source ~/dump.sql;
  6. ...
Category: 
0
10
Sep
2011

Redirect WWW to Non-WWW and Vise Versa

It's a rather common task - to make one of the two aliases the main one (with www or without) and set up redirection from the second alias. For instance, we can set up www.site.com as a main domain, so site.com will redirect us to the www.site.com. Or vise versa, we can set a redirect from www.site.com to site.com so the main domain become the one without www.

For this purposes, we should use 301 redirect, which is permanent redirect.

The easiest way to achieve this goal is to use .htaccess, which should be exist in your site's root directory. You should have Apache's mod_rewrite module has been installed too, but frequently it's already installed on your web hosting by default.

If you want to make domain without www the main one, you should add to your .htaccess the code below:

  1. <IfModule mod_rewrite.c>
  2. RewriteEngine on
  3. RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  4. RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]
  5. </IfModule>

Or, if you want to make domain with www the main one, you should use this:

  1. <IfModule mod_rewrite.c>
  2. RewriteEngine on
  3. RewriteCond %{HTTP_HOST} !^www\. [NC]
  4. RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  5. </IfModule>
Category: 
0
09
Sep
2011

MD5 Hashing and Salt

md5 hash can be easily cracked by using Rainbow Tables. So, pure md5 hash must be using only for checking data integrity and not for security purposes.

However, by using "salt" it's become impossible to reverse md5 and get the original data, which means that this method can be used to encipher passwords and other important information.

Salt is just a some random bits, for instance some text or even image.

So, instead of

  1. <?php
  2. $pwd = 'my_pwd';
  3. $hash = md5($pwd);
  4. ?>

we should use something like this

  1. <?php
  2. $salt = 'some text';
  3. $pwd = 'my_pwd';
  4. $hash = md5($pwd . $salt);
  5. ?>

or like this

  1. <?php
  2. $salt = 'some text';
  3. $pwd = 'my_pwd';
  4. $hash = md5(md5($pwd) . $salt);
  5. ?>

or even like this

  1. <?php
  2. $salt = 'some text';
  3. $pwd = 'my_pwd';
  4. $hash = md5(md5($pwd . $salt) . $salt);
  5. ?>
Category: 
8