星期四, 2月 23, 2012

What is my IP? (Public IP address)

Using wget
wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'
Using curl
curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'

curl -s http://whatismyip.org/
Using Lynx
lynx -dump checkip.dyndns.org

lynx -dump www.whatismyip.com | grep 'Your IP'
Reference Linux command: What is my IP? (Public IP address)

星期二, 2月 21, 2012

mysql grant

最近一個案子要連到廠商的db讀資料
不過不知卡在什麼地方,一直沒辦法連到db
後來想到是mysql沒開遠端連線的權限,想透過phpmyadmin去開
但是限制一堆,只能用ssh進mysql server下指令...
用慣了phpmyadmin.... 要用指令下授權...ㄟ... grant all db.... 什麼.... 放棄
下select就算了,下授權... 不知多久才用一次的東西...google一下吧..
  • 先連db囉
    $ mysql -u root -p
  • 查詢 某 User 的權限
    這樣就可以知道該User是否有從某ip連db的權限
    mysql> select User,Host from mysql.user;
    +------+---------------------------+
    | User | Host |
    +------+---------------------------+
    | root | localhost |
    +------+---------------------------+
    1 rows in set (0.00 sec)

  • add SELECT,INSERT privileges to a user using GRANT
    上例中,root未開放192.168.0.1的連線
    這裡假設開放root(密碼:ok1234)可從192.168.0.1連線db_base下的所有table(利用*來代表所有的table)的權限
    GRANT SELECT,INSERT ON db_base.* TO root@192.168.0.1 IDENTIFIED BY 'ok1234';

References

git增減remote及fetch and pull branch


  • 增加一個remote
    //增加一個在github的remote
    git remote add github https://github.com/username/Hello-World.git
    
    //origin也視成一個remote,所以一樣的方法,只是叫origin
    git remote add origin https://github.com/username/Hello-World.git

當origin或remote有local沒有的branhc要如何抓下來
  • 自己的
    #git branch
    * master
  • Origin或remote的
    #git branch
    check
    * master
  • 利用fetch抓回來
    #git fetch origin check:check 

星期五, 2月 10, 2012

ie中session遺失問題

先前寫個cross site的程式給廠商呼叫
結果最近在說只有ie會掛,firefox, chrome都通過
一試之下,還真的同事的全掛,最有趣的是我的ie卻能過
且我ie 7,8,9全試了,都可以,為什麼.. 還沒查出來

因為ie不能看iframe裡的內容,而自己的電腦又不會有問題
只能請同事看,查到快起肖了
最後發現是session值會不見

看到有人提到session的useragent不同,而ci裡的config會比對,只要關掉即可
可是還是失敗,總之查了半天,突然看到P3P的字眼...
才想到以前也有遇到ie跨網域取不到相關值的問題,加上後就解決了
friday night加班到9點...更~~~~~~~~~~~~

網上有人整理好了
  • How to set p3p http header in ASP.Net?
    HttpContext.Current.Response.AddHeader("p3p", "CP=\""IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT\""")
  • How to set p3p http header in PHP?
    header('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"');
  • How to set p3p http header in JSP?
    response.setHeader("P3P","CP='IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT'")
  • How to set p3p http header in ColdFusion?
    <cfheader name="P3P" value="CP='IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT'">

References

星期四, 2月 09, 2012

利用 php 本身寫 error log 及 error/warning/notice message

  • error_reporting
    設定php回發生錯誤的等級
    error_reporting(0);  // Turn off all error reporting
    ini_set('error_reporting', E_ALL);  // Same as error_reporting(E_ALL);
  • error_log
    bool error_log ( string $message [, int $message_type = 0 [, string $destination [, string $extra_headers ]]] )
    Sends an error message to the web server's error log or to a file.
    需寫入file,因此需指定file path
    用法
    //log to file
    ini_set('error_log', dirname(__FILE__) . '/error_log.txt');  
    
    // Send notification through the server log if we can not
    // connect to the database.
    if (!Ora_Logon($username, $password)) {
        error_log("Oracle database not available!", 0);
    }
    
    // Notify administrator by email if we run out of FOO
    if (!($foo = allocate_new_foo())) {
        error_log("Big trouble, we're all out of FOOs!", 1,
                   "operator@example.com");
    }
    
    // another way to call error_log():
    error_log("You messed up!", 3, "/var/tmp/my-errors.log");
    
  • error/warning/notice message
    定義log level多配合log4php,不過也可透過trigger_error
    即可靠php本身達成此效果
    trigger_error("Notice Message",E_USER_NOTICE);
    trigger_error("Warning Message",E_USER_WARNING);
    trigger_error("Error Message",E_USER_ERROR);
    如此就不用再掛log4php,就能方便帶訊息
也就是說error_report 是設定php觸發回報錯誤的等級
如果想利用此機制寫自定的訊息就可透過trigger_error
而error_log就自己想log什麼就log什麼

error_report設定篇
開啟是否顯示error及log的等級
  • 在php.ini中設定
    display_errors = On 
    
  • 在.php中設定
    每次要到php.ini設定就太累了,這邊有方法可以直接在.php裡,直接設定log,這樣開發起來就方便多了,且不會動到整體環境
    ini_set('display_errors', 1);   //turn on display error on screen
    ini_set('log_errors', 1);     //turn on log error
    error_reporting(E_ALL); //log all errors and warnings


常見常數定義
列出常見的幾個,其他看官網Predefined Constants
Constant Description Note
E_NOTICERun-time notices. Indicate that the script encountered something that could indicate an error, but could also happen in the normal course of running a script.
E_STRICTEnable to have PHP suggest changes to your code which will ensure the best interoperability and forward compatibility of your code.Since PHP 5 but not included in E_ALL until PHP 5.4.0
E_ALLAll errors and warnings, as supported, except of level E_STRICT prior to PHP 5.4.0.

References