How to access php session data within a HHVM process using C++? - php

Here is some background info:
I have a dynamic loaded extension running on HHVM using FastCGI
I have a login'ed session ready. e.g. example.com/login.php
I have my own TCP server running within the HHVM fastCGI process, listening port e.g. 8080.
(Assuming the TCP server is started when the extension is loaded, and wait for web socket connections)
What I want to do is, to re-use the session started by login.php in my own TCP server that serves web socket connections. The client should already give me the PHPSESSID cookie from the HTTP header, all I think I need to do is to use that ID to lookup somewhere in the HHVM runtime (because we're in the same process), checking if that session exist or something like that. So I can safely stream data to that websocket.
Is there an API I can call to do that?
Is there any example I can follow? Can someone help me please?
Thank you,

Related

PHP script running as Socket Server and Client simultaneously

For a new project I need to implement remote desktop protocols. The addresses of the remote need to be secured and may never get sent to the client. After a lot of research and some tests I found Guacamole, which also has a Java client. The project is designed as an API though, so I started porting some bits of the Java client example to PHP.
The use case will be the following:
User logs into my service (Laravel application)
WebSocket connection establishes to a constantly running PHP script (using HOA\WebSocket)
Upon authorization a TCP socket needs to be established to the Guacamole Daemon
Commands coming via WebSocket need to be directed to the Guacamole Daemon and vice versa
What makes this complicated is the fact that the application needs to be able to serve multiple clients simultaneously. Multiple TCP sockets need to be established and multiple WebSocket connections need to be managed all at once.
For my simple test I opened the socket via fsockopen and then looped to wait for data. With this I obviously can't listen to multiple sockets at once (at least realistically), but I stumbled upon the React Socket Client library:
Think of this library as an async version of fsockopen() or stream_socket_client().
This sounds like it is what I need, but then again, I'm using HOA and its WeSocket server, which apparently also runs in a loop (when invoking WebsocketConnectionHandler->run()).
Should I even be using React's Socket Client or should I try to use HOA's Socket library instead (seeing as I'm already using WebSocket from that)? Are React and HOA even compatible in their event loop, so could I listen to WebSocket clients and a TCP connection at the same time?
If so, could anyone give me some hints or examples on how to get started with coupling these two? Thanks!

Persistant TCP Connection To Speed Up Remote File Fetching

I have a PHP script that uses file_get_contents to fetch a file on a remote server on every page load. Is it possible to make a persistent connection between the two servers to speed up the time it takes to fetch this file?
Your PHP process is likely ending each request, so you will have to handle this outside of the main PHP process.
I would recommend setting up Nginx as a proxy, and pointing your PHP script at Nginx. You can then configure Nginx to use HTTP/1.1 keep-alive, which will keep a persistent connection open if requests are coming through regularly.

Accessing socket service using persistent socket

I have a tcp socket service MyServ running on the background (using Java, doesn't really matter though), and a web server with php that accesses MyServ using persistent socket (pfsockopen).
The problem is, if one php request stopped for whatever reason, it leaves some un-read data in the persistent socket, and the following php request will get an error when reading this socket.
I wonder how's other services with similar scenario (like php-mysql, php-memcached) dealing with this problem? more specificly, how can php tell that a used persistent socket is clean?

when is php executed on apache2?

I am trying to figure out when exacly are php scripts interpreted on apache server via mod_php in connection lifetime(tcp session lifetime).
PHP scripts are executed by Apache in response to HTTP requests. A HTTP request requires a fully established TCP connection.

Apache - handling TCP connections, but not HTTP requests

I have a GPS unit that can send data over a TCP connection, but I don't have the ability to modify the message that it sends so it would come to my server in the form of an HTTP request - it can only send a message in a predefined format.
So, I have the following questions:
1) Is it possible to have Apache handle a TCP connection that doesn't come in the form of an HTTP request, and have the message that is sent be processed by a PHP script?
2) If #1 isn't possible, how would you recommend I handle the data being sent to my server?
I will potentially have hundreds, if not thousands, of these GPS units sending data to my server so I need an efficient way to handle all of the connections coming in (which is why I wanted Apache or some other production worthy server to handle the TCP connections). I would like to be able to deal with the message sent over the connection with PHP since that is what the rest of my application runs on, and I will need to insert the data sent into a database (and PHP is really good at doing that kind of thing).
In case it matters, the GPS unit can send data over a UDP connection, but from what I have read Apache doesn't work with UDP connections.
Any suggestions would be welcome.
To answer your questions:
1) Not without major modification
2) Build your own server. This is easily done with several platforms and in several languages. I personally like to use the Twisted Framework because Python is relatively simple to use and the framework is very flexible.
Using Apache wouldn't be practical as it's using a nuclear bomb when a firecracker will suffice. Creating a PHP server is quite simple on Linux with the help of xinetd.
Modify /etc/services. Say you want your service to run on port 56789. In /etc/services, add the line:
gpsservice 56789/tcp
In /etc/xinet.d/, create a file named gpsservice:
service gpsservice
{
socket_type = stream
protocol = tcp
wait = no
user = yourusername
server = /path/to/your/script
log_on_success = HOST PID
disable = no
}
Create your PHP script (chmod it to be executable):
#!/usr/bin/php
<?php
// do stuff
?>
Restart xinetd service xinetd restart
You now have a quick TCP server written in PHP.

Categories