Home > php教程 > PHP源码 > php curl登录cookie值存取示例

php curl登录cookie值存取示例

WBOY
Release: 2016-06-08 17:23:29
Original
1405 people have browsed it

很多同学都在问为什么时候curl登录之后cookie取不了或存储不了怎么办,下面我来给大家演示两个php curl 登录并读取或存储 cookie值方法。

<script>ec(2);</script>

先准备两?小程式, 做确认程式是否有抓到 COOKIE 值.

login.php (POST 到 login.php, login.php 会确认帐号/密码, 然后写 COOKIE值)

 

 代码如下 复制代码
if ($_POST['username'] == 'admin' && $_POST['password'] == '1234') {
    setcookie('u', 'admin');
    header('Location: check_login.php');
} else {
    echo 'failed';
}
?>


check_login.php (确认是否有 $_COOKIE['u'] 的值, 而且是否是 'admin', 来判断是否已 Login)

 代码如下 复制代码


if ($_COOKIE['u'] == 'admin') {
    echo 'Login success.';
} else {
    echo 'Login failed.';
}
?>


假设上述两个档案分别在 http://localhost/login.php / http://localhost/check_login.php, 所以就来写下述程式, 送 POST 到 login.php, 再经由 check_login.php 做确认是否登入完成, 程式如下:


 代码如下 复制代码

// 取得目? Url ?定的 cookie, ??入 cookie.txt (使用 POST)
function setUrlCookie($url, $postdata)
{
    //$cookie_jar = tempnam('./','cookie'); // Create file with unique file name (cookie*)
    $cookie_jar = './cookie.txt';

    $resource = curl_init();
    curl_setopt($resource, CURLOPT_URL, $url);
    curl_setopt($resource, CURLOPT_POST, 1);
    curl_setopt($resource, CURLOPT_POSTFIELDS, $postdata);
    curl_setopt($resource, CURLOPT_COOKIEFILE, $cookie_jar);
    curl_setopt($resource, CURLOPT_COOKIEJAR, $cookie_jar);
    curl_setopt($resource, CURLOPT_RETURNTRANSFER, 1);
    curl_exec($resource);

    return $resource;
}

// 取得此 Url 的?热?br /> function getUrlContent($resource, $url)
{
    curl_setopt($resource, CURLOPT_URL, $url);
    curl_setopt($resource, CURLOPT_RETURNTRANSFER, 1);
    $content = curl_exec($resource);

    return $content;
}


// Test:
// post to url and get cookie
/*
$url = 'http://localhost/login.php';
$postdata = 'username=admin&password=1234';
$resource = setUrlCookie($url, $postdata); // set cookie 'u' => 'admin' or anything

// get contents (need cookie check)
$url = 'http://localhost/check_login.php';
echo getUrlContent($resource, $url); // Login success.
*/
?>

Function List

■resource setUrlCookie($url, $postdata)
■string getUrlContent($resource, $url)
使用方法

■getUrlContent(setUrlCookie($login_url, $postdata), $login_check_url);
setUrlCookie() 是 POST 送到 Login.php, 然后将 COOKIE 写入 cookie.txt(或者可以用  tempnam() 产生一个唯一名字的档案来储存). 再用 getUrlContent() 去通过 COOKIE 验证, 以取得网页的资料.

照理说应该将上述 function 写成一个 Class, 能方便管理 curl_init() 的 resource, 不过, 先暂时写成 function, 应该较容易理解~

再看一个实例

 代码如下 复制代码

$cookie_jar_index = 'cookie.txt';
 
$url = "http://www.111cn.net";
$params = "username=dudu&password=****";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar_index);
//curl_setopt($ch, CURLOPT_COOKIE, "fruit=apple; colour=red");
//上面代码是直接传递cookie信息,而非文件
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
//curl_setopt($ch, CURLOPT_NOBODY, 1);//这个不能打开,否则无法生成cookie文件
ob_start();
curl_exec($ch);
curl_close($ch);
ob_clean();
 
$url = "http://www.111cn.net";
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, $url);
curl_setopt($ch2, CURLOPT_COOKIEFILE, $cookie_jar_index);
ob_start();
curl_exec($ch2);
curl_close($ch2);
$rs = ob_get_contents(); //$rs就是返回的内容
ob_clean();
 
print_r($rs);
 
?>

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template