user1299412
user1299412

Reputation: 409

Simple C++ web server with php support

im working on a simple C++ HTTP server as a school project and I would like to add php support for it. Post and Get methods should not be a problem, but Im stuck on a cookies. I googled for long and couldnt find, how php handles cookies, where it gives the output for http server such as Apache or how does it work in global. Any ideas how I could print this code:

<?php
setcookie("cookie[three]","cookiethree");
?>

to console so it can be read by my server and after some parsing(?) sent to a client?

Thanks guys

EDIT: This is really close example to what I need, but when I execute the script it shows empty array.. http://php.net/manual/en/function.headers-list.php

php version: PHP 5.3.6-13ubuntu3.2 with Suhosin-Patch (cli) (built: Oct 13 2011 23:09:42) Copyright (c) 1997-2011 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies

Upvotes: 1

Views: 2297

Answers (2)

Halim Qarroum
Halim Qarroum

Reputation: 14103

PHP get its superglobals variables (such as Cookies) from the HTTP server itself. When you parse a client request, you must store every key/value pair in an appropriate container (an HTTPRequest class perhaps).

When interfacing your server with PHP you should write a module like apache does (mod_php). To do this, you will have to write your own API for interfacing with the modules. This means for every module you'll have (php, python ...) you will have the same interface for your Inputs/Outputs.

When writing such an API, you should define an easy way to pass all the superglobals variables PHP needs from the server. I've written my own HTTP server for the same purpose and the documentation of PHP is a little tricky about this point but you can inspire yourself from PHP-CGI : there is a php.exe or simply php command on Linux/Windows which can take arguments such as variables if my memory is good. Anyway, there are several ways to pass these arguments to php and I used CGI for my server.

Hope that'll help you.

Upvotes: 3

Brendan Long
Brendan Long

Reputation: 54302

The way cookies work is that the server sends a Set-Cookie header:

HTTP/1.0 200 OK
Set-Cookie: myCookieName=myCookieKey
Set-Cookie: anotherCookie=anotherValue
// other headers and probably content

Then, a compliant HTTP client will send it back in subsequent requests:

GET /some/path HTTP/1.0
Cookie: myCookieName=myCookieKey; anotherCookie=anotherValue

It's way more complicated than that, but that's the basics.

To summarize, you need to:

  • Send a Set-Cookie header when your code requests a cookie to be set.
  • Look for a Cookie header when you're reading incoming requests.

Upvotes: 1

Related Questions