session_start()导致history.go(-1)返回时无法保存表单数据的解决方法

问题背景:

在填写完表单提交时,由于某个表单项可能填写的不合法,导致提交失败,返回表单页面。但返回后所有的表单都被清空了,重新填写比较麻烦,度娘解释说,是由于每个页面都调用了session_start()的原因,在js返回上一页时,不能保存住表单信息。

解决方法:

在公共初始化文件的session_start()之后加入一句:
header(‘cache-control:private,must_revalidate’)

或:

session_cache_limiter(‘private’)

Read More

修复PHP支持的标准JSON数据格式

PHP的json_decode无法解析的JSON数据,代码如下:

1
2
$json = "{rst:5,c:[ [1018485,2,0,0,0,0,'','0-0','','',2,0,2],[1049809,17,0,0,0,0,'','','','',1,0,1],[1049813,17,0,0,0,0,'','','','',1,0,1],[1049810,17,0,0,0,0,'','','','',1,0,1]],fn:135388}";
echo json_decode($json);

//结果输出: null

Read More

assert函数的用法

assert这个函数在php语言中是用来判断一个表达式是否成立。返回true or false;
例如:

1
2
3
4
<?php  
$s = 123;
assert("is_int($s)");
?>

从这个例子可以看到字符串参数会被执行,这跟eval()类似。不过eval($code_str)只是执行符合php编码规范的$code_str。
assert的用法却更详细一点。

Read More

把一维数组合并成二维数组

描述:$name数组是3个人的名字,$age数组的元素分别对应3个人的年龄,希望合并后的数组$user,它的每一个元素都是一条独立的个人信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php  
$name = array('andy','beer','candy');
$age = array(22,13,32);

$user = array();
foreach ($name as $k => $v) {
$temp = array('name'=>$v,'age'=>$age[$k]);
$user[] = $temp;
//$user['name'][] = $v;
//$user['age'][] = $age[$k];
}

var_dump($user);
/*
array (size=3)
0 =>
array (size=2)
'name' => string 'andy' (length=4)
'age' => int 22
1 =>
array (size=2)
'name' => string 'beer' (length=4)
'age' => int 13
2 =>
array (size=2)
'name' => string 'candy' (length=5)
'age' => int 32
*/
?>

Read More

表格数据上下行互换位置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>表格数据上下行互换位置</title>
<script src="js/jquery-1.7.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
//上移
$("input.upbtn").each(function () {
$(this).click(function () {
var $tr = $(this).parents("tr");
if ($tr.index() != 0) {
$tr.prev().before($tr);
}
});
});
//下移
var trLength = $("input.downbtn").length;
$("input.downbtn").each(function () {
$(this).click(function () {
var $tr = $(this).parents("tr");
if ($tr.index() != trLength - 1) {
$tr.next().after($tr);
}
});
});
});

</script>
</head>
<body>
<table border="1" cellpadding=0 cellspacing=0>
<tr>
<td>6</td>
<td>xxxx66xxx</td>
<td>2013-5-26</td>
<td><input type="button" value="上移" class="upbtn" /><input type="button" value="下移" class="downbtn" /></td>
</tr>
<tr>
<td>7</td>
<td>xxxx77xxx</td>
<td>2013-5-27</td>
<td><input type="button" value="上移" class="upbtn" /><input type="button" value="下移" class="downbtn" /></td>
</tr>
<tr>
<td>8</td>
<td>xxx88xxxx</td>
<td>2013-5-28</td>
<td><input type="button" value="上移" class="upbtn" /><input type="button" value="下移" class="downbtn" /></td>
</tr>
</table>
</body>
</html>

Read More