Creating a session (create_session)

create_cookie_session()

Create a cookie_based session

create_session()

Create a session


SYNTAX

create_cookie_session(session_file_id [, timeout])

create_session(session_file_id [, timeout])

Create a unique session ID, store it in the specified session file in the field session_file_id.SessionID, and insert a record with that key into the session file. Each character in the session ID is randomly generated.

The optional timeout parameter can be used to specify the number of seconds after which the session should expire if there is no activity from the client. This value is stored in the session file in the field session_file_id.TimeOut, and defaults to 600 (10 minutes).

If create_session() is used, the session ID must be placed in all URL’s that refer to this session on this server, or must be stored on each output web page as a hidden field called SC_SESSION_ID. The value of the field session_file_id.Type is set to SESSION_TYPE_PARAMETER. This function must be called before encode_session_url(), which encodes the current session ID into a supplied URL string.

create_cookie_session() automatically stores the session ID in a temporary cookie called SC_SESSION_ID on the user’s machine. The cookie is returned to the server every time a new nph-srep program is executed. The value of the field session_file_id.Type is set to SESSION_TYPE_COOKIE. This method is more secure than embedding it in URLs, but can only be employed if the user allows cookies. A session must be created before any HTML output, so this function must be called before any print or put statement is executed.

These functions return 0 (OK) or a negative value to indicate an error. The possible errors, defined in http.h with the prefix WWW_ERR_, are:

PARAMETER; SESSION_FILE_CLOSED; NO_*FIELDNAME*_FLD; GENERATING_SESSION

create_cookie_session() can also generate the NOT_IN_HEADER error.


EXAMPLE

This example illustrates how to check whether the user allows cookies, and execute the appropriate function to create the session.

!ofile Session session update
!temp RetVal,,i4
!temp Value,,a20

     run report rep1
     exit

!report rep1 {
     put_cookie("CHK", "VALUE")                   /* Place a cookie for checking purposes */
     tmp.RetVal = get_cookie_value("CHK", value)  /* Attempt to read it back */
     if (tmp.RetVal = 0) (                        /* Browser accepts cookies*/
          create_cookie_session(Session)
     } else {                                     /* or else it doesn't*/
          create_session(Session)
     }
     ...
}

RELATED TOPICS

Creating web pages with Sculptor

Internet functions

Internet function error codes

The session file