CS 318 - Week 13 Lecture - 4-25-13 Intro to PHP, continued * some useful PHP functions for checking the status of array keys, etc.: NULL - a PHP type (?!), the absence of an object array_key_exists(desired_key, desired_array) * checks IF desired_key exists in desired_array isset(desired_variable) * determines IF desired_variable is set AND is not NULL * ONE way to redirect in PHP: * header function * 'Location: desired_url' ...and will go to that location * MUST be BEFORE ANY output on that page! (before even the <!DOCTYPE element more lovely global things: * $_SERVER is an associative array with server info in it * PHP_SELF is a name in this array whose value is the value of the file name for the containing PHP document action="$_SERVER[PHP_SELF]" * the above can be inserted into the boilerplate for a form rather reasonably with a HERE document, ANOTHER form of PHP string: HERE document: a PHP string that starts with: >>>DESIRED_LABEL boilerplate with PHP variables that will be replace with their values and your line breaks and etc DESIRED_LABEL * that closing label CANNOT be indented, must be ALONE on the line (except possibly for a single semicolon after it), cannot have ANYTHING after it, even a comment! * connecting to the HSU Oracle student database * we are using OCI - Oracle Call Interface (one of MANY packages PHP has for this...) * first: set up a connection string $db = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP) (HOST = cedar.humboldt.edu) (PORT = 1521)) (CONNECT_DATA = (SID = STUDENT)))"; * here's the actual connection attempt (and abort if it fails): $conn = oci_connect($username, $password, $db); if ($conn == FALSE) { ...take desired action... exit; // this IS a PHP-allowable thing } * to set up a SQL statement and then execute it: $stmt = oci_parse($conn, "select hiredate, salary, commission ". "from empl"); oci_execute($stmt, OCI_DEFAULT); * now, to loop through the results: while (oci_fetch($stmt)) { ?> <p> <?= oci_result($stmt, "HIREDATE") ?> </p> <p> <?= oci_result($stmt, "SALARY") ?> </p> <p> <?= oci_result($stmt, "COMMISSION") ?> </p> <?php } * and when you are done: oci_free_statement($stmt); oci_close($conn); * see posted examples for how to commit (necessary if you change the database), and how to call stored procedures and stored functions; SESSIONS * there is a global associative array $_SESSION that can contain session attributes and their values; * IF you are using cookie-based sessions in PHP, (as we will be), START your php file with: session_start(); ...that's EVEN BEFORE your <!DOCTYPE or <html> tag! (should be the FIRST thing!!) (not even a blank line beforehand) (although can be after <?php ...!) * if I want a session attribute named thing, $_SESSION['thing'] = 'whatever I want'; ...and later in the same session, I can grab: $_SESSION['thing'] and its value is 'whatever I want' * you can use unset to, well, un-set the value for a session attribute: unset($_SESSION['thing']); * when DONE with a session, call session_destroy(); session_destroy(); * see try-session1.php, try-trio.php