XAMPP連結SQL SERVER方式

XAMPP(也有免安裝版本) 是目前Windows上面最熱門的php開發環境套件,唯一美中不足的是無法連結MS SQL。至於有沒有解決方式?當然是有的囉!

一、先看一下目前XAMPP的phpinfo(),看到without-mssql…..一開始有點嚇到!不過別警張

二、到微軟網站下載Microsoft Drivers for PHP for SQL Server,對應的版本是

  • Version 3.2 supports PHP 5.6, 5.5, and 5.4
  • Version 3.1 supports PHP 5.5 and 5.4
  • Version 3.0 supports PHP 5.4.

我們的版本是5.6,所以下載SQLSRV32.EXE即可。下載後按右鍵解壓縮。

xampmssql0024.接下來我們確認一下PHP是ThreadSafe或是non-ThreadSafe版本,到XAMPP\php目錄下,找php.dll版本。看到是php5ts.dll(PHP5 ThreadSafe)。

五、確定版本後,回到剛才解開的檔案複製以下的檔案到xampp\php\ext目錄下

xampmssql004

六、接著編輯php.ini,尋找extension=區塊,在下面加上二行(注意.dll名稱是你剛才copy過去的版本名稱)

extension=php_pdo_sqlsrv_56_ts.dll
extension=php_sqlsrv_56_ts.dll

xampmssql005

 

xampmssql006

七、存檔後重新開啟APACHE

xampmssql008 xampmssql007

 

八、回到phpinfo()頁面,如果安裝成功可以搜尋到sqlsrv跟pdo_sqlsrvxampmssql010 xampmssql009九、接著就可以連線到sql資料庫(本文以CodeIgniter為例)

xampmssql012 xampmssql011

xampmssql013
$this->load->database();
$query = $this->db->get(‘cmed’)->result_array();
array_walk_recursive($query, function(&$value, $key) {
if(is_string($value)) {
$value = urlencode($value);
}
});
$json = urldecode(json_encode($query));
echo $json;

 

備註:

Microsoft® ODBC Driver 11 for SQL Server® – Windows

Microsoft ODBC Driver For SQL Server 提供從 Windows 到 Microsoft SQL Server 的原生連線能力。

 

2015.8.29 新增免安裝整合包,下載解壓縮後執行xampp-control 即可運作。

按我下載

 

 

 

 

CodeIgniter3.0將Session寫入DB

[sql]
CREATE TABLE IF NOT EXISTS `ci_sessions` (
`id` varchar(40) NOT NULL,
`ip_address` varchar(45) NOT NULL,
`timestamp` int(10) unsigned DEFAULT 0 NOT NULL,
`data` blob NOT NULL,
PRIMARY KEY (id),
KEY `ci_sessions_timestamp` (`timestamp`)
);
[/sql]
修改 application/config/config.php
[php]
$config[‘sess_driver’] = ‘database’;
$config[‘sess_save_path’] = ‘ci_sessions’;
[/php]

讓資料庫密碼加上鹽(Salt)值

以往許多人喜歡在資料庫的密碼欄使用明碼儲存,但站在資訊安全角度來說不是挺安全的,可以產生一個Salt值後,增加安全性。
[php]
// 隨機產生一組長度為10的Salt值
$salt = substr(md5(uniqid(rand(), true)), 0, 10);
// 存入資料庫的密碼欄,使用Salt值加上md5雜湊後存回資料庫
$new_password = md5(md5($old_password.$salt));
[/php]
使用者登入後,先從資料庫讀出salt值,接著再一樣加上salt值加上md5後比對就可以了!

CodeIgniter使用PHPExcel製作Excel

1 下載PHPExcel,將PHPExcel解壓縮到Application的third_party目錄下
2 在libraries目錄下新增 excel.php
[php]
<?php
if ( ! defined(‘BASEPATH’)) exit(‘No direct script access allowed’);

require_once APPPATH."/third_party/PHPExcel.php";

class Excel extends PHPExcel {
public function __construct() {
parent::__construct();
}
}
?>
[/php]
使用
[php]
function createexcel(){
// 產生php 範例
$this->load->library(‘excel’);
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue(‘A1’, ‘中文’);
$objPHPExcel->getActiveSheet()->setCellValue(‘B2’, ‘許’);
$objPHPExcel->getActiveSheet()->setCellValue(‘C3’, ‘test3’);
$objPHPExcel->getActiveSheet()->setCellValue(‘D3’, ‘test4’);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel2007’);

header(‘Content-Type: application/vnd.ms-excel’);
header(‘Content-Disposition: attachment;filename="excel_report_’ . date(‘ymd’) . ‘.xls"’);
// header(‘Content-Disposition: attachment; filename="file.xls"’);
header(‘Cache-Control: max-age=0’);

$objWriter->save(‘php://output’);

}
[/php]

PHP透過curl上傳xml給API

[php]

<?php
$xml_data ='<?xml version="1.0" encoding="UTF-8"?>
<FILE>
<HEAD>
<FILEDESC>ORDER</FILEDESC>
</HEAD>
<CONTENT>
<DATA>
<ID>XXXX</ID>
<MEMO>XXXXXXX</MEMO>
</DATA>
</CONTENT>
</FILE>
‘;

$URL = "http://apisite";

$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Content-Type: text/xml’));
curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml_data");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
[/php]

CodeIgniter發送台灣簡訊範例

[php]
<?php
$username = "xxx"; // 帳號
$password = "xxx"; // 密碼
$mobile = "09xxxxxxxx"; // 電話
$message = "測試簡訊"; // 簡訊內容
$MSGData = "";

$msg = "username=".$username."&password=".$password."&mobile=".$mobile."&message=".urlencode($message)."&drurl=".urlencode("http://xxxx.com.tw/admin/drul/");
$num = strlen($msg);

// 打開 API 閘道
$fp = fsockopen ("api.twsms.com", 80);
if ($fp) {
$MSGData .= "POST /smsSend.php HTTP/1.1\r\n";
$MSGData .= "Host: api.twsms.com\r\n";
$MSGData .= "Content-Length: ".$num."\r\n";
$MSGData .= "Content-Type: application/x-www-form-urlencoded\r\n";
$MSGData .= "Connection: Close\r\n\r\n";
$MSGData .= $msg."\r\n";
fputs ($fp, $MSGData);
$Tmp ="";
// 取出回傳值
while (!feof($fp)) $Tmp.=fgets($fp,128);
$temp2 = explode(PHP_EOL, $Tmp);
// 關閉閘道
fclose ($fp);
/*
0 傳送完成:HTTP/1.1 200 OK
1 Date: Wed, 03 Jun 20xx 09:31:36 GMT
2 Server: Apache
3 X-Powered-By: PHP/5.2.12
4 Connection: close
5 Transfer-Encoding: chunked
6 Content-Type: text/html
7
8 51
9 <smsResp><code>00000</code><text>Success</text><msgid>188xxxx69</msgid></smsResp>
10 0
*/
$xml = simplexml_load_string($temp2[9]);
$xmldata = json_decode( json_encode($xml) , 1);
echo "傳送完成:".$Tmp."<br/>".$xmldata[‘msgid’];
} else {
echo "您無法連接 TwSMS API Server";
}
$this->load->view(‘welcome_message’,$data);

}

public function drul(){
$this->load->database();
$inputdata = $this->input->get(‘xml’);
$xml = simplexml_load_string($inputdata);
$xmldata = json_decode( json_encode($xml) , 1);
$arraystring =print_r($xmldata);
$this->msg=$arraystring;

$dbvalue = array(
‘sms_msg’=> $xmldata[‘msgid’]."status=".$xmldata[‘statustext’],
‘id’ =>$xmldata[‘code’],
‘ip’ => $this->input->ip_address(),
);
$this->db->insert(‘drul’,$dbvalue );
$data = array(
‘message’ => $xmldata[‘msgid’]
);
$this->load->view(‘welcome_message’,$data);
}
[/php]

CodeIgniter超簡易發信

[php]
$email_config = Array(
‘mailtype’ => ‘html’

);
$this->load->library(’email’,$email_config);
$this->email->from(‘superlevin@gmail.com’, ‘林壽山’); //寄件者
$this->email->to(‘123@gmail.com’); // 收件者
//$this->email->cc(‘123cc@gmail.com’); // 副本
//$this->email->bcc(‘123bcc@gmail.com’); // 密件副本

$this->email->subject(‘成功通知信’); // 主旨
$this->email->message(‘<b>報名成功</b>’); //內容
$this->email->send();
[/php]

CodeIgniter上傳圖檔以及縮圖

[php]
// 縮圖
function _createThumbnail($fileName, $isThumb, $thumbMarker="", $width, $height) {
// 參數
$config[‘image_library’] = ‘gd2’;
$config[‘source_image’] = $fileName;
$config[‘create_thumb’] = $isThumb;
$config[‘maintain_ratio’] = TRUE;
$config[‘master_dim’] = ‘width’;
if(isset($thumbMarker) && $thumbMarker!=""){
$config[‘thumb_marker’] = $thumbMarker;
}
$config[‘width’] = $width;
$config[‘height’] = $height;

$this->load->library(‘image_lib’, $config);
$this->image_lib->clear();
$this->image_lib->initialize($config);
if(!$this->image_lib->resize()) echo $this->image_lib->display_errors();
}
[/php]
上傳
[php]
function uploadimage(){
$name = $this->input->get_post(‘my_file’);
$config[‘upload_path’] = ‘./uploads/’;
$config[‘allowed_types’] = ‘gif|jpg|png’;
$config[‘file_name’] = $name;
$config[‘overwrite’] = true;
$config[‘max_size’] = 4096;
//$config[‘max_width’] = 5000;
//$config[‘max_height’] = 5000;
$this->load->library(‘upload’, $config);
if ( ! $this->upload->do_upload())
{
$data[‘errormsg’] = array(‘error’ => $this->upload->display_errors());
$this->load->view(‘welcome_message’, $data);
}
else
{
$fInfo = $this->upload->data();
$this->_createThumbnail($fInfo[‘full_path’],TRUE,"",330,480);
$this->_createThumbnail($fInfo[‘full_path’],TRUE,"_tn",110,160);

$data[‘filename’]=$this->upload->data(‘file_name’);
$this->load->view(‘welcome_message’,$data);
}

}
[/php]

CodeIgniter解決CSS路徑問題

使用CodeIgniter撰寫網頁很方便,不過會遇到的問題常常是css、image的路徑問題。
除了使用絕對路徑外,也可以使用HTML base Tag來指定基準的URL。
[php]
// 記得要load helper url
$this->load->helper(‘url’);
// 在 view 中的HTML加上
<base href="<?php echo base_url();?>"/>
[/php]