Apr 27

zen cart – clear all customer data from zen cart website database

if you copy a new site from another zen cart website,you may need to clear all the customer data,include customer info,customer basket,customer order,and product review

Below is a sql script to do that clear job.Be careful when you test it against your zen cart database.

TRUNCATE `customers`;
TRUNCATE `customers_basket`;
TRUNCATE `customers_basket_attributes`;
TRUNCATE `customers_info`;
TRUNCATE `customers_wishlist`;
TRUNCATE `customers_info`;
TRUNCATE `customers_basket`;
TRUNCATE `orders`;
TRUNCATE `orders_products`;
TRUNCATE `orders_status_history`;
TRUNCATE `orders_total`;
TRUNCATE `reviews`;
TRUNCATE `reviews_description`;

Apr 26

php – how to locate php.ini file of the server

well,you need to locate the php.ini file to tweak some configuration of the php to meet your script.where is the php.ini file.Especial there may be more that one php.ini file on the server,which is the one take effect?

the easiest way is to code a phpinfo page,than browse to the phpinfo page.it will show the location of the php.ini current in use.

<?php phpinfo(); ?>

On some webservers, the php are installed from some binary package,you can list all file of that package to get some clue,ie

rpm -ql php5.2 OR
rpm -ql lamp

Yes,you may also use the locate command to do that

locate php.ini

Apr 25

bash – shell script to update all user’s password

With the help of the function in my last post,i can update all user’s password with my shell script now.below is the bash shell script code.i have test the shell script with bash shell,it works ok.It will store all new password in a file named updated_ftp.txt of the current working directory.

#/bin/bash
#author george zheng <xinhaozheng@gmail.com>
#date 04 – 22 – 2012
function gen_pass()
{
LENGTH=12
array1=(w e r t y u p a s d f h j k z x c v b m Q W E R T Y U P A D F H J K L Z X C V B N M 2 3 4 7 8 ! @ $ % \# \& \* \= \- \+ \?)
MODNUM=${#array1[*]}
pwd_len=0
while (( “$pwd_len” < “$LENGTH” ))
do
index=$(($RANDOM%$MODNUM))
password=”${password}${array1[$index]}”
pwd_len=$pwd_len+1
done
}
$ftpfile=’updated_ftp.txt’
touch $ftpfile
alluser=`ls -ald /home/*/public_html|grep -v -E ‘/ipp?[0123456789]+(com|co)?/’| awk ‘{print $9}’|cut -d/ -f3`
for user in $alluser
do
password=”;
gen_pass
echo $password | passwd $user –stdin
echo “$user:$password” >> $ftpfile;
done
echo “All user password has been updated successfully!”

Note: you will need to adapt the ‘alluser’ line to meet you needs.like you may only need to change some special user which username start with a prefix or whatever else.And you can also get the user info directly from /etc/passwd file.

Apr 24

bash — get a random password from shell script

As i managed more than ten linux server,i need to update all user account per customer request. i need to generate rand password for each account.here comes the shell script to generate a random password

#/bin/bash
#author george zheng <xinhaozheng@gmail.com>
#date 04 – 22 – 2012

LENGTH=12
array1=(w e r t y u p a s d f h j k z x c v b m Q W E R T Y U P A D F H J K L Z X C V B N M 2 3 4 7 8 ! @ $ % \# \& \* \= \- \+ \?)
MODNUM=${#array1[*]}
pwd_len=0
while (( “$pwd_len” < “$LENGTH” ))
do
index=$(($RANDOM%$MODNUM))
password=”${password}${array1[$index]}”
pwd_len=$pwd_len+1
done
echo $password > passfile

or you may define a function,check the get_pass function below

#/bin/bash
#author george zheng
function gen_pass()
{
LENGTH=12
array1=(w e r t y u p a s d f h j k z x c v b m Q W E R T Y U P A D F H J K L Z X C V B N M 2 3 4 7 8 ! @ $ % \# \& \* \= \- \+ \?)
MODNUM=${#array1[*]}
pwd_len=0
while (( “$pwd_len” < “$LENGTH” ))
do
index=$(($RANDOM%$MODNUM))
password=”${password}${array1[$index]}”
pwd_len=$pwd_len+1
done
}
gen_pass
echo $password

Keep in mind that definition of the function  need to be put before the first call to the function.

Apr 23

mysql – how to check if table existed with php

One of our zen cart website throw out an error after system crashed.The error message is something as below:

abc.configuration does not exist (abc is the db name)

i logged into mysql console with

mysql -u root -p

then i issue those command in the mysql console

use abc;
show tables;

The configuration table is showed in the result list.When I browse to the phpmyadmin page,i found the configuration is missing.This make me confused.I thought the configuration table may be corrupted.so it’s not shown in phpmyadmin and any sql query on it will result in error.that’s why the zen cart website got 404 error page.

I usually check the table existence with following php function which based upon ‘show table’ query.

function table_exists($link,$db,$table) {

if (empty($table)) {
die('No table name provided to check');
}
$tables = array();
$sql = "SHOW TABLES FROM $db";
$result = mysql_query($sql, $link);

if (!$result) {
echo 'DB Error, could not list tables,' . mysql_error();
exit;
}

while ($row = mysql_fetch_row($result)) {
$tables[] = $row[0];
}
return in_array('configuration', $tables);
}

it is obviously that the ‘show tables’ query will lead to error when a table is corrupted.so is there better way to check?After some seconds i got solution from google.Below is the php function to check the existence of a table.It worked upon MySQL information(the information_schema database).

function table_exists($tablename, $link, $database = false) {

if(!$database) {
  $res = mysql_query("SELECT DATABASE()", $link);
  $database = mysql_result($res, 0);
}

$res = mysql_query("SELECT COUNT(*) AS count FROM information_schema.tables WHERE table_schema = '$database' AND table_name = '$tablename'", $link);

return mysql_result($res, 0) == 1;
}

i thought this would be better way to go.

Apr 22

zen cart — how to show customer’s review in product’s info page

For the seo purpose,i need to show all product’s review in the product info page.so the product info page will be updated when customer add new review on the product.Google like the website which updates its content regularly.

By default,zen cart show customer’s review on the product review page,so i think this would not be a difficult thing to be done.All i need to do is copy and paste.Well,it’s easy,but it’s not so easy to be done.You may know that any page in zen cart is defined by at least three parts,the language file,the template file and the module file,the one usually located in zen_cart_site_root/includes/modules/pages/[page_name]/ folder.

so first i  include the language file in the product info page as below:

<?php
//require the language file of product reviews page
require_once($language_page_directory .'product_reviews.php');
?>

then,copy template part:

foreach ($reviewsArray as $reviews) {
?>

<hr />

<div></div>
<div></div>
<div></div>
<div>’) . ((strlen($reviews['reviewsText']) >= 100) ? ‘…’ : ”); ?></div>
}
?>

third,i copy the product_review module file,which get the review data from database

cd includes/modules/pages
cp product_reviews/header_php.php  product_info/header_php_review.php

Ok.now refresh the prodcut info page,it will show product review if any existed.

Apr 21

Apache – how to set up mass virtual hosts with mod_vhost_alias module

If you manage many websites on a Apache web server,you may need this to save your time and also the server’s memory.Think about you have 10K website(virtual hosts), you would never like to write the configuration for each virtual host.The Apache configuration file will be huge which my contain about 70k lines (10k multiply 7 ). This will also slow down the Apache web server start speed.

Setting up mass virtual host will dynamically create virtual hosts without any configuration.You don’t need to restart the Apache web server.The only thing you need to do is create a folder in the specified folder and add a dns record.

let’s take a look at how to setting up mass virtual host.

mod_vhost_alias has a config directive VirtualDocumentRoot which dynamically configure the location of the document root for a given virtual host.it allows you to determine where Apache will find your documents based on the value of the server name.

UseCanonicalName Off
VirtualDocumentRoot /var/www/vhost/%2+/
A request to http://www.example.com/ will be served by /var/www/host/example.com/,request to http://www.example.com/page/test.html will be parsed as /var/www/host/example.com/page/test.html

however,with the configuration above is not enough,for request to http://example.com/page/test.html will be served by /var/www/host/.com/page/test.html.This will lead to 404 error page as the file is not existed.
You need to redirect all request to none www domain too its long format www.domain.com.

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

So,i config my apache with the following configuration and it work very well.

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

<Directory “/var/www/vhost”>
AllowOverride ALL
</Directory>
VirtualDocumentRoot /var/www/vhost/%2+/

 

Apr 20

linux — how to extract tar ball file to specified folder

by default,tar will extract all the file to the current working directory.if you don’t want to change current working directory,then you can extract the tar ball’s content to specified folder by the -C command option of tar command.

this command will change the current working directory before tar extract the content.

Apr 19

putty — how to run putty without define a session

how to run putty without define a session? yes,you can run putty from command without any session.

Putty is very convenient if you like to work on ssh but you are working on window desktop.Putty can save host’s ip,login account and some other info as session.With the help of session,you save a lot of time.However,you will need to define those session if you move to a new computer.As putty save all session in the window registry,not as text file which can be copied to other computer.yes,you can export all the session from the window registry.

I prefer to run putty in command mode,without any session defined.PuTTY can be made to do various things without user intervention by supplying command-line arguments.

for example,the following command will login into the server as root user via ssh protocol,connect to the 5880 port of the remote server with ip 7x.8x.17x.16x.it also set up SOCKS-based dynamic port forwarding on a local port 30000,which i usually used as my firefox  proxy setting.

putty -ssh -P 5880 -pw bdKde?pNrfym  -D 30000 root@7x.8x.17x.16x

let me explain the option in detail:

-ssh specified the protocol to use

-P  set the remote port to connect to

-pw this option used to set the password

-D set up local port for SOCKS-based dynamic port forwarding

Apr 18

linux — how to list all files of a package installed by yum

yum is very helpful to install package.however,they are so many package from different repository with different kinds of configuration.for example, to install apache web server,there are a lot of package around the internet but each come with different file structure and configuration.To change the default setting,you need to find out the files where the package installed to first.so how to show all the files installed by yum?

in fact,yum is based upon rpm,so to list all file of a package,you can use the following command:

rpm -ql [package name]

ie,

rpm -ql php
rpm -ql httpd
rpm -ql httpd|more