Archive for the ‘ Programming ’ Category

MMA wordpress theme version 1.2 released

This release revision focuses on bug fixes. Thanks to the contributors and users who sent the bug reports.

Check/Read the readme file for more details and bug fixes.

Download MMA theme v-1.2.

Note: Upgrade from 1.1 and 1.0 is highly recommended. Download the zip file above, extract it and upload it to the themes directory of your blog. If you are using previous versions, it will ask for overwrite, just click yes to overwrite and every thing will be fine.

Version 1.1 details are here.

Good luck!

Vote This Post DownVote This Post Up (+1 rating, 1 votes)
Loading ... Loading ...

Popularity: 2%

Backup and Restore MySQL database in Linux via terminal

Hi,

Personally i think, that Linux terminal is the strongest application/tool whatever you name it. Yesterday i was restoring a MySQL database size of 5MB via MySQL Administrator application, and it crashed several times. Actually i use it because the easy way to have GUI with few clicks, but that doesn’t mean i never used terminal for such things. But this crashing made me think of this powerful tool to use and use it forever..

So lets have backup our database 1st via terminal.

1- The 1st thing you need to do is open terminal(konsole).

2- mysqldump will be used to backup the database we want to.

mysqldump -u Username -p  databasename > backup.sql

Username = this is your database username
databasename = the name of your database which you want to backup
backup.sql = the filename for your database backup

The text in red should be changed according to your specific MySQL/database details. You can also give path to the filename where you want to save the backup file like: /home/mian/backup.sql.

Now lets run it in real form:

mysqldump -u admin -p storedb > /home/mian/storedb.sql

By entering this command and hitting Enter key will ask you MySQL password for user admin. By doing so will backup storedb database to the path given.

Now lets restore storedb database.

1- I assume that terminal is open.

2- Run this command in terminal:

mysql -u admin -p storedb < /home/mian/storedb.sql

This command is not much different except we don’t have mysqldump this time. Which means we are not dumping any database, just to restore one. It will also ask for the password for user admin to proceed.

If you want to run bash script to do the job for you by cron at specific times, i had written this script in past, but didn’t get time to update it regularly, but still is enough to do the backup archives with no manual running from command line.

Have fun!

Vote This Post DownVote This Post Up (No Ratings Yet)
Loading ... Loading ...

Popularity: 3%

MMA wordpress theme version 1.1 released

After having successful implementation of 1.0 on my own blog(this blog), i spare some time to enhance this theme and give it more professional look. So here i am with version 1.1, which is followed from 1.0, there are not major changes but slightly improvements over version 1.0. This blog is the live demo, if any one wondering where to find the live demo. The main change in 1.1 is CSS3 functionality.

Here is the screen shot for version 1.0, and if you want to try 1.0, download it from here.

Screenshot of mma version 1.0

And here is the screen shot of version 1.1 with some minor improvements in design.

Screenshot of mma version 1.1

Download version 1.1 from anl4u.com download zone.

——————————————————–

Note: Upgrade from 1.0 to 1.1 in easy steps.

If you want to upgrade from version 1.0 to 1.1. Download version 1.1, extract it and upload the following files to your blog folder(to the respective directives):

  • images/header-bg.png
  • style.css
  • If you have wp-pagenavi plugin installed, upload the file pagenavi-css.css from plugins/wp-pagenavi(included in 1.1) folder to your blog plugin(wp-pagenavi) folder.

And that’s it, upgrading is done.

——————————————————

Enjoy and happy blogging.

UPDATE: Newer version is available, please read this post.

Arif

Vote This Post DownVote This Post Up (+1 rating, 1 votes)
Loading ... Loading ...

Popularity: 4%

Hi all,

Do you want to create a separate user for a database which only some users/user can access. Want to create user who can only have access to databases you assign them. Then this tutorial is for you.

This will be a very simple tutorial and howto about how to create a database 1st, a user and then assign some privileges to the user to allow him/her to perform some specific actions like insert,create,update,select etc etc.

I assume you are on Linux and command shell (terminal) is opened.

To enter to MySQL mode in terminal type this:

mian@liveserver:~$ mysql -u root -p

Just enter the red color text from above, or copy from here will work and upon asking for the password enter your root password (not distro one, MySQL root password). -u means username and -p is password.

You will see output like this:

Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 35614
Server version: 5.0.51a-3ubuntu5.7 (Ubuntu)

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.

mysql>

Lets create a database (if you don’t have yet) with any name you desire (use text in red):

mysql> create database store;

You will see this output:

Query OK, 1 row affected (0.00 sec)

Now the fun part, lets create a user with password and assign some basic privileges on a database we created above:

mysql> grant CREATE,INSERT,DELETE,UPDATE,SELECT on store.* to asim@localhost identified by ‘storeadminasim‘;

In the above command you need to change the following according to your need.

store represents the database we created above.

asim is the user we want to create for the database store.

storeadminasim is the password assigned to asim.

Text in dark blue above shows the privileges we want to assign to user asim, in this case he will just perform that actions on a database.  We will not let him do whatever he want, this is good for security. This user will only access this database assigned to him and nothing else, and he can create tables in it, can update it, insert data into tables etc etc. If the above command runs successfully you will see this output in terminal:

Query OK, 0 rows affected (0.00 sec)

Now the last and important thing is to flush the privileges, type this (text in red):

mysql> flush privileges;

You will see this output:

Query OK, 0 rows affected (0.00 sec)

That’s it, you can now quit/exit from MySQL session:

mysql> exit
Bye

The user and database is created and ready for use. Got any questions?, use the comment form to ask.

Arif

Vote This Post DownVote This Post Up (+1 rating, 1 votes)
Loading ... Loading ...

Popularity: 4%

Simple yet Powerful: my 1st wordpress theme (MMA)

Hi all,

As i said before that i will allow others to use my current blog theme, which i developed. So here we are with complete theme.

Some of the features are:

  1. Widget ready
  2. jQuery validation for comment form
  3. WP-ratings and WP-polls integration is available
  4. Related Posts feature added
  5. RSS feed for comments and posts
  6. Custom header image (change it to your taste)
  7. Custom footer
  8. Tags showing in posts
  9. Gravatar included for comments
  10. Threaded comments support included
  11. For more features, please read the readme file.

Checkout the screen shots at MMA Gallery.

Live Demo: anl4u.com

Download from anl4u.com download zone.

Note: Please read the readme file for more details and installation.

Got any issues, have  ideas to improve?, contact me by comment form.

Enjoy and have fun with it.

UPDATE: Newer version is available, please read this post. It is recommended to use the latest version for full functionality.

Arif

Vote This Post DownVote This Post Up (No Ratings Yet)
Loading ... Loading ...

Popularity: 2%

Generate Random Password in PHP

Hi all,

While working on a project last week, i implemented a function to generate random password by calling it. There are many styles of password generator available over the net, but it depends which one suits our taste and need. The function i used has the ability of creating random password from the given set of characters with digits. So lets have a look at the code 1st and explain it after that:

<?php

function  randpassword() {
$length = 8;
$characters = “0123456789abcdefghijklmnopqrstuvwxyz”;
$string = “”;

for ($p = 0; $p < $length; $p++) {
$string .= $characters[mt_rand(0, strlen($characters)-1)];
}

return $string;
}

echo randpassword();

?>

Now lets explains the important components:

function  randpassword() {

randpassword() is the function name to call it later for use.

$length = 8;

$length variable holds the number of characters to generate while passing it to the for loop condition.

$characters = “0123456789abcdefghijklmnopqrstuvwxyz”;

Important part of the function to tell the function to use these characters and numbers for the password, you can change it according to the need like using capital alphabets or both.

for ($p = 0; $p < $length; $p++) {

For loop to get the condition and loop it until the loop finished.

$string .= $characters[mt_rand(0, strlen($characters)-1)];

Another important part of the function in the for loop condition to analyze the characters. $characters are coming from the variable above. mt_rand() is PHP function to return the desired min and max number given to it. For example mt_rand(2,10) will generate random numbers between 2 and 10. In the above line, we pass 0(zero)  and then characters length with -1, please note that -1 is necessary here to avoid index out of bounds error. If you miss -1, the function will sometime generate 7 characters with error index out of bound but mentioned above 8 to generate. That’s because we are using mt_rand function which will sometime return integer $max and there will be no proper value in $characters[strlen()].

return $string;

echo randpassword();

And the last return $string will return the string value stored in it and then just echo it to see the outcome of the function.  You can save it in database or send it to user before storing a hash of it in database.

That’s it easy and simple.

Arif

Popularity: 3%

Write to excel sheet in PHP from MySQL tables

Hi folks,

Some time we need to have a CSV file and store our database data in it for reporting and other things. To create such file on the fly we will need PHP, Apache and MySQL installed which i am sure is installed. What this script will be capable of:

  • Get data from MySQL table
  • Export multiple rows from db
  • Save file with different file name with date & time

Here is the script. One would need to connect to database and the rest of explanation i write in the comments inside the script to explain the lines.

<?php
//db.php connection to your database

// Query to get results from mysql table
$query = “SELECT id,name,phone,address FROM user”;
$res = mysql_query($query);

// Functions for export to excel.
function xlsBOF() {
echo pack(“ssssss”, 0×809, 0×8, 0×0, 0×10, 0×0, 0×0);
return;
}
function xlsEOF() {
echo pack
(“ss”, 0x0A, 0×00);
return;
}
function xlsWriteNumber($Row, $Col, $Value) {
echo pack(“sssss”, 0×203, 14, $Row, $Col, 0×0);
echo pack(“d”, $Value);

return;
}
function xlsWriteLabel($Row, $Col, $Value ) {
$L = strlen($Value);
echo pack(“ssssss”, 0×204, 8 + $L, $Row, $Col, 0×0, $L);
echo $Value;

return;
}

// Unique file name with datetime
$file_name = “csv-”.date(‘dmy-His’);
header(“Pragma: public”);
header(“Expires: 0″);
header(“Cache-Control: must-revalidate, post-check=0, pre-check=0″);
header(“Content-Type: application/force-download”);
header(“Content-Type: application/octet-stream”);
header(“Content-Type: application/download”);;
header(“Content-Disposition: attachment;filename=$file_name.xls “);
header(“Content-Transfer-Encoding: binary “);

xlsBOF();

// Top of the excel sheet to write header
xlsWriteLabel(0,0,”User Data – CSV”);

// Make column labels. (at line 3)
xlsWriteLabel(2,0,”Id”);
xlsWriteLabel(2,1,”Name”);
xlsWriteLabel(2,2,”Phone”);
xlsWriteLabel(2,3,”Address”);

// To start writing from row three from top
$xlsRow = 3;

// Put data records from mysql by while loop.
while($row=mysql_fetch_array($res)){

xlsWriteNumber($xlsRow,0,$row['id']); // 0 indicates column number
xlsWriteLabel($xlsRow,1,$row['name']);
xlsWriteLabel($xlsRow,2,$row['phone']);
xlsWriteLabel($xlsRow,3,$row['address']);

$xlsRow++;
}
xlsEOF();
exit();
?>

This script is tested and working well. But if got any questions, use comment form for the feedback.

Arif

Popularity: 7%

Extract from Archive Tar/Gz/Tar.gz with Pear in PHP

Hi all,

Previously i had wrote how to install Pear. Now we will make use of Pear package to extract some archives like tar,gz,tar.gz etc.

1st of all we need Archive_Tar to download and install. Download package from here, and run this command in terminal to install it:

pear install path/to/Archive_Tar-1.3.x

In Archive_Tar-1.3.x x represents which version you downloaded, at the time of writing the latest version is 1.3.6. Also include the full path to the downloaded archive.

To check, either Archive_Tar is installed successfully:

pear list

It will show all the installed packages like:

me@me-desktop:~$ pear list
Installed packages, channel pear.php.net:
=========================================
Package          Version State
Archive_Tar      1.3.6   stable
Console_Getopt   1.2.3   stable
PEAR             1.9.0   stable
Structures_Graph 1.0.3   stable
XML_Util         1.2.1   stable

Now in your PHP script, you need to include it like:

<?php

require_once ‘Archive/Tar.php’;

?>

Before jumping to the script, lets create some directories:

me@me-desktop:~$ mkdir /var/www/files
me@me-desktop:~$ mkdir /var/www/files/extracted_files
me@me-desktop:~$ mkdir /var/www/files/backup

Change the permissions, that script will be able to write into it.

su -

root@mian-desktop:~# chmod 777 /var/www/files/*

Full Script:

<?php
require_once ‘Archive/Tar.php’;
//Filename to extract, you can get filename from database too.
$filename = ‘theme.tar’;
//From database, you need to run query to get the filename. Enable/uncomment the line below if you need to use it.
//$filename = $row->filename;

//Get file extension.
$ext = pathinfo($filename, PATHINFO_EXTENSION);

//Path to file.
$name_of_file = “/var/www/files/”.$filename;

//Directory to extract file, this is in case you want the files to be extracted in different location.
$dir = “/var/www/files/extracted_files/”;

//Directory to backup the old files, this is in case you want the old files to be deleted.
$backup = “/var/www/files/backup/”.$filename;

//Check if file exists
if(file_exists($name_of_file)){
//Check file extension
if($ext == ‘tar’ || $ext == ‘gz’){
//Open file to read
$tar = new Archive_Tar($name_of_file, true);
//Extract the file using Pear library
$result = $tar->extract($dir);
echo “File extraction done!<br>”;
}

//Move file to backup dir, if you dont want this feature just comment/delete this line and above starting with $backup.
rename($name_of_file, $backup);
echo “File moved to backup dir”;
}
?>

Just copy the above script and paste it in your favorite text editor, save it as index.php in files dir you just created above and now open browser input this address,

http://localhost/files

This script is TESTED and working properly.

Just follow the instructions step by step and hope it will work for you as it should.

Have any questions? use comment section to post.

Happy coding!

Arif

Popularity: 9%