How to Back Up and Restore a MySQL Database

As you know your website data is stored in your database server. To prevent disaster, backup your data regularly is a must for you and every website owner. There are many ways to backup MySQL data, one of them is using mysqldump to dump your MySQL data to a file.

Dump all MySQL Databases

mysqldump –user=XXXXXXXX –password=XXXXXXX –all-databases > /PATH/TO/DUMPFILE.sql

Dump Individual or Multiple MySQL Databases

mysqldump –user=XXXXXXXX –password=XXXXXXX –databases DB_NAME1 DB_NAME2 DB_NAME3 > /PATH/TO/DUMPFILE.sql

Dump only certain tables from a MySQL Database

mysqldump –user=XXXXXXXX –password=XXXXXXXX –databases DB_NAME –tables TABLE_NAME > /PATH/TO/DUMPFILE.sql

Dump and Compress MySQL Database

mysqldump –user=XXXXXXXX –password=XXXXXXXX –databases DB_NAME | gzip -9 > /PATH/TO/DUMPFILE.sql.gz

Note: The mysqldump will dump MySQL data according to the credential given, the output will pipeline to gzip to compress it with highest compression rate and output as DUMPFILE.sql.gz file.

Restore MySQL Dump file

mysql -u [uname] -p[pass] [dbname] < /PATH/TO/DUMPFILE.sql

Restore Compressed MySQL Dump file

gunzip < /PATH/TO/DUMPFILE.sql.gz | mysql -u [uname] -p[pass] [dbname]

Note: gunzip will extract the compressed file and the output (the sql commands) will be run in MySQL database as credential given. The sql command will create or overwrite the existing table and data.

Permanent link to this article: https://blog.openshell.in/2011/02/how-to-back-up-and-restore-a-mysql-database/

Displaying drop down menu over a swf file

By default when a drop down menu displayed over a swf file it will not be visible in the page. To make it visible and accessible to the user,

just put <param name=”wmode” value=”transparent” /> inside the <object> tag and add the property wmode=”transparent” in <embed> tag.

Permanent link to this article: https://blog.openshell.in/2011/01/displaying-drop-down-menu-over-a-swf-file/

Public IP and Private IP

hi,

In this post i would like to give a small intro about public and private ip addresses.  most people use to start with ip address have different classes and blah… blah.. blah…. in this post i would like to give it in a nontechnical term.

Private IP Address:

Private IP address is a address which is used by computers which are connected in a LAN(Local Area Network). using these kind of address you cannot able to communicate with outer world. It means private ip address cannot be accessed for other computers which are not connected in your LAN.  if you want to know your private ip address

Windows xp, 2003, vista users: click start and type cmd in run (or) press win+r (press both wondows key and r). this will open terminal windows or command window Type ipconfig and press enter it will print your ipaddress in command window.

windows 7 users: click start and type cmd in the search bar available in start menu. Type cmd and wait untill search shows cmd in the start menu. then right click on the cmd and select run as administrator.  and type ipconfig and press enter it will print your ipaddress in command window.

Linux, unix and mac users: go to terminal and type ifconfig and it will print your ipaddress for distro in linux normal users are not allowed to run ifconfig in such case type sudo ifconfig

Public IP Address:

Public IP Address are mostly used in routers or modems. IT means if u have internet connected in your office or home you will have your Public IP address in the modem or router which u use to connect to the internet. (technically routers have two interfaces one it to connect to your computer or LAN and another is to connect to internet or WAN). if you want to know yout public ip address open internet explorer or firefox or any internet browser and  visit http://network-tools.com/ it will show your public ip address.

Permanent link to this article: https://blog.openshell.in/2011/01/public-ip-and-private-ip/

Mysql Error: #1045

After installed the XAMPP and mySQL, when you typed

http://localhost/phpmyadmin

in your web browse to get into the phpmyadmin, it shows a error like this:

#1045 - Access denied for user 'root'@'localhost' (using password: NO)

At this point you have to edit the config.inc.php file on line 21  $cfg[‘Servers’][$i][‘password’]             = ”; to $cfg[‘Servers’][$i][‘password’]             = ‘yourpassword’;

config.inc.php file will found in xampplitephpMyAdmin

now restart your XAMPP and refresh your web browser.

if you still got the same error then just try this

http://localhost/phpmyadmin/?username=xxxx&password=yyyy

Note: type your mysql username in xxxx andpassword in yyyy

yow will get logged in.

Permanent link to this article: https://blog.openshell.in/2011/01/mysql-error-1045/

Skip or Hide PHP errors / warnings

Errors are hints from compiler for the programmer to correct. But sometimes we face some unwanted or dynamic error.

For example,

$content = file_get_contents(https://blog.openshell.in/2011/01/execute-php-script-from-command-line-in-ubuntu/);

Here , the url is invalid, or the server of the url is down , PHP shows Warning: Failed to open stream or file access is disabled.

Use @ in front of such function never shows any warning message.

For example,

$content = @file_get_contents(url here) or die (‘Sorry , cannot access the url’);

Another method is runtime PHP ini editing dynamically,

ini_set (‘display_errors’, 0);

//write the code which may cause error here

ini_set (‘display_errors’, 1);

But , these things are not recommended. Only for unavoidable situations.

Permanent link to this article: https://blog.openshell.in/2011/01/skip-or-hide-php-errors-warnings/

How to Send Email from a PHP Script Using SMTP Authentication

Now you can send emails with SMTP authentication using this script. For most email uses, the free PEAR Mail package offers all the power and flexibility needed, and it authenticates with your desired outgoing mail server, too. For enhanced security, secure SSL connections are supported.

Send Email from a PHP Script Using SMTP Authentication

To connect to an outgoing SMTP server from a PHP script using SMTP authentication and send an email:

* Make sure the PEAR Mail package is installed. (PEAR Mail install in Ubuntu)
* Make sure you change the following variables in given example:

  1. from: the email address from which you want the message tbe sent.
  2. to: the recipient’s email address and name.
  3. host: your outgoing SMTP server name.
  4. username: the SMTP user name (typically the same as the user name used tretrieve mail).
  5. password: the password for SMTP authentication.

Sending Mail from PHP Using SMTP Authentication:

<?php

require_once “Mail.php”;

/* mail setup recipients, subject etc */
$recipients = “Recipient Name “;
$headers[“From”] = “Sender Name “;
$headers[“To”] = “Recipient Name “;
$headers[“Subject”] = “Hi”;
$mailmsg = “Hello,nnThis is a test.”;

/* SMTP server name, port, user/passwd */
$smtpinfo[“host”] = “mail.openshell.in”;
$smtpinfo[“port”] = “26”;
$smtpinfo[“auth”] = true;
$smtpinfo[“username”] = “smtp_username”;
$smtpinfo[“password”] = ‘smtp_password’;

/* Create the mail object using the Mail::factory method */
$mail_object =& Mail::factory(“smtp”, $smtpinfo);
/* Ok send mail */

$mail = $mail_object->send($recipients, $headers, $mailmsg);

if (PEAR::isError($mail)) {

echo “<p>”. $mail->getMessage() .”</p>”;
} else {

echo “<p>Message successfully sent!</p>”;
}
?>

Sending Mail from PHP Using SMTP Authentication and SSL Encryption:

<?php

require_once “Mail.php”;

/* mail setup recipients, subject etc */
$recipients = “Recipient Name “;
$headers[“From”] = “Sender Name “;
$headers[“To”] = “Recipient Name “;
$headers[“Subject”] = “Hi”;
$mailmsg = “Hello,nnThis is a test.”;

/* SMTP server name, port, user/passwd */
$smtpinfo[“host”] = “ssl://mail.openshell.in”;
$smtpinfo[“port”] = “465”;
$smtpinfo[“auth”] = true;
$smtpinfo[“username”] = “smtp_username”;
$smtpinfo[“password”] = ‘smtp_password’;

/* Create the mail object using the Mail::factory method */
$mail_object =& Mail::factory(“smtp”, $smtpinfo);
/* Ok send mail */

$mail = $mail_object->send($recipients, $headers, $mailmsg);

if (PEAR::isError($mail)) {

echo “<p>”. $mail->getMessage() .”</p>”;
} else {

echo “<p>Message successfully sent!</p>”;
}
?>

Permanent link to this article: https://blog.openshell.in/2011/01/how-to-send-email-from-a-php-script-using-smtp-authentication/

Replace String In Mysql With One Query

We know how to change data inside a field by using update command inside a query. Here we will try to replace part of the data without changing the entire data of the field. The SQL query we design will work like string replacement in any script we use. For this we will use replace command to replace a part of the data inside a field. Here is the format:

UPDATE `table_name` SET `field_name` = REPLACE(`field_name`, ‘Keyword to be replaced’, ‘Replacement String’);

For example, if we want to replace the string “<” with an actual less-than angle bracket in all records that have “<” in the “articleItem” column.

UPDATE articles SET articleItem = REPLACE(articleItem, ‘&lt;’, ‘<‘)

Permanent link to this article: https://blog.openshell.in/2011/01/replace-string-in-mysql-with-one-query/

Installing PEAR Mail for PHP On Ubuntu

It helps you how to make Ubuntu PHP localhost mail function work, using PHP to send mail from localhost is easy. First, you need to install some PEAR mail packages.

In terminal, run the following command one by one:

sudo apt-get install php-pear
sudo pear install mail
sudo pear install Net_SMTP
sudo pear Auth_SASL
sudo pear install mail_mime

Permanent link to this article: https://blog.openshell.in/2011/01/installing-pear-mail-for-php-on-ubuntu/

How to Create a CAPTCHA with PHP

CAPTCHA is a simple test to determine if a user is a computer or a human. It is used to prevent spam abuse on the websites. So if you use CAPTCHA on your web site forms, this can help in stopping some bots and making life harder for other bots in accessing or using your forms.

So, you have a public submission form on your website (contact page, forum submission) and need to prevent spam auto-submitters. A common way to do this is to implement CAPTCHA – an image with a randomly generated string.

Obviously you need a PHP engine enabled for your Web server to execute PHP scripts, and GD (PHP graphics library) to generate the image. The solution below is tested for Apache (Windows and Unix), IIS (Windows), PHP-4, PHP-5, GD and GD2.

Please follow the steps to generate captcha generaion

1) Make a PHP script (separate file captcha.php) which will generate the image:

<?

session_start();

$md5_hash = md5(rand(0,999));
//We don’t need a 32 character long string so we trim it down to 5
$string = substr($md5_hash, 15, 4);

$_SESSION[‘rand_code’] = $string;

$dir = ‘./style/font/’;
$width=125;
$height=30;

$image = imagecreatetruecolor($width, $height);
$black = imagecolorallocate($image, 0, 0, 0);
$color = imagecolorallocate($image, 35, 155, 240);
$white = imagecolorallocate($image, 255, 255, 255);
$noise = imagecolorallocate($image, 0, 138, 187);

imagefilledrectangle($image,0,0,399,99,$white);

// add noise
for ($c = 0; $c < 40; $c++){

$x = rand(0,$width-1); $y = rand(0,$height-1); imagesetpixel($image, $x, $y, $noise);

}

//imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )

imagettftext ($image, 25, 0, 5, 27, $color, $dir.”AFPEPSI.TTF”, $_SESSION[‘rand_code’]); header(“Content-type: image/png”); imagepng($image);

?>

2) Add the following line at the top of the page where you need to implement CAPTCHA:

<?php session_start() ?>

3) Add the following line to check whether the CAPTCHA string entered by the visitor is valid, before the line where you will proceed with a submitted message:

<?php
if($_SESSION[“captcha”]==$_POST[“captcha”])
{
//CAPTHCA is valid; proceed the message: save to database, send by e-mail …
}
?>

4) Finaly add the CAPTCHA to the form:
Contact us (Post new message):

<?php session_start() ?>
<img src=”captcha.php” alt=”captcha image”>
<?php
if(isset($_POST[“captcha”]))
if($_SESSION[“captcha”]==$_POST[“captcha”])
{
//CAPTHCA is valid; proceed the message: save to database, send by e-mail …
echo ‘CAPTCHA is valid; proceed the message’;
}
else
{
echo ‘CAPTCHA is not valid; ignore submission’;
}
?>

Permanent link to this article: https://blog.openshell.in/2011/01/how-to-create-a-captcha-with-php/

Random Image Rotation in ROR

It’s pretty easy to accomplish random image rotation in ROR. This is a simple example of how it can be done, and of course there are ways to make it better (for example, this snippet will only rotate and display .jpg, .gif, and .png files).

First, create a folder – let’s name it “rotate” – inside of the /public/images/ folder in the rails project directory.

Next, drop a few images in the folder (ideally all of the same dimensions).

Then, add this method to your application_helper.rb file in the /app/helpers folder:

def random_image
image_files = %w( .jpg .gif .png )
files = Dir.entries(
“#{RAILS_ROOT}/public/images/rotate”
).delete_if { |x| !image_files.index(x[-4,4]) }
files[rand(files.length)]
end

Finally, include the following line in any .html.erb file to display a random image:

<img src=”./images/rotate/<%= random_image %>” />

And that’s all there is to it.

Permanent link to this article: https://blog.openshell.in/2011/01/random-image-rotation-in-ror/