stockton Posted November 16, 2008 Share Posted November 16, 2008 Is it possible to retrieve PHP session variables from a C or C++ cgi program? If so where should I start learning? Quote Link to comment Share on other sites More sharing options...
Mchl Posted November 16, 2008 Share Posted November 16, 2008 Session variables are saved to file or database. It's a matter of retrieving stored data. Quote Link to comment Share on other sites More sharing options...
corbin Posted November 16, 2008 Share Posted November 16, 2008 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. Quote Link to comment Share on other sites More sharing options...
Vermillion Posted December 7, 2008 Share Posted December 7, 2008 My apologies for the random interruption, but why would you want to do that? Are you writing some sort of program that can interact with your website? Quote Link to comment Share on other sites More sharing options...
stockton Posted December 7, 2008 Author Share Posted December 7, 2008 Yes that is exactly what I am doing. Quote Link to comment Share on other sites More sharing options...
corbin Posted December 7, 2008 Share Posted December 7, 2008 Oh, I thought of an easier way to do it. You could write a custom session handler that would store data in a more easily manipulated format. Quote Link to comment Share on other sites More sharing options...
Mchl Posted December 7, 2008 Share Posted December 7, 2008 Still, I think the most difficult part would be assigning session ID to a user. Quote Link to comment Share on other sites More sharing options...
corbin Posted December 8, 2008 Share Posted December 8, 2008 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 Quote Link to comment Share on other sites More sharing options...
flibs Posted February 16, 2009 Share Posted February 16, 2009 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 Quote Link to comment Share on other sites More sharing options...
corbin Posted February 16, 2009 Share Posted February 16, 2009 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. Quote Link to comment Share on other sites More sharing options...
corbin Posted February 16, 2009 Share Posted February 16, 2009 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] Quote Link to comment Share on other sites More sharing options...
corbin Posted February 22, 2009 Share Posted February 22, 2009 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] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.