[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]
分類: CodeIgniter
讓資料庫密碼加上鹽(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]
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]
CodeIgniter使用Securimage驗證碼方式
1 https://www.phpcaptcha.org/ 下載後解壓縮到application\libraries\securimage
2 controller中新增function
[php]
function securimage() {
$this->load->library(‘securimage’);
$img = new Securimage();
$img->show();
}
[/php]
3 view裡面增加
[html]
<img id="captcha" src="<?=site_url(‘[controller classname]/securimage’)?>" alt=’captcha’ id=’captcha’ />
<a href="#" onclick=" document.getElementById(‘captcha’).src = document.getElementById(‘captcha’).src + ‘?’ + (new Date()).getMilliseconds()">重新產生驗證碼</a>
[/html]
4 驗證
[php]
$captchacode= $this->input->post(‘captchacode’);
$this->load->library(‘securimage’);
if ($this->securimage->check($captchacode)==true){
redirect(‘index’);
}
[/php]
CodeIgniter學習筆記 – 安裝
從主站(英文)或中文站下載CodeIgniter_x.x.x.zip,解壓縮將index.php及application、system二個目錄上傳到主機。
1.為了安全性,建議把application、system二個目錄移到系統目錄
但需要修改 index.php
$system_path = ‘system’;
$application_folder = ‘application’;
2.如果需要資料庫設定,修改application\config\database.php
$db[‘default’][‘hostname’] = ‘localhost’;
$db[‘default’][‘username’] = ”;
$db[‘default’][‘password’] = ”;
$db[‘default’][‘database’] = ”;
如果沒有問題的話就可以看到下面的頁面。
相關連結:
CodeIgniter偵測使用者瀏覽器
方式一:使用user_agent跟redirect
[php]
$this->load->library(‘user_agent’);
$this->load->helper(‘url’);
if ($this->agent->browser() == ‘Internet Explorer’ and $this->agent->version() <= 7)
redirect(‘/unsupported-browser’);
[/php]
方式二:使用 http://mobiledetect.net
[php]
$this -> load -> library(‘Mobile_Detect’);
$detect = new Mobile_Detect();
if ($detect->is(‘Chrome’) || $detect->is(‘iOS’)) {
}
[/php]