Goto: 4C Home | 4C Docs | req4c Docs | req4c Function List req4c Concepts

req4c Concepts

In order to make best use of the req4c PHP interface, it is important to have a good understanding of the main concepts.


req4c Sessions

A req4c session is a way that state can be maintained between PHP req4c requests. From the PHP perspective a req4c session is a resource that is identified by a 24 byte cryptographically random base 64 string. The PHP application is free to save this string in any manner it sees fit. There is no secret information in this string. It can be saved in a cookie unencrypted. The req4c extension does not automatically save session ids. However, if you call req4c_open_session() without passing in a sessionid, req4c will look for a 'req4c_sessionid' cookie and use that when opening the session.

req4c does not automatically save any cookies. There are good reasons for this including: req4c sessions allow the PHP program to set session wide variables that are guranteed to be set any time the session that set the vars is open. These variables are never sent to the client browser. They are stored with session information in memory by the 4creqd process and sent automatically to the PHP program when a session is opened. The PHP program can set session vars usin the $_REQ4C_SESSION super global array. For example: $_REQ4C_SESSION['username'] = 'kjenglish'

From the 4creqd perspective, a session is this same session id along with other information stored only in memory by the 4creqd process. The other information includes: When a session is first created, it is associated with a single 4C server and 4C application. A single req4c session cannot be used to access different 4C servers or even different 4C applications on the same server. If your application needs to access multiple 4C servers, then it needs to use multiple req4c sessions.

4creqd has configuration options that allow you to specify the maximum length of time that a session can exist before being destroyed and the maximum time a session can remain idle before being destroyed.

When a session is in use, and a 4C program is run, 4creqd always sends the session id and the create time of the session to the 4csrvr process. The 4csrvr process can access these two values using the system variables sys.web_sid and sys.web_sts. The 4csrvr process, can then maintain state in whatever way is most suitable to the application. One real important thing to keep in mind about maintaining state is that there is no guarantee that 2 different calls, even if made by the same PHP script, to run 4C programs will be processed by the same 4csrvr process. This means you cannot use the FC_SESSION directory to store anything you expect to access in more then one req4c requests.

It is not necessary to use req4c sessions. req4c will not automatically use sessions. If you want to use sessions, you must explicitly create and open them. They are there as a tool to help the application maintain state when needed. It is assumed that most applications will need to maintain some state, so hopefully req4c sessions will be useful.

If you prefer to use PHP sessions req4c will send the PHP sessionid to the 4csrvr when running a 4C program and you can access this value using the system variable sys.web_phpsid.

You may even find it useful to combine req4c sessions with PHP sessions.


req4c ResultSets

A req4c ResultSet is everything sent from the 4csrvr process to the PHP program, except error messages and info messages. This includes: Though, Error messages and Information messages may be generated by a 4C program and accessed by a PHP program, they are not part of the req4c ResultSet.

A req4c ResultSet, though similar to an SQL ResultSet, has some significant differences. From the PHP perspective, a req4c ResultSet is a resource returned by the req4c_run() function call that persists until the PHP script ends or the resource is unset. It is possible for a PHP script to save multiple ResultSets in different variables.

Once a req4c ResultSet is returned to the PHP program, it can be queried in many ways. No req4c function call that queries a req4c ResultSet require any network access. It is all internal to the PHP engine.


req4c ResultRows

A req4c ResultRow is a single row of data generated by a 4C program. You must have a ResultSet resource in order to access any of the ResultRows. From a PHP perspective a ResultRow is always an array of strings. Depending on how you fetch the ResultRow, the array may be indexed numerically or keyed by the ResultRow column names. There are req4c functions for getting the 4C program name that generated a ResultRow and for getting the type of the ResultRow. The type of the ResultRow will either be the report name that generated the row, or the type passed to sys.send_result(). There may be places in the documentation where a ResultRow is referred to simply as a result and sometimes simply as a row. Both are the same as ResultRow.


req4c Information and Error Messages



req4c maintains a list of error messages and info messages. These messages can be accessed by the PHP program. Information and/or Error messages may be generated by: In addition to the above, the lib4cphp library will set a single error_num and a single error_msg separate from the error message list when the req4c function call fails. This message is always cleared at the start of any req4c function, except req4c_get_error_msg() and req4c_get_error_num().

There are req4c functions for Any req4c function that may generate error messages always clears the current message list first. If you need to process error/info messages, you need to either save them in a PHP variable for processing or make sure you do the processing before you call a req4c function that clears the list.

See the Comments section for each individual function to see if it clears the current message list or not.

Most req4c functions also clear the last error info which is stored in $_REQ4C['errno'] and $_REQ4C['errmsg']. The only ones that currently don't are:


req4c Function Call Processing

In order to have a responsive application it is important to know where network requests occur. Most req4c functions involve no network requests of any kind. There are some that involve round trip requests to the 4creqd process only. When the 4creqd process is on the same machine as the Web Server, these round trip requests can be extremely fast if the 4creqd server is configured to use domain sockets.

There is one req4c function, req4c_run() , that requires a round trip request between the PHP request and the 4creqd process as well as a round trip between the 4creqd process and a 4csrvr process. So rather than make one req4c_run() call to get a list and then make another req4c_run() call for every item in the list, it is better to design a program that can return all the needed information in one req4c_run() request. Of course, you need to be careful and make sure you are not returning 10s of thousands of rows at a time also.

The documentation for each of the req4c PHP functions includes types of network requests used in the Comments section.


Process Timing

Every request that communicates with the 4creqd process must first be authenticated. When the 4creqd process is running on the same machine as the Web Server, then this authentication will normally use the credentials of the connection to authenticate the process. This requires one round trip network request before the request is sent. The request itself, then requires another round trip network request. When domain sockets are in use, these types of round trip network requests are quite fast. This authentication is done only once per PHP request cycle. If one PHP request cycle makes multiple calls to req4c, the authentication is still only done on the first req4c request. A PHP request cycle is the running of one script from start to finish. A guess you would call this a PHP program instance also.

When a request requires communicating with a 4csrvr process, the 4creqd process first determines if it has a connection available with the proper authentication and then uses that connection for the request. If necessary, 4creqd will initiate another connection to a new 4csrvr process. This initiation is very slow and for this reason, 4creqd maintains a pool of threads with existing connections so that the initiation process does not need to occur frequently. There are configuration options that allow you to specify how large the thread pool should be and how many connections to maintain.


Superglobals

req4c defines two superglobal (also called autoglobal) arrays, $_REQ4C and $_REQ4C_SESSION. $_REQ4C always contains at least the following: Your PHP programs can query these values, but should not modify them.

$_REQ4C_SESSION is empty unless there is a req4c session open. If there is a req4c session open, then this array has all the variables that any PHP program has set into it. All your program needs to do to store variables is something like: $_REQ4C_SESSION['phone'] = '�'
The values you set in this array are stored in memory by 4creqd and sent to req4c everytime a session is opened.

Back to Top