php 递归遍历数组

复习PHP中, 简单写下, 练练手。

<?php

/*
*  -------------------------------------------------
*   Author : lellansin
*   Url    : www.lellansin.net
*   Date   : 2014-08-13
*  -------------------------------------------------
*/
function deep_foreach ($arr, $k='', $pre_indent = '') {
	if (!is_array ($arr)) {
		return false;
	}

	$str = $k ? "[$k] => " : '';
	$cur_indent = $pre_indent . "    ";

	echo $pre_indent.$str."Array<br/>$pre_indent(<br/>";

	foreach ($arr as $key => $val ) {
		if (is_array ($val)) {
			deep_foreach ($val, $key, $cur_indent);
		} else {
			echo $cur_indent."[$key] = > ".$val.'<br/>';
		}
	}

	echo $pre_indent.")<br/>";
}

$arr1 = array(array(2,2,8,4=>array(array(5,6,7,8),1)),5,array(5,6,8),3,4);

echo '<pre>';
deep_foreach ($arr1);
echo '</pre>';

输出:

Array
(
    Array
    (
        [0] = > 2
        [1] = > 2
        [2] = > 8
        [4] => Array
        (
            Array
            (
                [0] = > 5
                [1] = > 6
                [2] = > 7
                [3] = > 8
            )
            [1] = > 1
        )
    )
    [1] = > 5
    [2] => Array
    (
        [0] = > 5
        [1] = > 6
        [2] = > 8
    )
    [3] = > 3
    [4] = > 4
)

局域网想要DNS指向自己服务器的解决方案(主机名称的作用)

原本想使用路由添加 DNS 记录,来指向局域网测试的服务器。结果意外的发现了一种非常简单的办法。那就是通过修改当前主机的名称来实现局域网内的一个“模拟域名”。

博主这里用的是 win7 的操作系统,刷系统的时候用户名写的是 Lellansin,于是我的电脑主机名也就默认的变成了: Lellansin-PC

于是情况就来了,我们打开 cmd,输入:

ping Lellansin-PC

你会发现,这其实是 ping 的通的。这也意味着,在局域网测试的时候,原本通过 192.168.1.XX/test.php (当前计算机在局域网中的ip + 网站路径) 的访问方式可以通过使用 lellansin-pc/test.php 来代替。如果你把当前计算机的名称改为 pms 的话也可以通过 pms/test.php 来访问。

不过貌似 windows 上改名字的时候不能加点(有知道的望留言提醒),不然改成 http://www.test.com 也是极好的。

在 linux 上的话就会自由一些,通过:

sudo vim /etc/hostname

我就直接把局域网里测试用的服务器的主机名改成了 http://www.testgame.com ,尝试确实是可以用的。

php 报错 Use of undefined constant CURLOPT_*

碰到诸如

Notice: Use of undefined constant CURLOPT_PROTOCOLS - assumed 'CURLOPT_PROTOCOLS' in /home/john/public_html/test/test.php on line 9

Notice: Use of undefined constant CURLPROTO_SFTP - assumed 'CURLPROTO_SFTP' in /home/john/public_html/test/test.php on line 9

这类 Use of undefined constant CURLOPT_* 错误的时候。

打开 php.ini 中 php_curl 的拓展,然后重启服务器,即可。

php 网站搬家

#
# 原服务器
#
tar -cvf /home/wwwroot/lellansin.com    # 网站打包
mysqldump -u 用户名 -p 数据库 > 数据库.sql # mysql 数据备份
mv 文件 /home/wwwroot/lellansin.com/secret/

#
# 新服务器
#
wget http://lellansin.com/secret/文件  # 直接通过 http 在服务端下载
mysql -u root -p                       # 输入密码后进入 mysql 客户端
CREATE DATABASE `mydb` CHARACTER SET utf8 COLLATE utf8_general_ci; # 创建一个名为 mydb 的数据库
exit                                   # 退出 mysql
mysql -u 用户名 -p 数据库 < 数据库.sql # mysql 数据导入
tar -zxvf lellansin.com.tar.gz         # 解压网站之后移动到相应目录

# 如果担心时间比较久可以使用 nohup 命令, 例如
nohup tar -zxvf lellansin.com.tar.gz &

/home/wwwroot/lellansin.com/secret/ 中的 secret 是你自己定义的一个目录名称,可以把打包好的文件放在此处,让新服务器可以通过 wget 命令来直接下载,这样的好处是不用费时费力去搞 ftp。也不用蛋疼的先把备份下载到本地然后再上传到新服务器。(博主的两台服务器都在美国,试了一下,发现美国的下载速度果然非常快,峰值可以到 4M/s 平均快到 2M/s 了)

一般的 ssh 都有超时时间,如果担心命令执行时间过长导致 session 超时然后命令不得不被迫中断的话,可以使用 nohup 命令,该命令可以使得执行的命令脱离终端,即使关掉 ssh 客户端,服务器上依旧能继续执行。

要测试网站是否成功搬好,并且’无痛’搬家的话可以考虑使用本地解析, 通过修改 C:WindowsSystem32driversetchosts 文件来实现,添加:

// 形如
xxx.xx.xx.xxx    你的域名
// 例如
192.157.208.25   lellansin.com

在 hosts 文件中添加记录之后,那么本地就会解析域名到新服务器的地址。这个时候再用浏览器使用该域名访问的就是新服务器上的网站了。你可以看看新服务器上的网站是否有问题,没问题的话,就可以上域名服务商的网站修改域名解析了。