<?php
    /*-----
        VARIATION of hum_conn_no_login for when SESSIONS
            are involved, AND when failure to connect
            implies that the current session should
            be DESTROYED;

        ALSO: since this is a variation anyway, think I
            will try assuming this is being used by a
            postback PHP and including
            a "Try again" link to the calling PHP; IS
            this a good idea or not?

        function: hum_conn_sess: void -> connection
        purpose: expects nothing,

            has the side-effect of trying to connect to
            Humboldt's Oracle student database based on where
            the PHP file resides,

            and, if successful, returns the resulting connection 
            object,

            but, if NOT successful,
            *   complains, including a "try again" link to the
                calling document,
            *   ENDS the document
            *   destroys the current session, and
            *   exits the current PHP document

        last modified: 2025-04-24
    -----*/

    function hum_conn_sess()
    {
        // get part of the username from where this is installed

        $os_username = substr($_SERVER["CONTEXT_PREFIX"], 2);

        // but the Oracle account you can log into using OCI is your 
        //     username plus _php

        $ora_php_username = "{$os_username}_php";

        // but, to ask to use blah_php's password to log in as blah,
        //     we need to express it in the form blah_php[blah]

        $conn_username = "{$ora_php_username}[{$os_username}]";   

        // grab the password from this user's .oraauth

        $ora_php_password =
            trim(file_get_contents("/home/{$os_username}/.oraauth"));

        // now: oci_connect expects:
        //    a username,
        //    a password,
        //    a connection string (can be null in this particular 
        //                         approach, so PHP can build it from 
        //                         environment variables),
        //    the desired character encoding (we'll use "AL32UTF8"),
        //    the desired session mode (we'll use the default, the 
        //                              PHP constant OCI_DEFAULT)

        $connectn = oci_connect($conn_username, $ora_php_password, 
                                null, "AL32UTF8", OCI_DEFAULT);

        // complain and exit at least somewhat gracefully if
        //     oci_connect fails to make a connection

        if (! $connectn)
        {
            ?>
    <p> Could not log into Oracle, sorry! </p>
    <p> <a href="<?= htmlentities($_SERVER['PHP_SELF'], ENT_QUOTES) ?>">
        Try again </a> </p>

    <footer>
    <hr />
    <p>
        Validate by pasting .xhtml copy's URL into<br />
        <a href="https://validator.w3.org/nu">
            https://validator.w3.org/nu
        </a>
        or  
        <a href="https://html5.validator.nu/">
            https://html5.validator.nu/
        </a>
    </p>
    </footer>
</body>
</html>

            <?php
            // destroy the current session, and exit this PHP now

            session_destroy();
            exit;
        }
    
        // if reach here, I connected!

        return $connectn;
    }
?>