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

2025年9月2日 星期二

PHP FILETIME UNIX Time 轉換

UNIX時間採用世界標準時1970年1月1日00:00:00開始的秒數(不考慮閏秒)。
Windows API使用SYSTEMTIME表示年月日時分秒毫秒;
FILETIME表示自世界標準時1601年1月1日00:00:00開始的100奈秒為單位

 <?php
function ftime2utime($filetime_value){
  // 1. 轉換為秒 ( FILETIME單位是100奈秒 )
  $filetime_seconds = $filetime_value / 10000000;

  // 2. 減去UNIX紀元的偏移量 (1970年1月1日到1601年1月1日)
  $unix_timestamp = $filetime_seconds - 11644473600;

  // 3. 使用date()函數格式化時間戳
  $formatted_time = date('Y-m-d H:i:s', $unix_timestamp);

  echo "FILETIME : " . $filetime_value . "\n";
  echo "UNIX TIME: " . $unix_timestamp . "\n";
  echo "format   : " . $formatted_time . "\n";
}

function utime2ftime($unixTimestamp){
  // 计算自 1601 年以来的 100 纳秒间隔
  // 11644473600 是 1601 年 1 月 1 日 UTC 到 1970 年 1 月 1 日 UTC 的秒数 [1]
  // 10,000,000 将秒转换为 100 纳秒间隔 [1]
  $filetime = ($unixTimestamp + 11644473600) * 10000000;

  echo "Unix Timestamp: " . $unixTimestamp . "\n";
  echo "FILETIME (Hex): " . sprintf('%x', $filetime) . "\n";
  echo "FILETIME: ". $filetime . "\n";
}

$unixTimestamp = 1759420800;
utime2ftime($unixTimestamp);

echo "\n";
$filetime_value = 134038944000000000;
ftime2utime($filetime_value);
?>

2025年9月1日 星期一

PHP 查詢 Windows AD objectGUID objectSid (string fromat)

 <?php
function objectSid($binary_sid) {
    if(strlen(decbin(~0)) == 64)  //64bit PHP
    {
        // Get revision, indentifier, authority
        $parts = unpack('Crev/x/nidhigh/Nidlow', $binary_sid);
        // Set revision, indentifier, authority
        $sid = sprintf('S-%u-%d',  $parts['rev'], ($parts['idhigh']<<32) + $parts['idlow']);
        // Translate domain
        $parts = unpack('x8/V*', $binary_sid);
        // Append if parts exists
        if ($parts) $sid .= '-';
        // Join all
        return $sid.= join('-', $parts);
    }
     //32bit PHP
    $sid = 'S-';
    $sidinhex = str_split(bin2hex($binary_sid), 2);
    // Byte 0 = Revision Level
    $sid = $sid.hexdec($sidinhex[0]).'-';
    // Byte 1-7 = 48 Bit Authority
    $sid = $sid.hexdec($sidinhex[6].$sidinhex[5].$sidinhex[4].$sidinhex[3].$sidinhex[2].$sidinhex[1]);
    // Byte 8 count of sub authorities - Get number of sub-authorities
    $subauths = hexdec($sidinhex[7]);
    //Loop through Sub Authorities
    for($i = 0; $i < $subauths; $i++) {
        $start = 8 + (4 * $i);
        // X amount of 32Bit (4 Byte) Sub Authorities
        $sid = $sid.'-'.hexdec($sidinhex[$start+3].$sidinhex[$start+2].$sidinhex[$start+1].$sidinhex[$start]);
    }
    return $sid;
}

function objectGUID($binaryGuid) {
    $hexGuid = bin2hex($binaryGuid);
    // Reorder and format according to standard GUID representation
    $hex1 = substr($hexGuid, 6, 2) . substr($hexGuid, 4, 2) . substr($hexGuid, 2, 2) . substr($hexGuid, 0, 2);
    $hex2 = substr($hexGuid, 10, 2) . substr($hexGuid, 8, 2);
    $hex3 = substr($hexGuid, 14, 2) . substr($hexGuid, 12, 2);
    $hex4 = substr($hexGuid, 16, 4);
    $hex5 = substr($hexGuid, 20, 12);
    return sprintf('%s-%s-%s-%s-%s', $hex1, $hex2, $hex3, $hex4, $hex5);
}

$user = 'user1';         //設定欲認證的帳號名稱
$ldappass = 'p@ssw0rd';  //設定欲認證的帳號密碼
$domain = 'test.loc';   //設定網域名稱

putenv('LDAPTLS_REQCERT=allow');
$ldapconn = @ldap_connect("ldaps://" . $domain) or die("無法連接至 $domain");
ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);

if ($ldapconn) { // binding to ldap server
    $ldapbind = @ldap_bind($ldapconn, $user . '@' . $domain, $ldappass);
    if ($ldapbind) {  // verify binding
        $result = @ldap_search($ldapconn, sprintf("dc=%s", (str_replace(".", ",dc=", $domain))), "(sAMAccountName=$user)");

        if($result==false) echo "認證失敗";
        else {
          $entries = ldap_get_entries($ldapconn, $result);
          $results2 = ldap_search($ldapconn, sprintf("dc=%s", (str_replace(".", ",dc=", $domain))),"(&(objectclass=group)(objectsid=*))", array("cn", "objectguid"));
          $entries2 = ldap_get_entries($ldapconn, $results2);

          for ($i=1; $i<$entries2['count']; $i++)
            echo $entries2[$i]['cn'][0] . "\n" . objectGUID($entries2[$i]['objectguid'][0]) . "\n" . objectSid($entries2[$i]['objectsid'][0]) . "\n\n";
        }
    } else echo "認證失敗...";
}
?>

2025年8月21日 星期四

PHP 時區設定

編輯 /etc/php/8.2/cli/php.ini   /etc/php/8.2/apache2/php.ini 加入
date.timezone = 'Asia/Taipei'

程式中設定
date_default_timezone_set( 'Asia/Taipei')

2025年4月1日 星期二

PHP取得MIME類型

<?php
$f = "1.woff";

$fi = new finfo(FILEINFO_MIME_TYPE);
$mime_type = $fi->file($f);
echo $mime_type;

echo (new finfo(FILEINFO_MIME_TYPE))->file($f);
?>

2025年1月17日 星期五

PHP proc_open 摘要

 <?php
  $cmd = "sqlite3 1.db ";
  $interactive_cmd = "insert into a (a1,a2) values('中文','許');";
  $descriptorspec = array(
    0 => array("pipe", "r"),  // stdin
    1 => array("pipe", "w"),  // stdout
    2 => array("pipe", "w")   // stderr ?? instead of a file
  );

  $stdout = "";
  $stderr = "";

  $process = proc_open($cmd, $descriptorspec, $pipes, NULL);

  if (is_resource($process)) {
    fwrite($pipes[0], $interactive_cmd . PHP_EOL);
    fclose($pipes[0]);

    while($s= fgets($pipes[1], 1024)) $stdout .= $s;  // read from the pipe
    fclose($pipes[1]);

    // stderr optional:
    while($s= fgets($pipes[2], 1024)) $stderr .= $s;
    fclose($pipes[2]);
  }

  echo "stdout: $stdout \nstderr: $stderr\n";
?>

2024年12月10日 星期二

PHP $_SERVER['HTTP_REFERER'] 摘要

說明:
$_SERVER[‘HTTP_REFERER’] 來源於瀏覽器,不是所有都會設置且部份支援手動修改。

以下狀況會無效:
直接輸入網址訪問該網頁。
Javascript 打開的網址、重定向(window.location)網址及 FLASH 中的鏈接。
PHP header 或 meta refresh重定向的網址。
瀏覽器未加設置或修改

一般生效狀況:
<a></a>超鏈結
POST
GET

2023年12月8日 星期五

PHP 執行外部指令

<?php
echo "<pre>";

ob_implicit_flush(true);
ob_end_flush();

$cmd = "./1.sh";

$descriptorspec = array(
   0 => array("pipe", "r"),   // stdin is a pipe that the child will read from
   1 => array("pipe", "w"),   // stdout is a pipe that the child will write to
   2 => array("pipe", "w")    // stderr is a pipe that the child will write to
);

$process = proc_open($cmd, $descriptorspec, $pipes, realpath('./'), array());
if (is_resource($process)) while ($s = fgets($pipes[1])) print $s;
?>

2019年8月16日 星期五

2018年12月7日 星期五

Browser 來源 IP 相關訊息

HTTP Header
HTTP_CLIENT_IP
HTTP_X_FORWARDED_FOR
HTTP_X_FORWARDED
HTTP_X_CLUSTER_CLIENT_IP
HTTP_FORWARDED_FOR
HTTP_FORWARDED
REMOTE_ADDR
HTTP_VIA

Proxy
REMOTE_ADDR: 最後一個代理伺服器 IP
HTTP_VIA: 代理伺服器 IP
HTTP_X_FORWARDED_FOR

2018年10月4日 星期四

Debian 9 + PHP7 + ssh2 安裝設定

apt-get install libssh2-1-dev libssh2-1

wget https://github.com/Sean-Der/pecl-networking-ssh2/archive/php7.zip
unzip pecl-networking-ssh2-php7.zip
cd pecl-networking-ssh2-php7/
phpize
./configure
make
make install

echo extension=ssh2.so >>/etc/php/7.0/apache2/php.ini
echo extension=ssh2.so >>/etc/php/7.0/cli/php.ini

2018年9月28日 星期五

PHP Sqlite3 筆記

try {
//建立連線
$dbh = new PDO('sqlite:./test.db3');

//建表格
$dbh->exec("CREATE TABLE a(id integer,name varchar(255))");

//增加資料
$dbh->exec("INSERT INTO a values(1,'test')");
$dbh->beginTransaction();

//取得資料
$sth = $dbh->prepare('SELECT * FROM a');
$sth->execute();

//獲取結果
$result = $sth->fetchAll();
print_r($result);
}
catch (PDOException $e)
{  echo 'Connection failed: ' . $e->getMessage(); }
if(isset($dsn))  $dsn=null;

2018年9月17日 星期一

Debian9 設定PHP7 beast

busybox=/usr/local/bin/busybox
wget -q https://busybox.net/downloads/binaries/1.28.1-defconfig-multiarch/busybox-x86_64 -O  ${busybox}&&chmod a+x ${busybox}||exit 1
apt-get -y install gcc make php php-dev

wget https://github.com/liexusong/php-beast/archive/master.zip
busybox unzip master.zip
cd php-beast-master
phpize
./configure
make
make install

echo extension=beast.so >>/etc/php/7.0/apache2/php.ini
echo extension=beast.so >>/etc/php/7.0/cli/php.ini

2018年5月21日 星期一

PHP 更改Windows AD 帳號密碼摘要

1.The password is stored in the AD and LDS database on a user object in the unicodePwd attribute

2.the client must have a 128-bit Transport Layer Security (TLS)/Secure Socket Layer (SSL) connection to the server.

3.設定 AD CS,確認 LDAPS:636 正常運作

4.用於 PHP 密碼編碼及更新程式碼
$userdata["unicodePwd"] = iconv("UTF-8", "UTF-16LE", '"' . $new_pass . '"');
ldap_mod_replace($ldapconn, 'CN=user,CN=Users,DC=domain,DC=com', $userdata);

5.參考資料
https://support.microsoft.com/en-us/help/269190/how-to-change-a-windows-active-directory-and-lds-user-password-through

6.認證程式碼
putenv('LDAPTLS_REQCERT=allow');

$ldapurl = "ldaps://domain.local";
$ldapurl = "ldaps://192.168.0.1";

$ldapconn = ldap_connect($ldapurl);

ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);

$ldapbind = ldap_bind($ldapconn, "php@domain.local", "p@ssw0rd");

2018年5月20日 星期日

安裝 php-beast

wget https://github.com/liexusong/php-beast/archive/master.zip
unzip master.zip
cd php-beast-master
phpize
./configure
make
make install

編輯 /etc/php.ini 加入
extension=beast.so

CentOS7 安裝 PHP 5.6

yum install epel-release
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
yum install yum-utils
yum-config-manager --enable remi

yum-config-manager --enable remi-php56
yum install php

2018年5月19日 星期六

PHP Warning: ldap_bind(): Unable to bind to server: Can't contact LDAP server in

編輯 /etc/openldap/ldap.conf 加入 TLS_REQCERT never

PHP 程式加入環境變數
putenv('LDAPTLS_REQCERT=allow');

2018年1月30日 星期二

PHP 使用 fgetcsv 無法正確剖析 UTF-8 中文

使用  fgetcsv 函式前,先執行
setlocale(LC_ALL, 'en_US.UTF-8')

2017年5月22日 星期一

增加 PHP 上傳檔案大小

 修改 /etc/php.ini

; Maximum allowed size for uploaded files.
; http://www.php.net/manual/en/ini.core.php#ini.upload-max-filesize
;upload_max_filesize = 2M
upload_max_filesize = 16M

; Maximum size of POST data that PHP will accept.
; http://www.php.net/manual/en/ini.core.php#ini.post-max-size
;post_max_size = 8M
post_max_size = 16M

2016年10月11日 星期二

列出所有網路遮罩長度 IP

$ip = 0; for ($i=1; $i<33; $i++) printf("%d %s %s\n\n", $i, $ip+= pow(2, 32-$i), long2ip($ip));

2016年3月10日 星期四

php md5() 與 Linux md5sum

md5sum
echo -n 1|md5sum
echo 1|md5sum

PHP
echo md5('1')
echo md5("1\n")