Link to home
Start Free TrialLog in
Avatar of tnowacoski
tnowacoski

asked on

php/OCI signon credentials

PHP5/OCI8 newbie here.  I am looking for suggestions on best practices on how to store username/password/database information from a signon form.  I have a simple web site with a main navigation page, a logon page, and some database update pages.  All of these pages have the proper OCI connection string but I am not sure how to retain the logon information to use on each individual pages.  Currently all of my pages can connect to the Oracle database but the connections strings are hardcoded.  Does anyone have examples?
Avatar of boon86
boon86
Flag of Malaysia image

<?php

// Connects to the XE service (i.e. database) on the "localhost" machine
$conn = oci_connect('hr', 'welcome', 'localhost/XE');
if (!$conn) {
    $e = oci_error();
    trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}

$stid = oci_parse($conn, 'SELECT * FROM employees');
oci_execute($stid);

echo "<table border='1'>\n";
while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
    echo "<tr>\n";
    foreach ($row as $item) {
        echo "    <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : "&nbsp;") . "</td>\n";
    }
    echo "</tr>\n";
}
echo "</table>\n";

?>

Open in new window


<?php

$conn = oci_connect('hr', 'welcome', 'localhost/XE', 'AL32UTF8');
if (!$conn) {
    $e = oci_error();
    trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}

$stid = oci_parse($conn, 'SELECT * FROM employees');
oci_execute($stid);

echo "<table border='1'>\n";
while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
    echo "<tr>\n";
    foreach ($row as $item) {
        echo "    <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : "&nbsp;") . "</td>\n";
    }
    echo "</tr>\n";
}
echo "</table>\n";

?>

Open in new window


read more at:
http://www.php.net/manual/en/function.oci-connect.php
http://www.oracle-base.com/articles/misc/WebScriptingForOracle.php
Avatar of tnowacoski
tnowacoski

ASKER

I have a handle on the OCI8 connection logic.

$conn = oci_connect('hr', 'welcome', 'localhost/XE');

Open in new window


How do I get the Username, Password, Database information from a Logon form to the connection logic on another page?  Session Storage Variables?  None of the examples that I can find are dynamic.  They all have a static connection string like the examples above.
you could use post from your form and store the logon detail to session

<?php
session_start();
session_register("user");
session_register("pass");
session_register("host");


if(isset($_POST['save']))
{

$user = $_POST['user'];
$pass = $_POST['pass'];
$host = $_POST['host'];

$_SESSION['user'] = $user;
$_SESSION['pass'] = $pass;
$_SESSION['host'] = $host;

}

?>

and use them later by:
<?php
session_start();
echo $_SESSION['user'];
echo $_SESSION['pass'];
echo $_SESSION['host'];

//or assign it to variable:

$user =  $_SESSION['user'];
?>

you can use session on unlimted page as long as your browser is opened after posted form
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ray: I am following what you are saying, can you look to see what is wrong with this code based on your recommendations?
I am getting an internal server error.

<?php 

// Start session
session_start();

// Fill Session Variables
// THE VARIABLES WE EXPECT
$vars = array( 'username', 'password', 'database');

if ($_POST['database'] == 'IFSTEST' )
  $database='172.22.9.246/IFSTEST';
else
  $database='172.22.9.111/IFS75';
  
// Try connecting to the database 
echo '<b>Trying to Connect to Oracle</b> with:<br>';

echo "$_POST['username'] @ $database <br>";
$conn = oci_connect($_POST['username'], $_POST['password'], $database);

if (!$conn)
{ 
      $e = oci_error();   // For oci_connect errors pass no handle 
      echo '<b><font color="red">FAILED</font></b> : ' . htmlentities($e['message']); 
}
else
{ 
	$_SESSION['username'] = $_POST['username'];
	$_SESSION['password'] = $_POST['password'];
	$_SESSION['database'] = $database;
	
	/*
    foreach ($vars as $key)
    {
        $_SESSION[$key] = $_POST[$key];
    }
	*/
}
    oci_close($conn); 
    echo 'Oracle connection closed<br>';
?> 

Open in new window

That looks right to me.
yah, had a syntax error but that is fixed and working now!  Thanks
Great!  Thanks for the points, ~Ray