DB not connecting - but will on refresh! HELP!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tsg
Forum Contributor
Posts: 142
Joined: Sun Jan 12, 2003 9:22 pm
Location: SE, Alabama
Contact:

DB not connecting - but will on refresh! HELP!

Post by tsg »

This is driving me nuts!

Everything has been working fine, but now I notice when I hit a page for the first time on this site I am working on I get a unable to conenct to database message. I refresh the page, and everything comes up and on all following pages.

It has something to do with my sessions because when I take out the session information it works fine.

here is my code. This is in a file called db.php and included at the top of all the pages:

Code: Select all

session_start ();
if(empty($session_id)) {
$cust_ip = "$REMOTE_ADDR";
session_register("cust_ip" );
$xxid2 = str_replace(".", "", $cust_ip);
$session_id = (date("ymdHis" ) . "$xxid2" );
session_register("session_id" );
$cust_refer = $HTTP_REFERER;
session_register("cust_refer" );
}
$dbloc = "localhost";
$dbuser = "XXXXX";
$dbpass = "XXXXXX";
$dbname = "XXXXXX";
$dbcon = @mysql_connect("$dbloc", "$dbuser", "$dbpass" );
if (!$dbcon) {
echo( "Unable to connect to the database" );
exit();
}
if (! @mysql_select_db("$dbname", $dbcon) ) {
echo( "Unable to locate the $dbname database " );
exit();
}

In this order, the $dbloc - $dbuser, etc don't even register (included in error messge and didn't show).

If I add the session register information after the sql, does not create a session_id.

Any Advice???? (PS: This does not have anything to do with the previous post about speeding up query.)

Thanks,
Tim
User avatar
redcircle
Forum Commoner
Posts: 43
Joined: Fri Jan 31, 2003 8:47 pm
Location: michigan, usa

Post by redcircle »

If I were you I would look into PHP's super globals. Makes things much easier. Plus I have heard rumors of session_register being depricated. When using super globals you have much more flexibility on what you can do and from what i have seen less bugs. Also you should code with the idea that register_globals is turned off. Here's your code using super globals. Also the order in which you define a custom session_id is a little backwards. You must define the session_id before the session_start

Code: Select all

$xxid2 = str_replace(".", "", $_SERVER['REMOTE_ADDR']); 
$session_id = (date("ymdHis" ) . $xxid2 ); 
session_id($session_id);
session_start (); 
if(session_id()) { 
$_SESSION['cust_refer'] = $_SERVER['HTTP_REFERER']; 

} 
$dbloc = "localhost"; 
$dbuser = "XXXXX"; 
$dbpass = "XXXXXX"; 
$dbname = "XXXXXX"; 
$dbcon = @mysql_connect("$dbloc", "$dbuser", "$dbpass" ); 
if (!$dbcon) { 
echo( "Unable to connect to the database" ); 
exit(); 
} 
if (! @mysql_select_db("$dbname", $dbcon) ) { 
echo( "Unable to locate the $dbname database " ); 
exit(); 
}
see http://us2.php.net/variables.predefined for more info.
tsg
Forum Contributor
Posts: 142
Joined: Sun Jan 12, 2003 9:22 pm
Location: SE, Alabama
Contact:

Post by tsg »

Thank you. What you posted didn't exactly work because it created a new ID each time, but I changed it around a little and now works!

This is what I ended up with:

Code: Select all

session_start (); 
if( $_SESSION['registered'] != true ) {
$_SESSION['cust_ip'] = $_SERVER['REMOTE_ADDR']; 
$xxid2 = str_replace(".", "", $_SESSION['cust_ip']);
$session_id = (date("ymdHis") . "$xxid2");
$_SESSION['cust_refer'] = $_SERVER['HTTP_REFERER']; 
$_SESSION['registered'] = true;
$_SESSION['session_id'] = $session_id; 
}
Then the database connection.

Thanks again!
Tim
User avatar
redcircle
Forum Commoner
Posts: 43
Joined: Fri Jan 31, 2003 8:47 pm
Location: michigan, usa

Post by redcircle »

Just goin off of the docs :)
Post Reply