For the life of me, I cannot get mysql_pconnect() to work the way I think it should. My understanding is that a mysql_connect will be automatically killed at the end of a script. However, mysql_pconnect() is supposed to get around this.
I am loading pages that are dynamic based on database content. I am able to authenticate using the db and pull data from it on the first page. However, every page beyond that won't use the same connection. So I am faced with 3 options right now:
1) I completely misunderstand mysql_pconnect and how it works
2) Register a session variable for the user name AND password (I am doing the user name right now but the idea of registering the password doesn't thrill me from a security stand point).
3) Regsiter a session variable that is the db connection (probably talking out of my butt on this one though).
Here is my code as it exists now. The data is pulled from the db in the DisplayHeader function which works on this page. When I go to a different page, I got errors saying that the connection could a connection could not be opened.
require_once ("load-libs.php");
session_start ();
if (@ !$_SESSION["valid_user"])
{
if (@ $db = login ($_POST["user_name"], $_POST["passwd"]))
{
$_SESSION["valid_user"] = $_POST["user_name"];
}
else
{ CheckValidUser (); }
}
CheckValidUser ();
DisplayHeader ("Welcome to X-Stuff USA!");
DisplayFooter ();
function login ($name, $pw)
{
if ($db = OpenDB ("$name", "$pw"))
return 1;
else return $db;
}
function OpenDB ($user, $pw)
{
if (!$db = mysql_pconnect ("ns25.zabco.net", "$user", "$pw"))
{
die (sprintf ("<br><br>Cannot connect to db!\n<br><b>Reason: %s (%d)\n",
htmlspecialchars (mysql_error()),
mysql_errno()));
return false;
}
elseif (!mysql_select_db ("db0666"))
{
die (sprintf ("<br><br>Cannot select db!\n<br><b>Reason: %s (%d)\n",
htmlspecialchars (mysql_error()),
mysql_errno()));
return false;
}
return $db;
}
function CheckValidUser ()
{
if (@ !$_SESSION["valid_user"])
{
DisplayHeader ("Cannot View Page.");
echo "<p>You must be logged in to view this page.</p>";
echo "<p><a href=\"index.php\">Click here</a> to login.</p>";
DisplayFooter ();
exit;
}
}