Table of Contents:

php ssh2

下载 php extension ssh2 并进行配置

$connection = ssh2_connect("hangge.oicp.net",8022);
if(ssh2_auth_password($connection,"root","yuhang")){
    echo "Successfull";
}else{
    die("Failed");
}
 
$tcmd = '/usr/bin/etherwake -D "1c:6f:65:20:70:d4"';
$stream = ssh2_exec($connection,$tcmd);
stream_set_blocking($stream,true);
echo stream_get_contents($stream);
$connection = ssh2_connect('10.1.10.81', 22);
if (!$connection) {
        exit();
}
if (!ssh2_auth_password($connection, 'root', '123456')) {
        exit();
}


$cmd = "mysqldump worktime > /data/worktime.sql";
$stream = ssh2_exec($connection, $cmd);
stream_set_blocking($stream, true);
 
stream_get_contents($stream);
 
ssh2_scp_recv($connection, "/data/worktime.sql", "/home/worktime.sql");

文件锁

$fp = fopen('a.lock','r');
$try = 10;
do{
    $lock = flock($fp, LOCK_EX);
    if(!$lock)
        usleep(50000);
}while(!$lock && --try >0);


if($lock){
    //执行要做的逻辑
    //执行完后
    flock($pf,LOCK_UN);
    fclose($fp);
}else{
    fclose($fp);
    die('系统繁忙,稍后再试');
}
<?php
class Locker
{
 private static $_lockFp = array();
 public static function startLock($lockFileName)
 {
  self::$_lockFp[$lockFileName] = $fp = fopen(ROOT . '/data/lock/'.$lockFileName, 'r');
  if(!$fp)
   return FALSE;
  $try = 10;
  $lock = false;
  do 
  {
   $lock = flock($fp, LOCK_EX);
   if(!$lock)
    usleep(50000); // 0.05秒
  }while(!$lock && --$try > 0);
  return $lock;
 }
 public static function endLock($lockFileName)
 {
  if(isset(self::$_lockFp[$lockFileName]))
  {
   @flock(self::$_lockFp[$lockFileName], LOCK_UN);
   @fclose(self::$_lockFp[$lockFileName]);
  }
 }
}

crul运用

function GetCurl($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    $resp = curl_exec($curl);
    curl_close($curl);
    return $resp;
}

echo(GetCurl('http://e.wesambo.com/index.php?c=index&a=getip'));

生成数字签名

<?php
//error_reporting(0);
 
$m = $_GET['m'];
$t = $_GET['t'];
 
if (md5('yy_zz_fancy3d') != $m) {
    echo '';
    //exit();
}
 


if (strlen($t) < 8) {
    $t = sprintf('%08s',$t);
}
 
$prikey = <<<EOD
-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQCW9ZXCaUo7MPKF8mnJahuocseVaHlFIJmvWbjtT58gisfx3y5Z
d3q+A8BGcuy5KcWY+zQ6AKjcEGTasHzo7gshUMQDyaFnruJTDC7AjnH91pMX2H/g
jH+1Y2TZPZXyRHZBoHUzR2Z8ubLXU3T5zjZCnm+vKFatPI+L/+6TzqLXIQIDAQAB
AoGAesPz66vWy3ZN9PKkZ0Fchsv5VZ8l6DrP4ROVWaDD5yIV4rS+q9GHx1mjl2dE
7nxzDqtT2MvKh1TpwBOBMajM8SZD4Hx4nTzvrUod/6qdppTC2uTIXBYxJqGemaUt
yJ1OBetwpD6MKR1d1JzIdm4PgV3mfZc9Sub5Phhiwgwl4AECQQDIALoXFue69sbT
6K8X0jK9EM92AwcIc7nLAIsDF+cvsfJzb3YVT1/1cwkWGd36dd10AeFPWmG1hnUw
7Pxmp5AhAkEAwTmlgWhpklx7LwCDWJQFA/lH2nGCgHtMqn/BSEaTeq36UHGgedqh
lYrgReQVJK6j5jpamCk/iE2JD6AWdT5nAQJBAKfiFFL1YydJprSSiQdR5k1WIw9I
k0rA+aAxoH7EPceHc6D6WwCgVEhQe7wVkoN/FJkgSuS1VXdumkirZWM2HIECQFs9
PzuF9CPaa4hcM6lF68Bem2E4rCjDEAaKYNgiC02ojUQjA2XGqPoWI6Sc+KbjmB0E
fPbQs2FHS2sHatNd7gECQFPWDR10stjyRJjuG8loOxtNcZq1m3KnsdnnPDQiAc6K
R20c0rz6Nr5KFwjzFo/SmW2BYtbEtIXyMah6KcVA2vA=
-----END RSA PRIVATE KEY-----
EOD;
 
$priv_key_id = openssl_get_privatekey($prikey);
 
$sign;
 
$ret = openssl_sign($t,$sign,$prikey,OPENSSL_ALGO_SHA1);
 
echo base64_encode($sign);

验证数字签名

<?php
 
$data = "00012345";
 
$private_key = <<<EOD
-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQCW9ZXCaUo7MPKF8mnJahuocseVaHlFIJmvWbjtT58gisfx3y5Z
d3q+A8BGcuy5KcWY+zQ6AKjcEGTasHzo7gshUMQDyaFnruJTDC7AjnH91pMX2H/g
jH+1Y2TZPZXyRHZBoHUzR2Z8ubLXU3T5zjZCnm+vKFatPI+L/+6TzqLXIQIDAQAB
AoGAesPz66vWy3ZN9PKkZ0Fchsv5VZ8l6DrP4ROVWaDD5yIV4rS+q9GHx1mjl2dE
7nxzDqtT2MvKh1TpwBOBMajM8SZD4Hx4nTzvrUod/6qdppTC2uTIXBYxJqGemaUt
yJ1OBetwpD6MKR1d1JzIdm4PgV3mfZc9Sub5Phhiwgwl4AECQQDIALoXFue69sbT
6K8X0jK9EM92AwcIc7nLAIsDF+cvsfJzb3YVT1/1cwkWGd36dd10AeFPWmG1hnUw
7Pxmp5AhAkEAwTmlgWhpklx7LwCDWJQFA/lH2nGCgHtMqn/BSEaTeq36UHGgedqh
lYrgReQVJK6j5jpamCk/iE2JD6AWdT5nAQJBAKfiFFL1YydJprSSiQdR5k1WIw9I
k0rA+aAxoH7EPceHc6D6WwCgVEhQe7wVkoN/FJkgSuS1VXdumkirZWM2HIECQFs9
PzuF9CPaa4hcM6lF68Bem2E4rCjDEAaKYNgiC02ojUQjA2XGqPoWI6Sc+KbjmB0E
fPbQs2FHS2sHatNd7gECQFPWDR10stjyRJjuG8loOxtNcZq1m3KnsdnnPDQiAc6K
R20c0rz6Nr5KFwjzFo/SmW2BYtbEtIXyMah6KcVA2vA=
-----END RSA PRIVATE KEY-----
EOD;
$public_key = <<<EOD
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCW9ZXCaUo7MPKF8mnJahuocseV
aHlFIJmvWbjtT58gisfx3y5Zd3q+A8BGcuy5KcWY+zQ6AKjcEGTasHzo7gshUMQD
yaFnruJTDC7AjnH91pMX2H/gjH+1Y2TZPZXyRHZBoHUzR2Z8ubLXU3T5zjZCnm+v
KFatPI+L/+6TzqLXIQIDAQAB
-----END PUBLIC KEY-----
EOD;
 
$binary_signature = "";
 
 
openssl_sign($data, $binary_signature, $private_key, OPENSSL_ALGO_SHA1);
 
// Check signature
$ok = openssl_verify($data, $binary_signature, $public_key, OPENSSL_ALGO_SHA1);
echo "check #1: ";
if ($ok == 1) {
    echo "signature ok (as it should be)\n";
} elseif ($ok == 0) {
    echo "bad (there's something wrong)\n";
} else {
    echo "ugly, error checking signature\n";
}

php通过header函数下载文件

般来说, 我们可以通过直接让URL指向一个位于Document Root下面的文件, 来引导用户下载文件.
但是, 这样做, 就没办法做一些统计, 权限检查, 等等的工作. 于是, 很多时候, 我们采用让PHP来做转发, 为用户提供文件下载.

<?php
    $file = "/tmp/dummy.tar.gz";
    header("Content-type: application/octet-stream");
    header('Content-Disposition: attachment; filename="' . basename($file) . '"');
    header("Content-Length: ". filesize($file));
    readfile($file);

查看邮件是否已被阅读

高明之处在于在图片中隐藏东西

<?php
error_reporting(0);
//Get IP
if (!empty($_SERVER['HTTP_CLIENT_IP']))
{
  $ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
  $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
  $ip=$_SERVER['REMOTE_ADDR'];
}


//Time
$actual_time = time();
$actual_day = date('Y.m.d', $actual_time);
$actual_day_chart = date('d/m/y', $actual_time);
$actual_hour = date('H:i:s', $actual_time);


//GET Browser
$browser = $_SERVER['HTTP_USER_AGENT'];


//LOG
$myFile = "log.txt";
$fh = fopen($myFile, 'a+');
$stringData = $actual_day . ' ' . $actual_hour . ' ' . $ip . ' ' . $browser . ' ' . "\r\n";
fwrite($fh, $stringData);
fclose($fh);


// Set the content type header - in this case image/jpeg
header('Content-Type: image/jpeg');
$newimage = ImageCreate(1,1);
$grigio = ImageColorAllocate($newimage,255,0,0);
ImageJPEG($newimage);
ImageDestroy($newimage);

查找页面上的所有链接

高明之处在于用dom来找

<?php
$html = file_get_contents('http://www.zhihu.com');


$dom = new DOMDocument();
@$dom->loadHTML($html);


// grab all the on the page
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");


for ($i = 0; $i < $hrefs->length; $i++) {
       $href = $hrefs->item($i);
       $url = $href->getAttribute('href');
       echo $url.'<br />';
}

从服务器上下载&保存一个远程图片

$image = file_get_contents('http://www.url.com/image.jpg');
file_put_contents('/images/image.jpg', $image); //Where to save the image