Title: Simple C++ web server with php support
Tags: php;c++;http
Question: 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");
?&gt;
```

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

Comment: @Justinᚅᚔᚈᚄᚒᚔ ups , thanks for warning

Comment: Check this tutorial [phpcookies](http://www.tizag.com/phpT/phpcookies.php)

Comment: Dafarov: i guess u did not understand my post. basically i want to be able to get the cookie header code just running php script in console so i can read it by mu http server and send to user

Here is the accepted answer: 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.

Comment for this answer: You should first take a look at how Apache interfaces with PHP (through SAPI, Server Application Programming Interface for example). Also look at FastCGI, what it does and how it is used to communicate between PHP and apache and how you could use it in your web server.
Comment for this answer: Look at http://fuzzytolerance.info/cgi-vs-sapi-vs-fastcgi/ for an explanation about the two and a listing of the differences between them.
Comment for this answer: thanks, probably most helpful answer so far :) I tried to override setcookie with echo inside, but thats not possible :/ I will look for more info about the running CGI parameters, but If you could remember how it was done(what params u used) please do so, I would be really thankful:) thanks

Here is another answer: 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.


Comment for this answer: I know I have to set a cookie header, already prepared my code for that.. but How do I know what cookie to set, when I run in terminal: "php script.php" and in the code is smth like setcookie("cookie1","val1"); It does not print the cookie to console, so how am I supposed to get it?

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:03/28/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php><c++><http>', 'Title': 'Simple C++ web server with php support', 'ViewCount': 2036, 'id': 9916782, 'user_AboutMe': None, 'user_AccountId': 1362269.0, 'user_DisplayName': 'user1299412', 'user_Reputation': 379.0, 'user_Views': 47.0, 'user_id': 1299412.0}}