Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexchent committed Dec 12, 2019
1 parent 748fb75 commit 962febf
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 66 deletions.
27 changes: 27 additions & 0 deletions Helper/Tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,5 +316,32 @@ public static function getServerIp()
return $_SERVER['SERVER_ADDR'];
}

/**
* 求a相对于b的路径
*
* @param $a
* @param $b
* @return string
*/
public static function getRelativePath($a, $b)
{
$path = '';
$arr = explode('/', $a);
$brr = explode('/', $b);
// var_dump($brr);
$same = array_intersect_assoc($arr, $brr);//获取两个数组相同的部分
//var_dump($same);
$dir = array_diff_assoc($brr, $same);
// var_dump($dir);die;
for($i = 1; $i <= count($dir)-1; $i++) {
$path .='../';
}

$path .= str_replace(implode('/',$same).'/','', $a);

return $path;
}



}
23 changes: 22 additions & 1 deletion array_merge_sort.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,25 @@ function array_merge_sort2($a, $b)
return $result;
}

print_r(array_merge_sort2($a, $b));

function sort_mix_array($a, $b)
{
if(count($a) == 0) return $b;

$result = [];
$i=0;
//以a数组中的元素为基数,将b中比基数小的先存入结果数组,再将基数存入;再取出a中的下一个数对b剩下的数组重复以上操作,直到所有元素存入结果数组
foreach ($a as $k => $item) {

while (isset($b[$i]) && $item > $b[$i]) {
$result[] = $b[$i];
$i++;
}

$result[] = $item;
}
return $result;
}


print_r(sort_mix_array($a, $b));
File renamed without changes.
61 changes: 61 additions & 0 deletions codehero3/kill_redis.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* 已知有10万个cdkey号码。给出一个发放cdkey的方案
* 要求:1.用户在登录后,领取,每人限领一张
* 2.高并发的情况下,不允许一个cdkey发放给同一个人
*
*
*
* 高并发下存在误差待改进
*
*
* 连接redis的命令 redis-cli -h 127.0.0.1 -p 6379
*
* 压力测试:ab -c 100 -n 20000 http://localhost:8888/interview/codehero3/kill_redis.php
*/

$eventId = isset($_GET['eventId']) ? intval($_GET['eventId']) : 1;
$userId = isset($_GET['userId']) ? intval($_GET['userId']) : rand(1, 100000000);

kill($userId);

function kill($userId)
{

$cdkey = 'cd_keys';
$userkey = "join_user";


$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

$cnt = $redis->sCard($cdkey);
if (!$cnt) {
echo '已经发放完毕!余量'.$cnt;
die;
}

if (!$redis->sAdd($userkey, $userId)) {
echo "只能领取一次";die;
}

//仓库中随机取出一个cdkey
if($cdkey = $redis->sPop($cdkey)) {
$db = new mysqli();
// $db->connect('127.0.0.1', 'root', 'root', 'codehero', 8889);
$db->connect('127.0.0.1', 'root', '12345678', 'codehero', 3306);
$db->set_charset('utf8');

$time = time();
$db->query("BEGIN");
$db->query("UPDATE `cdkeys` SET `status` = 1 WHERE `id` = {$cdkey} ADN `status`=0");
$db->query("INSERT INTO `log` (`cdkey`, `userid`, `createTime`) VALUES ({$cdkey}, {$userId}, {$time})");
$db->query("COMMIT");

echo "success";die;
} else {
echo "已经发放完毕!";die;
}


}
1 change: 1 addition & 0 deletions codehero3/makeData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php/** * Created by PhpStorm. * User: chentao * Date: 2019/12/6 * Time: 6:05 PM */error_reporting(E_ALL);ini_set('display_errors', '1');$db = new mysqli();$db->connect('127.0.0.1', 'root', '12345678', 'codehero', 3306);$db->set_charset('utf8');$redis = new Redis();$redis->connect('127.0.0.1', 6379);$time = time();for($i=1;$i<=10000;$i++) { $redis->sAdd('cd_keys', $i);// $cdkey = rand(0,10000000);// $db->query("INSERT INTO `cdkeys` (`cdkey`) VALUES ($cdkey)");}echo 'success';
Expand Down
64 changes: 3 additions & 61 deletions deep_in_array.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* Time: 12:55 PM
*/

error_reporting(E_ALL);

/**
* 给定一个二维数组,数组每行从左到右都是递增的;没列也是递增的
* 给定一个二维数组,数组每行从左到右都是递增的;每列也是递增的
* 请完成一个函数,输入如上二维数组和一个整数,函数功能为判断该整数是都存在于数组中。
* 时间复杂度尽可能低。(请说明时间复杂度)
*/
Expand All @@ -26,7 +26,7 @@
var_dump( find($arr, $findParam) );

/**
* 冒泡——时间复杂付O(n^2)
* 冒泡——时间复杂付O(n²)
* @param $arr
* @param $findParam
* @return bool
Expand All @@ -47,62 +47,4 @@ function find($arr, $findParam) {
}


/**
* 循环方式——冒泡
* @param array $arr 待查二维数组
* @param int $findParam
* @return bool
* 时间复杂度O(n²)
*/
function deep_in_array($arr, $findParam)
{
//下面是一个例子:
//二维数组:
//$arr = array([1,2,8,9],[2,4,9,12], [4,7,10,13], [6,8,11,15]);
//数字:9
foreach ($arr as $item) {
foreach ($item as $value) {
if ($value == $findParam) return true;
if ($value > $findParam) continue;
}
}
return false;
}


/**
* 利用in_array
* @param $arr
* @param $findParam
* @return bool
* 时间复杂度O(n²)
*/
function deep_in_array2($arr, $findParam)
{
foreach ($arr as $item) {
//in_array严格模式性能更高,因为松模式会将字符型数字串转换为长整型
//使用时注意数据类型
if (in_array($findParam, $item, true)) return true;
}
return false;
}

/**
* 递归实现,优势,可以进行更高阶数组的判断
*
* 时间复杂度?
*/
function deep_in_array3($arr, $findParam){
if (is_array($arr)) {
foreach ($arr as $value) {
if (is_array($value) && deep_in_array3($value, $findParam)) return true;
if ($value == $findParam) return true;
if ($value > $findParam) continue;
}
}
if ($arr == $findParam) return true;
return false;
}



6 changes: 3 additions & 3 deletions output.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//var_dump(is_null([]));
//var_dump(false == []);
//var_dump([] == null);

//die;
//utf-8中文占3个字符
//echo mb_substr($str = '欢迎来我中国,you are welocme!', 0, 7)."\n";
//echo substr($str = '欢迎来我中国,you are welocme!', 0, 7);
Expand All @@ -26,8 +26,8 @@

print $test.$array."\n";

print_r($array);
//print_r($array);

var_dump($array);
//var_dump($array);

?>
2 changes: 1 addition & 1 deletion test.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php/** * Created by PhpStorm. * User: chentao * Date: 2019/12/5 * Time: 11:02 AM */include "Helper/Tools.php";//echo \Helper\Tools::getServerIp();die;//echo $ip = $_SERVER['REMOTE_ADDR'];die;//echo $ip = getenv('HTTP_X_FORWARDED_FOR');die;$a = [1,2,3,4,5,6];//echo current($a);//echo next($a);//echo prev($a);//echo end($a);class A { private $x = 1;}// PHP 7 之前版本定义闭包函数代码$getXCB = function() { return $this->x;};// 闭包函数绑定到类 A 上//$getX = $getXCB->bindTo(new A, 'A');//echo $getX();//print(PHP_EOL);echo $getXCB->call(new A, 'A');
<?php/** * Created by PhpStorm. * User: chentao * Date: 2019/12/5 * Time: 11:02 AM */declare(strict_types=0);include "Helper/Tools.php";static $c =1;$d = 3;$res1 = test(1.5,2);var_dump($res1);$res2 = test(1.5,2);var_dump($res2);function test(int $a, int $b) :int { static $d = 4; $res = $a + $b; if(isset($c)) { $res += $c; } if(isset($d)) { $res += $d; $d += 1.5; } return $res;}die;class A { private $x = 1;}// PHP 7 之前版本定义闭包函数代码$getXCB = function() { return $this->x;};// 闭包函数绑定到类 A 上//$getX = $getXCB->bindTo(new A, 'A');//echo $getX();//print(PHP_EOL);echo $getXCB->call(new A, 'A');
Expand Down
1 change: 1 addition & 0 deletions xss.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!DOCTYPE html><html lang="zh"><head> <meta charset="UTF-8"> <title>xss漏洞广告页面</title> <style> a { text-decoration: none; font-size: 2rem; } img { width: 8rem; height: 8rem; } </style></head><body><a href="attack.html?content=<img src='aaa.png' onerror='alert(1)'/>"> <img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1520930605289&di=04f8835509d8c3c3fac4db7636247431&imgtype=0&src=http%3A%2F%2Fpic.58pic.com%2F58pic%2F13%2F14%2F16%2F37J58PICWTD_1024.jpg"></a><a href="attack.html?content=<img src='aaa.png' onerror='while(true)alert(/关不掉/)'/>">敏感词汇</a><script></script></body></html>
Expand Down

0 comments on commit 962febf

Please sign in to comment.