由于某种原因把php的allow_url_fopen选项是关闭了,就是没法直接使用file_get_contents来获取远程web页面的内容。那就是可以使用另外一个函数curl。
无限file_get_contents获取远程网页内容函数
代码如下 |
复制代码 |
function vita_get_url_content($url) {
if(function_exists('file_get_contents')) {
$file_contents = file_get_contents($url);
} else {
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, curlopt_url, $url);
curl_setopt ($ch, curlopt_returntransfer, 1);
curl_setopt ($ch, curlopt_connecttimeout, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
}
return $file_contents;
} |
/*
由于某种原因把php教程的allow_url_fopen选项是关闭了,就是没法直接使用file_get_contents来获取远程web页面的内容。那就是可以使用另外一个函数curl。
本教程主要利用jquery 的ajax来实现无刷新jquery php验证码代码的提前验证操作。
<?php
//调用此页面,如果下面的式子成立,则生成验证码图片
if($_get['action']=='verifycode'){
rand_create();
}
//验证码图片生成
代码如下 |
复制代码 |
function rand_create(){
//通知浏览器将要输出png图片
header('content-type: image/png');
//准备好随机数发生器种子
srand((double)microtime()*1000000);
//准备图片的相关参数
$im = imagecreate(62,20);
$black = imagecolorallocate($im, 0,0,0); //rgb黑色标识符
$white = imagecolorallocate($im, 255,255,255); //rgb白色标识符
$gray = imagecolorallocate($im, 200,200,200); //rgb灰色标识符
//开始作图
imagefill($im,0,0,$gray);
while(($randval=rand()%100000)<10000);{
//将四位整数验证码绘入图片
session_start();
$_session['login_check_num'] = $randval;
imagestring($im, 5, 10, 3, $randval, $black);
}
//加入干扰象素
for($i=0;$i<200;$i++){
$randcolor = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);
}
//输出验证图片
imagepng($im);
//销毁图像标识符
imagedestroy($im);
}
|
?>
静态页面上显示验证码代码为:
验证码:<input class="reg_main_code_input" type="text" name="reg_code" id="reg_code" />
<img id="reg_code_img"" width=100% src="code.php?action=verifycode" />
jquery部分的ajax验证代码为:
代码如下 |
复制代码 |
$.post("session.php",
{reg_code:$("#reg_code").val()},
function(data){
if(data === "1"){
//do...
}else{
do... }
}
); |
而协助ajax验证的php页面名为session.php,其代码为:
<?php
代码如下 |
复制代码 |
session_start();
//检验验证码
if($_post['reg_code'] == $_session['login_check_num']){
echo 1;
}else{
echo 0;
exit();
} |
?>
var_export
(php教程 4 >= 4.2.0, php 5)
var_export -- 输出或返回一个变量的字符串表示
描述
mixed var_export ( mixed expression [, bool return] )
此函数返回关于传递给该函数的变量的结构信息,它和 var_dump() 类似,不同的是其返回的表示是合法的 php 代码。
您可以通过将函数的第二个参数设置为 true,从而返回变量的表示。
eg:
var_export(array('a','b',array('aa','bb','cc'))) 这种与var_dump没什么区别;
$var =var_export(array('a','b',array('aa','bb','cc')),true),加上true后,不会再打印出来,而是给了一个变量,这样就可以直接输出;
echo $var;此时输出来的形式与var_dump()打印的相似。
eg2
代码如下:
$data = array ('name' => 'abc', 'job' => 'programmer','a'=>array('aa','cc','bb'));
$data = var_export($data,true);
echo $data;
输出形式如下:
代码如下:
array (
'name' => 'abc',
'job' => 'programmer',
'a' =>
array (
0 => 'aa',
1 => 'cc',
2 => 'bb',
),
)
下面的实例是利用php 正则替换函数 ereg_replace来把指定的字符替换成我想需要的字符实例。
代码如下 |
复制代码 |
$num = 'www.111cn.net';
$string = "this string has four words. <br>";
$string = ereg_replace ('four', $num, $string);
echo $string;
$num = '49';
$string = "this string has four words";
$string = ereg_replace ('four', $num, $string);
echo $string;
$string ="测试用文字";
echo "**********$string**********<p>";
$string = ereg_replace ("^", "<br>", $string);
$string = ereg_replace ("$", "<br>", $string);
echo "==========$string=========="; |
本教程是一款php ajax返回 json数据实例哦,就是利用ajax实时的接受json.php文件发送的数据请求,并且进行了处理。
代码如下 |
复制代码 |
<!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>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>php教程 ajax返回 网页特效on数据实例</title>
<script type="text/网页特效" language="javascript">
var xmlhttp;
function createxmlhttprequest()
{
//var xmlhttp=null;
try
{
// firefox, opera 8.0+, safari
xmlhttp=new xmlhttprequest();
}
catch (e)
{
// internet explorer
try
{
xmlhttp=new activexobject("msxml2.xmlhttp");
}
catch (e)
{
xmlhttp=new activexobject("microsoft.xmlhttp");
}
}
return xmlhttp;
}
function startrequest(id)
{
createxmlhttprequest();
try
{
url="json.php?cid="+id;
xmlhttp.onreadystatechange = handlestatechange;
xmlhttp.open("post", url, true);
xmlhttp.send(null);
}
catch(exception)
{
alert("xmlhttp fail");
}
}
function handlestatechange()
{
if(xmlhttp.readystate == 4)
{
if (xmlhttp.status == 200 || xmlhttp.status == 0)
{
var result = xmlhttp.responsetext;
var json = eval("(" + result + ")");
alert('name:'+json.name);
alert('age:'+json.age);
alert('id:'+json.id);
}
}
}
</script>
</head>
<body>
<div>
<input type="button" value="ajaxtest" onclick="startrequest(5);" />
</div>
</body>
</html>
|
json.php 文件
代码如下 |
复制代码 |
<?php
/**************************************************************
*
* 使用特定function对数组中所有元素做处理
* @param string &$array 要处理的字符串
* @param string $function 要执行的函数
* @return boolean $apply_to_keys_also 是否也应用到key上
* @access public
*
*************************************************************/
function arrayrecursive(&$array, $function, $apply_to_keys_also = false)
{
static $recursive_counter = 0;
if (++$recursive_counter > 1000) {
die('possible deep recursion attack');
}
foreach ($array as $key => $value) {
if (is_array($value)) {
arrayrecursive($array[$key], $function, $apply_to_keys_also);
} else {
$array[$key] = $function($value);
}
if ($apply_to_keys_also && is_string($key)) {
$new_key = $function($key);
if ($new_key != $key) {
$array[$new_key] = $array[$key];
unset($array[$key]);
}
}
}
$recursive_counter--;
}
/**************************************************************
*
* 将数组转换为json字符串(兼容中文)
* @param array $array 要转换的数组
* @return string 转换得到的json字符串
* @access public
*
*************************************************************/
function json($array) {
arrayrecursive($array, 'urlencode', true);
$json = json_encode($array);
return urldecode($json);
}
$array = array
(
'name'=>'希亚',
'age'=>20,
'id'=>$_post['cid']
);
|
echo json($array);
/*********
{"name":"希亚","age":"20"}
?>
标签:[!--infotagslink--]