顯示具有 PHP 標籤的文章。 顯示所有文章
顯示具有 PHP 標籤的文章。 顯示所有文章

2010年11月29日 星期一

[PHP] 解決 date(): It is not safe to rely on the system's timezone settings

Message:  date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function.

解決方法:

修改php.ini, 加上預設timezone, 重新啟動Apache即可

date.timezone = "Asia/Taipei"

2010年11月25日 星期四

[PHP] 解決使用Windows IIS執行PHP時的檔案上傳限制

今天在解決一個PHP檔案大小上傳限制的問題


系統是Windows 2008 Server
Web Server是IIS


原以為跟Apache一樣, 改一下php.ini的兩個設定即可



post_max_size = 10M
upload_max_filesize = 10M



沒想到開phpinfo出來看還是沒有作用, Google一下發現, 原來Windows Server本身基於安全性問題, 自己就有限制。


解決方法如下:


Step 1. 系統管理工具 -> 服務 (或是直接用 執行 -> services.msc)


Step 2. 停止IIS Admin ServiceWorld Wide Web Publishing Service服務 (否則修改檔案後要存檔時, 他會說有服務在使用)


Step 3. 用文字編輯器開啟 C:\Windows\system32\inetsrv\metabase.xml


Step 4. 尋找 AspMaxRequestEntityAllowed,原本預設為 204800 (代表 200 KB)

Step 5. 我的需求為10M 所以我改成 10240000

Step 6.存檔後重新啟動上述兩個服務即可



2010年11月21日 星期日

[phpmyadmin] Windows 7 phpmyadmin首頁空白 無法登入

今天灌好Apache + MySQL + PHP時, 一直用phpmyadmin一直無法登入

最後到網路上去查才發現原來Windows 7的hosts預設是空的

hosts的路徑在 C:\Windows\System32\drivers\etc\hosts



把127.0.0.1前面的# 拿掉存檔就可以了

2010年11月2日 星期二

[Codeigniter] 使用URI Routing來做網址自訂及SEO

URI Routing
http://codeigniter.com/user_guide/general/routing.html


上周在思考製作Blog時, 如何讓網址可以一個name對應到一個post_id

假設我有一個網址

http://localhost/post/list/10

我想要他不要每篇post都只是跟隨著post_id

我可以利用route來做

在route.php中加入
$route['post/cool'] = "post/list/10";

這樣我就可以用
http://localhost/post/cool

來mapping到post/list/10

來達到SEO的效果。

2010年11月1日 星期一

[PHP] 用microtime 記錄PHP執行時間

在PHP的開頭貼上:
$mtime = explode(" ", microtime()); 
$startTime = $mtime[1] + $mtime[0];

在結尾貼上

$mtime = explode(" ", microtime()); 
$endTime = $mtime[1] + $mtime[0]; 
$totalTime = ($endTime - $startTime); 
echo $totalTime." seconds";


就會顯示如
3.99565601349 seconds
的結果

2010年10月16日 星期六

[PHP] 延長phpmyadmin的 timeout時間

在PHP開發階段使用到phpmyadmin, 常會發生過一段時間後, phpmyadmin timeout了, 又要重新登入一次, 實在是很麻煩, 所以調高timeout時間, 較為方便, 做法如下:

修改config.inc.php

$cfg['LoginCookieValidity'] = 7200; // In seconds

2010年10月15日 星期五

[PEAR] Windows使用PHP安裝PEAR遇到failed to open stream: phar error

C:\php>go-pear.bat

在Windows上使用PHP安裝PEAR時遇到錯誤

Warning: require_once(phar://go-pear.phar/index.php): failed to open stream: phar error: invalid url or non-existent phar "phar://go-pear.phar/index.php" in C:\php\PEAR\go-pear.phar on line 1236


解決方法:

修改php.ini

找到

;phar.require_hash = On

改成

phar.require_hash = Off

再安裝一次
C:\php>go-pear.bat

就可以跑了~

2010年9月16日 星期四

[CodeIgniter] Show all session userdata 列出所有session

使用CI 時, 在http://codeigniter.com/user_guide/libraries/sessions.html

找不到列出所有Session key & value的方法

後來直接到CI Library打開Session.php

發現裡面有個方法叫 all_userdata


/**
  * Fetch all session data
  *
  * @access public
  * @return mixed
  */
 function all_userdata()
 {
  return ( ! isset($this->userdata)) ? FALSE : $this->userdata;
 }

所以他有提供這方法, 只是Document沒有寫

使用方法
$this->session->all_userdata();

2010年9月15日 星期三

[PHP] 執行緒安全Thread Safe or None-Thread Safe ?

None-Thread Safe就是非執行緒安全,在執行時不進行執行緒(thread)安全檢查;
Thread Safe就是執行緒安全,執行時會進行執行緒(thread)安全檢查,以防止有新要求就啟動新執行緒的 CGI 執行方式耗盡系統資源。

PHP執行有兩種方式:ISAPI和FastCGI。

FastCGI執行方式是以單一執行緒來執行操作,所以不需要進行執行緒的安全檢查,除去執行緒安全檢查的防護反而可以提高執行效率,所以,如果是以 FastCGI(無論搭配 IIS 6 或 IIS 7)執行 PHP ,都建議下載執行 non-thread safe 的 PHP。

而執行緒安全檢查正是為ISAPI方式的PHP準備的,因為有許多php模塊都不是執行緒安全的,所以需要使用Thread Safe的PHP。

[PHP] 我該選擇哪個版本的PHP on Windows

在PHP的官網上有兩種Windows Binaries的版本for windows, 翻譯以下官方原文讓不知道該下載哪個的人參考


我該選擇哪個版本?
Which version do I choose?


如果你用Apache1 和 Apache2, 你要用VC6版本的PHP
If you are using PHP with Apache 1 or Apache2 from apache.org you need to use the VC6 versions of PHP


如果你在IIS上用PHP, 你需要VC9版本的PHP
If you are using PHP with IIS you should use the VC9 versions of PHP


VC6 版本是用古老的Visual Studi 6編譯器來編譯
VC6 Versions are compiled with the legacy Visual Studio 6 compiler


VC9 版本是用Visual Studio 2008編譯器來編譯, 改進了效能且更加穩定
VC9 Versions are compiled with the Visual Studio 2008 compiler and have improvements in performance and stability.


VC9  版本要求安裝 Microsoft 2008 C++ Runtime (x86) or the Microsoft 2008 C++ Runtime (x64) 執行環境
The VC9 versions require you to have the Microsoft 2008 C++ Runtime (x86) or the Microsoft 2008 C++ Runtime (x64)installed


別在Apache使用VC9 的版本
Do NOT use VC9 version with apache.org binaries

2010年9月3日 星期五

[PHP] 你的PHP程式是否會產生Y2K38的Bug?

原本的時間取得方式會導致Y2K38

<?php
$date = '2040-02-01';
$format = 'l d F Y H:i';
$mydate1 = strtotime($date);
echo date($format, $mydate1);
?>

用PHP 5.2.0 後的新的DateTime Class來修正

<?php  
$date = '2040-02-01';  
$format = 'l j F Y H:i';  
$mydate2 = new DateTime($date);  
echo $mydate2->format($format);
?>

不過2038還那麼久, 現在擔心好像還太早, 目前很難出現災情, 不然以後都習慣用DateTime Class就好了

2010年8月11日 星期三

[CodeIgniter] 使用Model時, 檔案名稱要用小寫

避免在不同的作業系統(Linux, Windows) Server大小寫錯誤。
The file name will be a lower case version of your class name. For example, if your class is this:
class User_model extends Model {

    function User_model()
    {
        parent::Model();
    }
}
Your file will be this:
application/models/user_model.php
都怪我沒把說明文件看清楚= =" 除錯除很久~

2010年8月10日 星期二

[PHP] convert date to timestamp 時間戳記

http://php.net/manual/en/function.strtotime.php
strtotime  Parse about any English textual datetime description into a Unix timestamp


Datetime to Timestamp

$timestamp = strtotime($datetime);


Timestamp to Datetime

date('Y-m-d', $timestamp);


<?php 
    // Calculate the difference in days.  
    $date1 = strtotime(2010-10-10);
    $date2 = strtotime(2011-12-01);
    $daysDiff = ($date2 - $date1)/(24 * 60 * 60);

    // Which is the latest?
    if ($date2 > $date1) {
      echo "$date2 is later than $date1";
    }
?>

可以用以上Code來作時間前後的比較, But if you do, your code will be susceptible to the 2038 bug.

2010年8月2日 星期一

[PHP] Visibility

http://www.php.net/manual/en/language.oop5.visibility.php

<?php
/**
 * Define MyClass
 */
class MyClass
{
    public $public = 'Public';
    protected $protected = 'Protected';
    private $private = 'Private';

    function printHello()
    {
        echo $this->public."<br>";
        echo $this->protected."<br>";
        echo $this->private."<br>";
    }
}

$obj = new MyClass();
echo $obj->public."<br>"; // Works
//echo $obj->protected."<br>"; // Fatal Error
//echo $obj->private."<br>"; // Fatal Error
$obj->printHello(); // Shows Public, Protected and Private


/**
 * Define MyClass2
 */
class MyClass2 extends MyClass
{
    // We can redeclare the public and protected method, but not private
    protected $protected = 'Protected2';

    function printHello()
    {
        echo $this->public."<br>";
        echo $this->protected."<br>";
        echo $this->private."<br>";
    }
    
    function parentHello()
    {
     parent::printHello();
    }
}

$obj2 = new MyClass2();
echo $obj2->public."<br>"; // Works
echo $obj2->private."<br>"; // Undefined
//echo $obj2->protected."<br>"; // Fatal Error
$obj2->printHello(); // Shows Public, Protected2, Undefined
$obj2->parentHello();
?>


Result:
Public
Public
Protected
Private
Public
Notice: Undefined property: MyClass2::$private in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\global.php on line 49 
Public
Protected2
Notice: Undefined property: MyClass2::$private in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\global.php on line 38 
Public
Protected2
Private

[PHP] Variables Scope

變數範圍
http://php.net/manual/en/language.variables.scope.php

This can cause some problems in that people may inadvertently change a global variable.
這是為了避免無意間改變到全域變數。

<?php

$a=123; /* global scope 全域範圍 */

function test() {
 echo $a; /* reference to local scope variable
       so output nothing */
 
 echo $GLOBALS['a']; // 123
 
 global $a;
 echo $a; // 123
 
 $a = 456;
 echo $a; // 456
}

test();
?>

2010年8月1日 星期日

[PHP] substr_count 計算某一長字串中, 短字串的出現次數

substr_count  Count the number of substring occurrences
PHP Manual - substr_count



Description

int substr_count ( string $haystack , string $needle [, int $offset = 0 [, int $length ]] )


Example:

<?php
echo substr_count("不看就不看不看就不看不看就不看(有幾個不看?)", "不看");
?>
 
 
 


Result:

7

2010年7月30日 星期五

[CodeIgniter] 使用rewrite URLs

Reference


Step 1 : 

使用mod_rewrite : 開啟Apache mod_rewirte Module
1. 打開httpd.conf

Options FollowSymLinks
AllowOverride All

2. 找到 #LoadModule rewrite_module modules/mod_rewrite.so
3. # 字號拿掉後存檔, restart Apache Server即可開啟

Step 2 : 設定 .htaccess 檔

其中的
RewriteBase /
要改成自己的子目錄

2010年7月27日 星期二

[PHP] CodeIgniter

http://codeigniter.com/user_guide/overview/at_a_glance.html

http://codeigniter.com/wiki/Special:Titles


CodeIgniter Features

  • Model-View-Controller Based System
    • MVC軟體架構
    image by wiki
  • PHP 4 Compatible
    • 相容 PHP 4
  • Extremely Light Weight
    • 極輕量, 檔案非常小
  • Full Featured database classes with support for several platforms.
    • 支援各種資料庫
  • Active Record Database Support
    • 一般情況下使用SQL語法來存取資料庫,而透過Active Record則可以簡易的使用物件的方式來存取資料庫,不需要經過任何連接、宣告,Active Record自動會去找尋對應的資料表名稱,然後直接建立物件函式,將資料庫存取簡化到一個極致。
  • Form and Data Validation
    • 表單與資料驗證
  • Security and XSS Filtering
    • 安全性與XSS(Cross-site scripting)過慮
  • Session Management
    • Session管理
  • Email Sending Class. Supports Attachments, HTML/Text email, multiple protocols (sendmail, SMTP, and Mail) and more.
    • 寄送Email類別, 支援附加檔案, HTML/Text格式。支援多種協定(sendmail, SMTP, and Mail) 還有更多。
  • Image Manipulation Library (cropping, resizing, rotating, etc.). Supports GD, ImageMagick, and NetPBM
    • 圖片處理函式庫 (裁切, 變更尺寸, 旋轉...等),  支援GD, ImageMagick 和 NetPBM
  • File Uploading Class
    • 檔案上傳類別
  • FTP Class
    • FTP類別
  • Localization
    • 多語言
  • Pagination
    • 分頁
  • Data Encryption
    • 資料加密
  • Benchmarking
  • Full Page Caching
    • 網頁快取
  • Error Logging
    • 錯誤記錄
  • Application Profiling
    • 應用程式效能分析
  • Scaffolding
  • Calendaring Class
    • 日曆類別
  • User Agent Class
    • 瀏覽器標頭類別
  • Zip Encoding Class
    • 壓縮編碼類別
  • Template Engine Class
    • 模板引擎類別
  • Trackback Class
    • 實現了網站之間的互相通告;因此,它也可以提供提醒功能。
  • XML-RPC Library
    • 使用http協議,通過xml將調用函數封裝的一種遠端過程調用方式,它為服務端和客戶端的信息交換提供了一種很簡單方法。
  • Unit Testing Class
    • 單元測試
  • Search-engine Friendly URLs
    • 友善的網址(搜尋引擎上)
  • Flexible URI Routing
    • 靈活的 URI Routing
  • Support for Hooks, Class Extensions, and Plugins
    • 支援Hooks, 類別延伸及外掛
  • Large library of "helper" functions
    • 非常龐大的"helper"函數支援

2008年8月21日 星期四

BigDump - 載入大檔案的sql檔到MySQL

今天要載入一個近300Mb的sql檔到MySQL時,想到三個方法

1. phpMyAdmin
因為PHP檔案上傳限制原因,而且要將那麼大的sql檔傳完也蠻久的。
所以在不更改系統伺服器設定的前提下,此方法不可行

2. MySQL Tools - MySQL Query Browser
Open SQL檔時,整個畫面都沒出現東西,預估是檔案過大。所以不可行

3. 用命令提示字元,直接連結資料庫匯入
mysql -u root -p myDB < myDB.sql
這方法是可行的,但是完全沒有顯示進度狀況,只能枯等,等他跑完。

4. BigDump: Staggered MySQL Dump Importer v0.29b
http://www.ozerov.de/bigdump.php
這是在網路上別人開發的PHP程式,在載入SQL時會顯示進度報告。
但是如果不是用PHP的人就無法使用。


希望之後可以發現更好的方法。因為有機會下次要載入的檔案可能是Gb以上了。
Related Posts Plugin for WordPress, Blogger...