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

2016-01-07

php imap_errors 解決方式

解封權限即可
https://www.google.com/settings/security/lesssecureapps

https://accounts.google.com/DisplayUnlockCaptcha

2014-07-25

PHP中imagecopyresampled參數解說

bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
 
$dst_image:新建的图片
 
$src_image:需要载入的图片
 
$dst_x:设定需要载入的图片在新图中的x坐标
 
$dst_y:设定需要载入的图片在新图中的y坐标
 
$src_x:设定载入图片要载入的区域x坐标
 
$src_y:设定载入图片要载入的区域y坐标
 
$dst_w:设定载入的原图的宽度(在此设置缩放)
 
$dst_h:设定载入的原图的高度(在此设置缩放)
 
$src_w:原图要载入的宽度
 
$src_h:原图要载入的高度

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";
   }
  }
 }
}

2014-02-08

PHP發送POST請求的三種方式

class Request{

    public static function post($url, $post_data = '', $timeout = 5){

        $ch = curl_init();

        curl_setopt ($ch, CURLOPT_URL, $url);

        curl_setopt ($ch, CURLOPT_POST, 1);

        if($post_data != ''){

            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

        }

        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

        curl_setopt($ch, CURLOPT_HEADER, false);

        $file_contents = curl_exec($ch);

        curl_close($ch);

        return $file_contents;

    }


    public static function post2($url, $data){

     

        $postdata = http_build_query(

            $data

        );

     

        $opts = array('http' =>

                      array(

                          'method'  => 'POST',

                          'header'  => 'Content-type: application/x-www-form-urlencoded',

                          'content' => $postdata

                      )

        );

     

        $context = stream_context_create($opts);


        $result = file_get_contents($url, false, $context);

        return $result;


    }


    public static function post3($host,$path,$query,$others=''){


        $post="POST $path HTTP/1.1\r\nHost: $host\r\n";

        $post.="Content-type: application/x-www-form-";

        $post.="urlencoded\r\n${others}";

        $post.="User-Agent: Mozilla 4.0\r\nContent-length: ";

        $post.=strlen($query)."\r\nConnection: close\r\n\r\n$query";

        $h=fsockopen($host,80);

        fwrite($h,$post);

        for($a=0,$r='';!$a;){

                $b=fread($h,8192);

                $r.=$b;

                $a=(($b=='')?1:0);

            }

        fclose($h);

        return $r;

    }
}

2014-01-28

PHP DCOM 爬文網頁,自動登入

ref:http://www.blueshop.com.tw/board/FUM20041006152627A9N/BRD20130417225944UFH/2.html
<?php
$browser = new COM("InternetExplorer.Application") or die("can not start internet explorer");
$browser->Visible = true;
$browser->Navigate("http://php.a168a.com/andyto202/login.php");
while (($browser->Busy) || ($browser->readystate != 4) ) {
    com_message_pump(1000);
}
     $browser->Document->passForm->username->INNERTEXT = "abcdef";
     $browser->Document->passForm->password->INNERTEXT = "123456";
     com_message_pump(2000);
//隔兩秒才送出才看得到結果
$browser->Document->passForm->Submit->Click;
//在這裡判斷$iecontent;正確進入才存入cookie;
while (($browser->Busy) || ($browser->readystate != 4) ) {
    com_message_pump(2000);
}
$string = "錯誤";
$iecontent = $browser->Document->body->INNERTEXT;
$iecontent  = iconv("big5","UTF-8",$iecontent);
if (strpos($iecontent,"錯誤")) {
//還有很多判斷例如無此帳號....,每個網頁不一樣請自行增加
echo $iecontent;
}
else {
echo "write cookie ok";
$iecookie = $browser->Document->cookie;
echo $iecookie;
}
$browser->Quit();
?>

2014-01-27

PHP CURL不使用文件存取COOKIE

/*-----保存COOKIE-----*/
$url = 'domain'; //url地址
$post = "id=user&pwd=123456"; //POST数据
$ch = curl_init($url); //初始化
curl_setopt($ch,CURLOPT_HEADER,1); //将头文件的信息作为数据流输出
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); //返回获取的输出文本流
curl_setopt($ch,CURLOPT_POSTFIELDS,$post); //发送POST数据
$content = curl_exec($ch); //执行curl并赋值给$content
preg_match('/Set-Cookie:(.*);/iU',$content,$str); //正则匹配
$cookie = $str[1]; //获得COOKIE(SESSIONID)
curl_close($ch); //关闭curl

/*-----使用COOKIE-----*/
curl_setopt($ch,CURLOPT_COOKIE,$cookie);

2013-04-23

PHP 時區 timezone 設定


date_default_timezone_set("Asia/Taipei")


////////////////////////////
if (function_exists ( 'date_default_timezone_set' )){
date_default_timezone_set('Asia/Taipei'); //PHP5設定時區, 在PHP4無法使用
} else {
putenv("TZ=Asia/Taipei"); //PHP4設定時區的用法
}

2012-08-09

php中++i 与 i++ 的區別

1、++i的用法(以a=++i ,i=2為例)先將i值加1 (也就是i=i+1 ),然後賦給變量a ​​(也就是a=i ),則最終a值等於3 , i值等於3 。所以a=++i相當於i=i+1 ,a=i 
2、i++的用法(以a=i++ ,i=2為例)先將i值賦給變量a ​​(也就是a=i ),然後i值加1 (也就是i=i+1 ),則最終a值等於2 ,i值等於3 。所以a=i++相當於a=i , i=i+1 
3、++i與i++ a=++i相當於i++ , a=i a=i++相當於a=i , i++
 4、++i與i++單獨使用時,相當於i=i+1如果賦給一個新變量,則++i先將i值加1 ,而i++先將i賦給新變量。 

2011-05-04

php 將數字補零 使用str_pad

http://tw2.php.net/str_pad
string str_pad ( string $input , int $pad_length [, string $pad_string = " " [, int $pad_type = STR_PAD_RIGHT ]] )


<?php
$input 
"Alien";
echo 
str_pad($input10);                      // produces "Alien     "echo str_pad($input10"-="STR_PAD_LEFT);  // produces "-=-=-Alien"echo str_pad($input10"_"STR_PAD_BOTH);   // produces "__Alien___"echo str_pad($input"___");               // produces "Alien_"?>

2011-04-26

PHP讀取EXCEL 2007 2003

一個功能強大的Excel函式庫,一般的讀寫都可以處理,且支援圖片插入。
官方網站:http://www.codeplex.com/PHPExcel/


/** PHPExcel_IOFactory */
require_once '../Classes/PHPExcel/IOFactory.php';


if (!file_exists("05featuredemo.xlsx")) {
exit("Please run 05featuredemo.php first.\n");
}

echo date('H:i:s') . " Load from Excel2007 file\n";
$objPHPExcel = PHPExcel_IOFactory::load("05featuredemo.xlsx");

echo date('H:i:s') . " Write to Excel2007 format\n";
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));


// Echo memory peak usage
echo date('H:i:s') . " Peak memory usage: " . (memory_get_peak_usage(true) / 1024 / 1024) . " MB\r\n";

// Echo done
echo date('H:i:s') . " Done writing files.\r\n";

PHP 使用 Apache WWW-Authenticate 認證身份

<?php
$user = '認證帳號';
$pw = '認證密碼';

if(!isset($_SERVER['PHP_AUTH_USER']) || $_SERVER['PHP_AUTH_PW']=='') {
    Header("WWW-Authenticate: Basic realm=\"login\"");
    Header("HTTP/1.0 401 Unauthorized");
    die('認證失敗');
} else {
    if($_SERVER['PHP_AUTH_USER']!=$user || $_SERVER['PHP_AUTH_PW']!=$pw) {
        Header("WWW-Authenticate: Basic realm=\"login\"");
        Header("HTTP/1.0 401 Unauthorized");
        die('帳號或密碼錯誤');
    }
}

echo "認證成功";

2011-04-14

PHP 求質數

function IsPrime($i) 

if($i<2) 

return false; 

//var $iterator; 
for($iterator = 2 ; $iterator <= sqrt($i) ; $iterator++) 

if($i % $iterator==0) 

return false; 


return true; 

2011-03-31

利用curl取得http header訊息

$remoteFile = "http://www.google.com/";  
$ch = curl_init($remoteFile); 
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
$data = curl_exec($ch); 
curl_close($ch);

2011-01-20

php連接mssql2005

<?php 
$serverName = "127.0.0.1"; 
$connectionInfo = array("Database"=>"TestDB","UID"=>"test","PWD"=>"test"); 
$conn = sqlsrv_connect($serverName, $connectionInfo); 
if($conn) { 
echo "Connection established.<br>"; 
} else { 
echo "Connection could not be established.<br>"; 
die(print_r(sqlsrv_errors(), true)); 
exit(); 
} 
$sql = "select * from T_Employee"; 
$result = sqlsrv_query($conn,$sql); 
$data = array(); 
while($row=sqlsrv_fetch_array($result)) { 
$data[] = $row; 
} 
foreach($data as $p) { 
echo $p['Name']."<br>"; 
} 
sqlsrv_close($conn); 
echo "<br> Done <br>"; 
echo date("Y-m-d h:i:s"); 
?> 

php自動產生隨機密碼

该算法主要用到了两个函数,mt_rand ( int $min , int $max )函数用于生成随机整数,其中 $min – $max 为 ASCII 码的范围,这里取 33 -126 ,可以根据需要调整范围,如ASCII码表中 97 – 122 位对应 a – z 的英文字母,具体可参考 ASCII码表 chr ( int $ascii )函数用于将对应整数 $ascii 转换成对应的字符
function create_password($pw_length = 8) 

$randpwd = ''; 
for ($i = 0; $i < $pw_length; $i++) 

$randpwd .= chr(mt_rand(33, 126)); 

return $randpwd; 


// 调用该函数,传递长度参数$pw_length = 6 
echo create_password(6);