Jump to content

Retrieve session variables in C or C++


stockton

Recommended Posts

Yeah it's possible.

 

 

It will be a bitch though.

 

 

Here's what will need to happen:

 

-Read cookie sess id from HTTP headers in C/C++ program

-If there's not a cookie, make a new session file

-If there is, open the file and read the data into memory

-Close the file handle

-Deserialize it.  You could probably steal PHP's deserialize function and make it use a hash map or something instead of a PHP style array (which is, in some ways, a hash map).

 

So yeah...  Good luck ;p.

Link to comment
Share on other sites

  • 3 weeks later...

Very worst case, it could just be passed with GET.  The cookie way would be easy to though.  I think the hardest part would be parsing the session data.  With C/C++ CGI programs, the data is passed in through the standard input stuff, so it would just be a matter of reading the headers and grabbing either the cookie or get info.  Libraries already exist to do both of those things.

 

I'm tempted to try this my self later.... lol

Link to comment
Share on other sites

  • 2 months later...

I find myself wanting to do the exact same thing.  I'm finding I have to write part of the website I am working on in C (soing some pretty meaty image manipulation and PHP just isn't fast enough), and I need it to use some of the PHP session data to do the manipulation.

 

I'd like to do it all in C as it's cleaner, but I feel I may have to resort to wrapping the C program in PHP to get this site done in time.

 

If there's been any breakthroughs in this respect (code snippets etc) I would be overjoyed to see them

 

Thanks

 

M

Link to comment
Share on other sites

I started coding some stuff, but I got caught up parsing GET/POST data.  I don't remember if I ever got anything done in the way of sessions.

 

 

 

Now that I think about it though, the SESSID cookie (or what ever it would be called) would be available to the C application, therefore the session ID could be determined without a GET string or what ever.  Then, it would be a matter of parsing PHP's session files (which would be a bitch).

 

 

I guess one could write a session class in PHP that does sessions normally and then makes an easily parsable ini file (GetPrivateProfileString or what ever could be used to parse it).

 

 

 

Hrmmm, I think I shall dig up the code later and do something.

 

 

If I can find what I coded before I'll post it, but I don't know where it is.

Link to comment
Share on other sites

Wow....  I'm embarrassed that I coded this.  It appears that I didn't get the idea of headers or prototypes or anything.  And some of the code is iffy....

 

Fixing parts up and making a very basic session handler now.

 

 

(The project files in the attachment are VS08 by the way, and this is coded in C++.)

 

[attachment deleted by admin]

Link to comment
Share on other sites

Finally got around to revising it last night and just now....

 

 

The code attached can share sessions between C++ and PHP (the C++ code could be converted down to C, but have fun ;p).

 

 

Note:  This code is just example code. I personally would not use it in a production environment.  The PHP part is not optimized, and the C++ code is far from clean.  Also, if I were to ever really do something like this, I would make C++ parse the PHP formatted session files, not have two different files going.

 

[attachment deleted by admin]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.