数据传输是用户交互中最重要的环节,下面收集了几个数据传输的方法,作为记录(未测试,在使用之前需要测试,如果后面我测试了,会对已测试项进行标注)

一. 网址传递

<a href=”test.php?id=3&name=mike”>next</a>

可用 $_GET['id'] 和$_GET['name']访问GET 的数据。
二. Cookie 传递
1、 设置Cookie
简单的:

SetCookie("MyCookie", "Value of MyCookie"); 

带失效时间的:

SetCookie("WithExpire", "Expire in 1 hour", time()+3600);//3600秒=1小时

什么都有的:

SetCookie("FullCookie", "Full cookie value", time()+3600, "/forum", ".phpuser.com", 1);

如果要设置同名的多个Cookie,要用数组,方法是:

SetCookie("CookieArray[0]", "Value 1");
SetCookie("CookieArray[1]", "Value 2");

2、 接收和处理Cookie

echo $_COOKIE[‘MyCookie’];
echo $_COOKIE[‘CookieArray[0]’];
echo count($_COOKIE[‘CookieArray’]);

3、删除Cookie 
要删除一个已经存在的Cookie,有两个办法: 
一个办法是调用只带有name参数的SetCookie,那么名为这个name的Cookie 将被从关系户机上删掉;
另一个办法是设置Cookie的失效时间为time()或time()-1,那么这个Cookie在这个页面的浏览完之后就被删除了(其实是失效了)。 
要注意的是,当一个Cookie被删除时,它的值在当前页在仍然有效的。
三. Session传递
test1.php

<?
session_start();
session_register("count");
echo $count=0;
?>

test2.php

<?
session_start();
echo $count++;
?>

四.ajax post

jQuery(function($)
{
$.ajax({
type: 'post',
url: 'xxx.php',
data: strPost,
dataType: 'json',
success: function(data){
}
});
});

五.curl post

exm1:

$ch = curl_init();
$useragent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)";
$header = array('Accept-Language: zh-cn','Connection: Keep-Alive','Cache-Control: no-cache');
curl_setopt($ch, CURLOPT_REFERER, "http://www.xxx.com");
curl_setopt($ch, CURLOPT_URL, "http://www.xxx/login/login.php");
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);
curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIEJAR);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$strPOST = "url=/home/&email=xxx@sohu.com&password=xxx";
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $strPOST);
$result = curl_exec($ch);

exm2:

function cevin_http_open($url, $conf = array())
{
if(!function_exists('curl_init') or !is_array($conf)) return FALSE; $post = '';
$purl = parse_url($url); $arr = array(
'post' => FALSE,
'return' => TRUE,
'cookie' => 'C:/cookie.txt',);
$arr = array_merge($arr, $conf);
$ch = curl_init(); if($purl['scheme'] == 'https')
{
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
} curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, $arr['return']);
curl_setopt($ch, CURLOPT_COOKIEJAR, $arr['cookie']);
curl_setopt($ch, CURLOPT_COOKIEFILE, $arr['cookie']); if($arr['post'] != FALSE)
{
curl_setopt($ch, CURL_POST, TRUE);
if(is_array($arr['post']))
{
$post = http_build_query($arr['post']);
} else {
$post = $arr['post'];
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
} $result = curl_exec($ch);
curl_close($ch); return $result;
}
$postfield="userName=test&year=2008&passWord=123456&Submit=%CC%E1%BD%BB"; //curl post的数据格式
$post = 'aa=ddd&ee=d';
echo cevin_http_open('http://localhost/ret.php',array('post'=>$postfield)); //post 过去的目标页http://localhost/ret.php

ret.php

<?php
if($_POST)
print_r($_POST);
?>

六.fsockopen post

function post($data)
{
//创建socket连接
$fp = fsockopen("www.xxx.com",80,$errno,$errstr,10) or exit($errstr."--->".$errno);
//构造post请求的头
$header = "POST /xxx.aspx HTTP/1.1\r\n";
$header .= "Host:127.0.0.1\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: ".strlen($data)."\r\n";
$header .= "Connection: Close\r\n\r\n";
//添加post的字符串
$header .= $data."\r\n";
//发送post的数据
fputs($fp,$header);
$inheader = 1;
while (!feof($fp)) {
$line = fgets($fp,128); //去除请求包的头只显示页面的返回数据
if ($inheader && ($line == "\n" || $line == "\r\n")) {
$inheader = 0;
}
if ($inheader == 0) {
$ret=$line;
}
}
fclose($fp);
return $ret;
}
$str = "Name=".urlencode($name)."&sex=".urlencode($sex)."&Age=".urlencode($age)."&Phone=".urlencode($phone)."&IDCard=".urlencode($idcard)."&Time=".urlencode($time)."&Email=".urlencode($email);
$UserID = post($str); //返回值

七.include框架

include('get.php');

来源:http://bbs.csdn.net/topics/330033219

最新文章

  1. Unity3d uGUI适配
  2. 深入.net(继承)
  3. SQL获取时间段内的所有月份
  4. C++之: CDib类
  5. org.springframework.jdbc.datasource
  6. 详解 Android 的 Activity 组件
  7. CSS中 清除浮动解决“包含问题”
  8. linux 下 zookeeper安装
  9. CLR之委托的揭秘(二)
  10. yyb要填的各种总结的坑
  11. 使用 NLog 给 Asp.Net Core 做请求监控
  12. 使用ANY和ALL条件
  13. OI养老专题03:让坏人出列的约瑟夫问题
  14. 《内蒙古自治区第十三届大学生程序设计竞赛试题_H 公孙玉龙》
  15. Scss 与 Sass 是什么,他们的区别在哪里?
  16. nginx + iis 使用介绍
  17. vue-awesome-swiper组件不能自动播放和导航器小圆点不显示问题
  18. 用.NET CORE做项目,VS里编译碰到‘。。。。包降级。。。。’错误
  19. Intercepting a 404 in IIS 7 and up
  20. openx _金额

热门文章

  1. ORACLE错误1033出现和ORA-00600错误解决的方法
  2. spring基于通用Dao的多数据源配置
  3. java多线程设置优先级
  4. springboot配置文件加载位置
  5. luogu1993 小K的农场
  6. rk3288对于parameter参数文件的解析处理【转】
  7. windows10+arch linux双系统 uefi启动
  8. Redis(四)-配置
  9. 【DP悬线法】奶牛浴场
  10. Codeforces 609D 被二分教做人