centos 安装 mssql 支持php访问mssql(仅64位)

1、安装所需库

yum install php.x86_64
yum install php-odbc.x86_64
yum install php-pear.noarch
yum install php-pecl-apc.x86_64
yum install php-xml.x86_64
yum install php-xmlrpc.x86_64
yum install php-tidy.x86_64
yum install php-intl.x86_64
yum install php-imap.x86_64
yum install php-pecl-memcache.x86_64

2、下载 mssql 的OSBC驱动
http://www.microsoft.com/en-us/download/confirmation.aspx?id=28160

3、安装
cd ~
tar xvf sqlncli-11.0.1790.0.tar.gz

cd ./sqlncli-11.0.1790.0

./build_dm.sh
如果有提示输入”YES” 必须大写。

如果成功

./install.sh install
如果有提示输入”YES” 必须大写。

校验是否安装
odbcinst -q -d -n “SQL Server Native Client 11.0”

4、其他

vi /etc/odbcinst.ini 查看内容

[PostgreSQL]
Description=ODBC for PostgreSQL
Driver=/usr/lib/psqlodbc.so
Setup=/usr/lib/libodbcpsqlS.so
Driver64=/usr/lib64/psqlodbc.so
Setup64=/usr/lib64/libodbcpsqlS.so
FileUsage=1

[MySQL]
Description=ODBC for MySQL
Driver=/usr/lib/libmyodbc5.so
Setup=/usr/lib/libodbcmyS.so
Driver64=/usr/lib64/libmyodbc5.so
Setup64=/usr/lib64/libodbcmyS.so
FileUsage=1

[SQL Server Native Client 11.0]
Description=Microsoft SQL Server ODBC Driver V1.0 for Linux
Driver=/opt/microsoft/sqlncli/lib64/libsqlncli-11.0.so.1790.0
Threading=1
UsageCount=1

vi /etc/odbc.ini 开始空文件

编辑内容如下:

[连接名]
Driver = SQL Server Native Client 11.0
Server = tcp:host网址,1433
Trace=Yes

保存即可。

5、配置 httpd.conf文件,不知道有没有用
Add these two lines to /etc/httpd/conf/httpd.conf
SetEnv ODBCSYSINI /etc
SetEnv ODBCINI /etc/odbc.ini

测试
# isql -v 连接名 ‘用户名’ ‘密码’

将显示如下表示连接成功:
+—————————————+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+—————————————+
SQL> quit

http://strangenut.com/blogs/dacrowlah/archive/2012/04/13/installing-and-using-the-microsoft-sql-server-odbc-driver-for-linux.aspx

6、编辑php程序:
prepare(“$query”);
$stmt->execute();
while ($row = $stmt->fetch()) {
print_r($row);
}
$dbh = null;
unset($dbh); unset($stmt);

?>

http://php.net/manual/zh/ref.pdo-odbc.php

http://phplens.com/phpeverywhere/node/view/9

7、注意:
http://www.vixual.net/blog/archives/133

PHP Warning: PHP Startup: Unable to load dynamic library ‘/usr/lib64/php/modules/module.so’ – /usr/lib64/php/modules/module.so: cannot open shared object file: No such file or directory in Unknown on line 0
; Enable mcrypt extension module

編輯 /etc/php.d/mcrypt.ini,將第 2 行的
将 extension=module.so 改成 extension=mcrypt.so
# service httpd restart

关于生日计算的程序

因为一个朋友要我帮忙改个javascript计算 他朋友孩子年龄的代码,发现其中有些难度,后来用php改写了程序,计算更准确些。

header ( "Content-type: text/html; charset=utf-8" );

$weeklist = array ("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" );
$date = date ( 'Y年m月d日' );
$dateDisp = $date . $weeklist [date ( w )];

echo '今天是' . $date . '<br>';

$birthday = strtotime ( '1977-02-14' ); //生日
$haveday = floor ( (time () - $birthday) / (60 * 60 * 24) ); //出生距离现在的天数
echo '生日是'.date('Y-m-d',$birthday).'<br>';
echo '距现在是'.$haveday.'天<br>';

//计算年
if (date ( 'n' ) >= date ( 'n', $birthday ) && date ( 'j' ) >= date ( 'j', $birthday ))
 $have_year = date ( 'Y' ) - date ( 'Y', $birthday );
else
 $have_year = date ( 'Y' ) - date ( 'Y', $birthday ) - 1;
 
//计算月 
if (date ( 'n' ) >= date ( 'n', $birthday )) {
 $have_month = date ( 'n' ) - date ( 'n', $birthday );

} else {
 if (date ( 'j' ) >= date ( 'j', $birthday )) {
  $have_month = 4 + date ( 'n' );
 } else {
  $have_month = 4 + date ( 'n' ) - 1;
 }
}

if (date ( 'j' ) >= date ( 'j', $birthday )) {
 $have_day = date ( 'j' ) - date ( 'j', $birthday );
} else {
 $time_tmp = time () - date ( 'd' ) * 86400;
 $date_tmp = date ( 't', $time_tmp );
 $have_day = $date_tmp - date ( 'j', $birthday ) + date ( 'j' );
}

echo '您'.$have_year . '岁' . $have_month . '月零' . $have_day . '天啦 ^_^';
//echo $haveday;

防止 SQL injection 攻击

if($user_name == || $user_passwd == ){
        ErrMsg(“账号不能为空白”);
    }
if (!preg_match(‘/^\w+$/’, $user_name)){
        ErrMsg(“只能是数字字母”);
    }

对输入的过滤

if( !get_magic_quotes_gpc() )
{
    if( is_array($_GET) )
    {
        while( list($k, $v) = each($_GET) )
        {
            if( is_array($_GET[$k]) )
            {
                while( list($k2, $v2) = each($_GET[$k]) )
                {
                    $_GET[$k][$k2] = addslashes($v2);
                }
                @reset($_GET[$k]);
            }
            else
            {
                $_GET[$k] = addslashes($v);
            }
        }
        @reset($_GET);
    }

    if( is_array($_POST) )
    {
        while( list($k, $v) = each($_POST) )
        {
            if( is_array($_POST[$k]) )
            {
                while( list($k2, $v2) = each($_POST[$k]) )
                {
                    $_POST[$k][$k2] = addslashes($v2);
                }
                @reset($_POST[$k]);
            }
            else
            {
                $_POST[$k] = addslashes($v);
            }
        }
        @reset($_POST);
    }

    if( is_array($_COOKIE) )
    {
        while( list($k, $v) = each($_COOKIE) )
        {
            if( is_array($_COOKIE[$k]) )
            {
                while( list($k2, $v2) = each($_COOKIE[$k]) )
                {
                    $_COOKIE[$k][$k2] = addslashes($v2);
                }
                @reset($_COOKIE[$k]);
            }
            else
            {
                $_COOKIE[$k] = addslashes($v);
            }
        }
        @reset($_COOKIE);
    }
}

SMTP Reply Codes

 Code  Description
211   System status, or system help reply.
214   Help message.
220   Domain service ready.Ready to start TLS.
221   Domain service closing transmission channel.
250   OK, queuing for node node started.Requested mail action okay, completed.
251   OK, no messages waiting for node node.User not local, will forward to forwardpath.
252   OK, pending messages for node node started.
    Cannot VRFY user (e.g., info is not local), but will take message for this user and attempt delivery.
253   OK, messages pending messages for node node started.
354   Start mail input; end with <CRLF>.<CRLF>.
355   Octet-offset is the transaction offset.
421   Domain service not available, closing transmission channel.
432   A password transition is needed.
450   Requested mail action not taken: mailbox unavailable.ATRN request refused.
451   Requested action aborted: local error in processing.Unable to process ATRN request now
452   Requested action not taken: insufficient system storage.
453   You have no mail.
454   TLS not available due to temporary reason.Encryption required for requested authentication mechanism.
458   Unable to queue messages for node node.
459   Node node not allowed: reason.
500   Command not recognized: command.Syntax error.
501   Syntax error, no parameters allowed.
502   Command not implemented.
503   Bad sequence of commands.
504   Command parameter not implemented.
521   Machine does not accept mail.
530   Must issue a STARTTLS command first.Encryption required for requested authentication mechanism.
534   Authentication mechanism is too weak.
538   Encryption required for requested authentication mechanism.
550   Requested action not taken: mailbox unavailable.
551   User not local; please try forwardpath.
552   Requested mail action aborted: exceeded storage allocation.
553   Requested action not taken: mailbox name not allowed.
554   Transaction failed.

自己整理的php带密钥的加密解密类

在有些时候我们需要存贮到数据库中的内容是加密信息,比如 password,money 等敏感信息,以提高安全等级,

这里是一个带公钥的加密解密程序 ,测试是不错性能的, 

<?php
/*
字符串加密解密函数,用于mysql存贮加密解密信息
*/
class  MyClass_Crypt{
   
    //公钥
    protected $key = “123456”; 

    private function keyED($txt,$encrypt_key)
    {
        $encrypt_key = md5($encrypt_key);
        $ctr=0;
        $tmp = “”;
        for ($i=0;$i<strlen($txt);$i++)
        {
            if ($ctr==strlen($encrypt_key)) $ctr=0;
            $tmp.= substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1);
            $ctr++;
        }
        return $tmp;
    }

//$txt 是需要加密的字符串   
    public function encrypt($txt,$key=”)
    {
        if(empty($key))
            $key=$this->key;
           
        srand((double)microtime()*1000000);
        $encrypt_key = md5(rand(0,32000));
        $ctr=0;
        $tmp = “”;
        for ($i=0;$i<strlen($txt);$i++)
        {
            if ($ctr==strlen($encrypt_key)) $ctr=0;
            $tmp.= substr($encrypt_key,$ctr,1) .
            (substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1));
            $ctr++;
        }
        return $this->keyED($tmp,$key);
    }

//$txt 是需要解密的字符串   
    public function decrypt($txt,$key=”)
    {
        if(empty($key))
            $key=$this->key;
       
        $txt = $this->keyED($txt,$key);
        $tmp = “”;
        for ($i=0;$i<strlen($txt);$i++)
        {
            $md5 = substr($txt,$i,1);
            $i++;
            $tmp.= (substr($txt,$i,1) ^ $md5);
        }
        return $tmp;
    }

    public function setKey($key)
    {
        if(empty($key))
            return Null;
       
        $this->key=$key;
    }

    public function getKey()
    {
        return $this->key;
    }
   
}
/*
$string = “123456”;
$crypt= new Crypt;
echo $crypt->getKey().'<br>’;
$crypt->setKey(‘sdfsd’);
echo $crypt->getKey().'<br>’;

// encrypt $string, and store it in $enc_text
$enc_text = $crypt->encrypt($string);

// decrypt the encrypted text $enc_text, and store it in $dec_text
$dec_text = $crypt->decrypt($enc_text);
print “Original text : $string<br>\n”;
print “Encrypted text : $enc_text<br>\n”;
print “Decrypted text : $dec_text\n”;
*/
?>