dikidera
dikidera

Reputation: 2043

Sessions like PHP in C++?

Is it possible to use something like sessions like the ones in PHP in C++?

In PHP they're used like

$_SESSION['name'] = $value;

I really want to make the same application using C++ as PHP...requires a run-time parser.

My application written in PHP uses sessions so that i can fetch some stuff from a website(numbers) and store the value in a SESSION varible which i call last to compare it to the new value when the page is reloaded.

I'd rather have the same functionality in C++.

Seeing as this is going nowhere i am going to provide the PHP code

<?php
session_start();

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"http://dashjr.org/~luke-jr/programs/bitcoin/pool/balances.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);

echo 'Last BTC: '.$_SESSION['last'].'<br>';

$obj = json_decode($result,true);
echo 'Current BTC: ';
$new = (int)$obj["1Hy5h24yBYKtQ1vNGaxY5rBJSEuTiMkBkx"]["balance"]/100000000;
echo $new;

if((int)$_SESSION['last'] < $new)
{
    echo '<br><br>You earned more';
}
else
{
    echo '<br><br>You earned less';
}

$_SESSION['last'] = $obj["1Hy5h24yBYKtQ1vNGaxY5rBJSEuTiMkBkx"]["balance"]/100000000;



unset($result,$obj);

?>

I need to port that to C++.

Upvotes: 1

Views: 1388

Answers (4)

isekaijin
isekaijin

Reputation: 19742

The concept of a "session" as in a "PHP session" only makes sense if you are doing server-side Web development. C++ was designed to be as general-purpose as possible (whether that goal was achieved is a completely different story), so it does not natively support any concept related to any specific application. Which, in your particular case, means C++ has no native concept of a "session", of course.

Of course, in theory, you could do PHP-style Web development in C++, including using PHP-like sessions. But you would have to either (1) use some framework exposing PHP-like session functionality, which I doubt anyone has ever developed; or (2) implement said framework yourself, which would be really inconvenient. And there would no tangible advantage you could get in return for your inconvenience: The main reasons why people use C++ these days are (1) performance is too critical to use a higher-level language, and (2) compatibility with other systems written in C++ is a requirement. (1) does not apply, because, in most cases, a Web application's response time is dominated by the time it takes to transmit messages across the network, even for Web applications deployed in an Intranet. The time it takes a Web server to process a request, however complex, is often minuscule in comparison. And (2) does not apply because systems interoperating on the Web are developed following standards that ensure the technologies underlying these systems do not matter.

Most people saying they need to implement their applications in C++ are people who have already implemented said applications in their languages of choice (or their bosses' languages of choice) and found performance problems. In my experience, most software-caused performance problems arise from the fact applications are not properly designed and/or implemented with scalability in mind. For a Web application, things like minimizing the amount of server trips (each time a particular value/object/whatever has to be sent across the network in order to get a response back) are the kind of design decisions that can meaningfully affect performance.

In short: If performance problems are the reason why you want to implement this application in C++, you better analyze each part of your current PHP system, determine which one(s) are causing the performance issues, and redesign and reimplement them.

Upvotes: 1

Trevor Tippins
Trevor Tippins

Reputation: 2847

If your data requirements are small you could just store the value in a cookie which the caller will pass back to the C++ CGI program when it invokes it again.

EDIT: As this isn't a webapp, just use regular C++ IO functions to preserve the data, either in a file, a registry key (for Windows) or, if you've got extensive data requirements, an embedded database like Sqlite.

EDIT: If it's just sitting in a loop, and you don't need to preserve the value from one invocation to another (i.e. across program restarts), just store the value in a different variable. You can just use an STL hashmap as the "SESSION" if you want to use name/value pairs. Or am I missing something fundamental?

Upvotes: 1

bobflux
bobflux

Reputation: 11581

PHP...requires a run-time parser.

Nope, install apc or any other compiled code cache, and php is only parsed once. Of course the generated bytecode is still interpreted...

On big frameworks (lots of code) parsing time can be a lot longer than actual script execution, so this cuts down the page times quite a lot.

Upvotes: 0

chx
chx

Reputation: 11760

This makes no sense, PHP sessions are a kludge because the process does not survive one HTTP request to the next. I really dunno what do you want to achieve here. Maybe http://www.boost.org/doc/libs/1_46_1/doc/html/interprocess.html this?

Upvotes: 1

Related Questions