简体   繁体   English

如何使用 C++ cURL (libcurl) 管理一个简单的 PHP 会话

[英]How to manage a simple PHP session using C++ cURL (libcurl)

I'm writing a C++ client which is using libcurl for communicating with a PHP script.我正在编写一个 C++ 客户端,它使用 libcurl 与 PHP 脚本进行通信。

The communication should be session based, and thus the first task is to login and make the PHP script set up a session.通信应该基于会话,因此第一个任务是登录并使 PHP 脚本建立会话。

I'm not used to working with sessions either from C++ or PHP.我不习惯使用来自 C++ 或 PHP 的会话。 I basically know that it has to do with cookies and communicating session id.我基本上知道它与 cookie 和通信会话 id 有关。

I can't find any example on the curl homepage which demonstrates a simple session management use case.我在 curl 主页上找不到任何演示简单会话管理用例的示例。

I'm assuming it has something to do with one or many of the following options in curl:我假设它与 curl 中的以下一个或多个选项有关:

CURLOPT_COOKIE
CURLOPT_COOKIEFILE
CURLOPT_COOKIEJAR
CURLOPT_COOKIESESSION
CURLOPT_COOKIELIST

But I can't really see the big picture just from the documentation of CURLOPT_COOKIESESSION for instance.但是,例如,仅从 CURLOPT_COOKIESESSION 的文档中我无法真正看到大局。

Anybody who has done this, please share a simple piece of code which shows the concept.任何做过这个的人,请分享一段简单的代码来展示这个概念。

Regards问候

Robert罗伯特

As far as I understand it, CURL will handle session cookies automatically for you if you enable cookies, as long as you reuse your CURL handle for each request in the session:据我了解,如果您启用 cookie,CURL 将自动为您处理会话 cookie,只要您对会话中的每个请求重复使用您的 CURL 句柄:

CURL *Handle = curl_easy_init();

// Read cookies from a previous session, as stored in MyCookieFileName.
curl_easy_setopt( Handle, CURLOPT_COOKIEFILE, MyCookieFileName );
// Save cookies from *this* session in MyCookieFileName
curl_easy_setopt( Handle, CURLOPT_COOKIEJAR, MyCookieFileName );

curl_easy_setopt( Handle, CURLOPT_URL, MyLoginPageUrl );
assert( curl_easy_perform( Handle ) == CURLE_OK );

curl_easy_setopt( Handle, CURLOPT_URL, MyActionPageUrl );
assert( curl_easy_perform( Handle ) == CURLE_OK );

// The cookies are actually saved here.
curl_easy_cleanup( Handle );

I'm not positive that you need to set both COOKIEFILE and COOKIEJAR, but the documentation makes it seem that way.我不确定您需要同时设置 COOKIEFILE 和 COOKIEJAR,但文档使它看起来是这样。 In any case, you have to set one of the two in order to enable cookies at all in CURL.在任何情况下,您都必须设置两者之一才能在 CURL 中完全启用 cookie。 You can do something as simple as:你可以做一些简单的事情:

curl_easy_setopt( Handle, CURLOPT_COOKIEFILE, "" );

That won't read any cookies from disk, but it will enable session cookies for the duration of the curl handle.这不会从磁盘读取任何 cookie,但它会在 curl 句柄期间启用会话 cookie。

I have an example for command line curl in bash - logging in to PHPMyAdmin and then using its export function.我有一个 bash 命令行 curl 示例 - 登录 PHPMyAdmin,然后使用其导出功能。 Maybe it will help you:也许它会帮助你:

#!/bin/bash

PHPMYADMINURL="http://www.example.com/phpmyadmin/"

# Username and password, has to be URL-encoded
MYUSERNAME="username"
MYPASSWORD="password"

TMPCOOKIES="$(mktemp)" || exit 1

TOKEN=$(
        curl \
                --silent \
                --show-error \
                --data @- \
                --data "lang=en-utf-8" \
                --cookie-jar "$TMPCOOKIES" \
                --dump-header - \
                --url "$PHPMYADMINURL" \
                <<< "pma_username=$MYUSERNAME&pma_password=$MYPASSWORD" \
                | egrep 'token=[0-9a-h]+' \
                | head -1 \
                | sed -r 's/^(.*token=)([0-9a-h]+)(.*)/\2/' \
        ) || exit 1

curl \
       --cookie "$TMPCOOKIES" \
       --data "token=$TOKEN" \
       --data "export_type=server" \
       --data "what=sql" \
       --data "asfile=sendit" \
       --data "sql_data=something" \
       --data "sql_columns=something" \
       --data "sql_hex_for_blob=something" \
       --data "compression=gzip" \
       --url "$PHPMYADMINURL"export.php 1>&2 || exit 1

rm -f "$TMPCOOKIES" || exit 1

PHPMyAdmin uses tokens besides cookies so the code is a little more complicated than normally needed. PHPMyAdmin 使用 cookie 之外的令牌,因此代码比通常需要的要复杂一些。

A session in PHP has the purpose of preserving some state over several requests, since HTTP in itself is stateless. PHP 中的会话旨在为多个请求保留一些状态,因为 HTTP 本身是无状态的。 To get a session from PHP, simply request a php page that starts a session, and keep the cookie you get back for subsequent requests.要从 PHP 获取会话,只需请求一个启动会话的 php 页面,并保留您获得的 cookie 以供后续请求使用。

Starting a session in php is simple - call the session_start() function.在 php 中启动会话很简单 - 调用 session_start() 函数。 That function will resume an existsing session if the cookie exists in the request.如果请求中存在 cookie,该函数将恢复现有会话。 When the session is started, persistent variables can be set using the superglobal array $_SESSION.当会话开始时,可​​以使用超全局数组 $_SESSION 设置持久变量。 It's a good idea to store a 'is logged in'-token there =) To end the PHP session, set $_SESSION to array(), so that the token is destroyed.在那里存储“已登录”令牌是个好主意 =) 要结束 PHP 会话,请将 $_SESSION 设置为 array(),以便销毁令牌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM