2014-03-28

document.form1.submit(); 不能動作

http://www.neo.com.tw/archives/000312.html 

[Javascript] 請勿用保留字來取 HTML 的標籤名 

剛剛查一個 javascript 的 bug ,原因是 document.form1.submit(); 不能動作。出現的錯誤訊息是「物件不支援此屬性或方法」。 

這種 document.form1.submit(); 的 javascript 是再平常不過的指令,怎麼可能不支援呢? 

後來才發現,是美工把 submit 的按鈕取名叫 submit,也就是在 HTML 裡面有一行是: 

<input type="submit" name="submit" value="submit"> 

這樣 document.form1.submit(); 就無法執行了,因為 document.form1.submit 已經變成那個按鈕的物件了,自然 submit 的 method 也無法動作。 

因為自己不會犯這種用保留字來取 HTML Tag name 的錯誤,不過遇到對程式不熟的美工,還是會遇到這種狀況,只能自求多福了。

2014-03-26

PHP獲取路徑和目錄方法總結

<? php
/**
 * PHP獲取路徑或目錄實現
 * @link http://www.phpddt.com
 */

//魔術變量,獲取當前文件的絕對路徑
echo "__FILE__: ========> " . __FILE__ ;
echo '<br/>' ;

//魔術變量,獲取當前腳本的目錄
echo "__DIR__: ========> " . __DIR__ ;
echo '<br/>' ;

//dirname返迴路徑的目錄部分,dirname(__FILE__)相當於__DIR__
echo "dirname(__FILE__): ========> " . dirname ( __FILE__ );
echo '<br/>' ;

//$_SERVER['PHP_SELF']和$_SERVER['SCRIPT_NAME']的結果一般相同,他們都是獲取當前腳本的文件名
//只有當php以cgi方式運行時有區別,但是現在幾乎找不到以cgi方式運行php了
echo '$_SERVER["PHP_SELF"]: ========> ' . $_SERVER [ 'PHP_SELF' ];
echo '<br/>' ;

echo '$_SERVER["SCRIPT_NAME"]: ========> ' . $_SERVER [ 'SCRIPT_NAME' ];
echo '<br/>' ;

//當前執行腳本的絕對路徑。記住,在CLI方式運行php是獲取不到的
echo '$_SERVER["SCRIPT_FILENAME"]: ========> ' . $_SERVER [ 'SCRIPT_FILENAME' ];
echo '<br/>' ;

//當前運行腳本所在的文檔根目錄。在服務器配置文件中定義。
echo '$_SERVER["DOCUMENT_ROOT"]: ========> ' . $_SERVER [ 'DOCUMENT_ROOT' ];
echo '<br>' ;

//getcwd()返回當前工作目錄
echo "getcwd(): ========> " . getcwd ();
echo '<br>' ;

echo '<br>' ;
echo "PHP點點通(www.phpddt.com)整理" ;
轉載請註明地址: http://www.phpddt.com/php/get-path.html尊重他人勞動成果就是尊重自己!

2014-03-18

PHP TELNET PTT

class Telnet {
 var $sock = NULL;

 function telnet($host,$port) {
  $this->sock = fsockopen($host,$port);
  socket_set_timeout($this->sock,2,0);
 }

 function close() {
  if ($this->sock)  fclose($this->sock);
  $this->sock = NULL;
 }

 function write($buffer) {
  $buffer = str_replace(chr(255),chr(255).chr(255),$buffer);
  fwrite($this->sock,$buffer);
 }

 function getc() {
  return fgetc($this->sock); 
 }

 function read_till($what) {
  $buf = '';
  while (1) {
   $IAC = chr(255);

   $DONT = chr(254);
   $DO = chr(253);

   $WONT = chr(252);
   $WILL = chr(251);

   $theNULL = chr(0);

   $c = $this->getc();

   if ($c === false) return $buf;
   if ($c == $theNULL) {
    continue;
   }

   if ($c == "1") {
    continue;
   }

   if ($c != $IAC) {
    $buf .= $c;

    if ($what == (substr($buf,strlen($buf)-strlen($what)))) {
     return $buf;
    }
    else {
     continue;
    }
   }

   $c = $this->getc();

   if ($c == $IAC) {
   $buf .= $c;
   }
   else if (($c == $DO) || ($c == $DONT)) {
    $opt = $this->getc();
    // echo "we wont ".ord($opt)."\n";
    fwrite($this->sock,$IAC.$WONT.$opt);
   }
   elseif (($c == $WILL) || ($c == $WONT)) {
    $opt = $this->getc();
    //echo "we dont ".ord($opt)."\n";
    fwrite($this->sock,$IAC.$DONT.$opt);
   }
   else {
    //echo "where are we? c=".ord($c)."\n";
   }
  }
 }
}