divadiva Posted November 19, 2008 Share Posted November 19, 2008 I am stuck with strange problem. But before telling the details ,I would like to tell what I am shooting for. When I login to the website ,I get connected with database1.For accessing secured information I have to enter password for database2.The database name is hardcoded in webpage. What I am trying to achieve is to get rid of the password screen for database2.Instead of entering the password everytime for accessing database2,I have stored username and password in a session.Username and Password is same as the one with which I loginto the website (database1). The problem is I have tried session,but when ever I try to access the database I get "mysql unable to fetch data".My connection is failing for database 2. I have attached the two php pages.PHP page1 stores session value for username and password whereas,PHP page2 calls those session and tries to connect with database. Page1********** </SCRIPT> <% } /* %> </HEAD> <BODY> <%*/ globvars('username','password','do'); global $globvars; extract($globvars) ; //*********Test whether $_SESSION variable exists /*if(!isset($_SESSION['password'])){ //redirect out header("Location: index.php"); die(); // need this, or the rest of the script executes }*/ // SESSION DECLARATION $_SESSION['username'] = $username; $_SESSION['password'] = $password; $globvars['session_id'] = session_id(); /*PRINTING SESSION USERNAME AND PASSWORD print("Username ".$_SESSION['username']); print("Password ".$_SESSION['password']);*/ $globvars['session_id'] = session_id(); // get host if (isset($HTTP_HOST)){ $globvars['host']=$HTTP_HOST; } else { $globvars['host']=$_SERVER['HTTP_HOST']; } // get page name if (isset($PHP_SELF)){ $page=$PHP_SELF; } else { $page=$_SERVER['PHP_SELF']; } if(strrpos($page,'/')) { $page = substr( $page , strrpos($page,'/')+1 ); } $globvars['page']=$page; opendb(); function verify($filename) { global $globvars; extract($globvars) ; $stamp = date("Y-m-d H:i:s"); $globvars['login']['user'] = null ; $globvars['login']['email'] = null ; $globvars['login']['message'] = null ; $globvars['login']['types'] = null ; $globvars['login']['pages'] = null ; $globvars['login']['name'] = null ; if($filename) { $string = "SELECT * FROM `pages` WHERE `filename` = '$filename'"; $query= mysql_query("$string"); if(mysql_num_rows($query)) { $t_row = mysql_fetch_array($query); $utallow = $t_row['user_types']; if( ( $do == 'logout' ) && $session_id ) { $string = "UPDATE `users` SET `session` = '' WHERE `session` = '$session_id' LIMIT 1"; mysql_query("$string"); $globvars['login']['message'] = 'You are logged out'; } elseif($do=='login') { if($username && $password) { // check user $string = "SELECT * FROM `users` WHERE `username` = '$username' AND `password` = '$password'"; $check = mysql_query("$string"); if(mysql_num_rows($check)) { // user found $u_row = mysql_fetch_array($check); $string = "UPDATE `users` SET `session` = '$session_id', `lastlogin` = '$stamp' WHERE `username` = '$username' LIMIT 1"; mysql_query("$string"); setcookie("username", $username, time()+60*60*24*365); $globvars['login']['message'] = 'Welcome ' . $u_row['forename'] . ' ' . $u_row['surname'] . ', you are logged in.' ; $globvars['login']['user'] = $u_row['username'] ; $globvars['login']['name'] = $u_row['forename'] . ' ' . $u_row['surname'] ; $globvars['login']['email'] = $u_row['email'] ; // run cleanup cleanup(); // check user types getutype($utallow,$u_row['utypes']) ; } else { $globvars['login']['message'] = 'Error: Invalid username or password' ; } } else { $globvars['login']['message'] = 'Error: Enter username and password' ; } } elseif($session_id) { // check if logged in $string = "SELECT * FROM `users` WHERE `session` = '$session_id'"; $check = mysql_query("$string"); if(mysql_num_rows($check)==1) { // found already logged in $u_row = mysql_fetch_array($check); $globvars['login']['user'] = $u_row['username'] ; $globvars['login']['name'] = $u_row['forename'] . ' ' . $u_row['surname'] ; $globvars['login']['email'] = $u_row['email'] ; // check user types getutype($utallow,$u_row['utypes']) ; } } } } session_start(); //Destroying a session //session_destroy(); globvars('super_pass'); global $globvars; extract($globvars) ; $GLOBALS['db'] = 'test' ; $GLOBALS['link'] = '' ; //print $GLOBALS['link']; // USING SESSION if($super_pass) { if(( $_SESSION['username'] ) && ($_SESSION['password'] )) { print ("connection established "); // TRYING TO CONNECT TO THE DATABASE $GLOBALS['link'] = mysql_connect('localhost', $_SESSION['username'] , $_SESSION['password']) OR die("Error: Failed to open database"); //PRINTING SESSION USERNAME AND PASSWORD print($_SESSION['username']); print($_SESSION['password']); $dbsup = $_SESSION['username']; $globvars['dblog'] = $dbsup; //CONNECTION SUCCESSFUL OR NOT //mysql_select_db($GLOBALS['link'])OR die("Error: Failed to open database"); //mysql_select_db($GLOBALS['db'])OR die("Error: Failed to open database"); //print (mysql_select_db($GLOBALS['db']); } } //FUNCTION opendb()(in AGADMIN.inc) GIVES DATABASE ACCESS function opendb() { globvars('super_pass'); global $globvars; extract($globvars) ; //passing agsemi //$GLOBALS['db'] = $dbnam ; //passing host $cfgServer['host'] = $dbhst ; $GLOBALS['link'] = '' ; //if($super_pass) { /* ORIGNAL SUPERADMIN LOGIN $cfgServer['user'] = $dbsup ; $cfgServer['password'] = sp_code($super_pass,'d') ; $GLOBALS['link'] = mysql_connect( $cfgServer['host'] , $cfgServer['user'] , $cfgServer['password'] ); print($GLOBALS['link']); $globvars['dblog'] = $dbsup ;*/ //********* CHECK AFTER CONNECTING WITH THE DATABASE /* //calling session user $cfgServer['user'] = $_SESSION['username']; print("Username for this page is ".$cfgServer['user']); //calling session password $cfgServer['password'] = sp_code($_SESSION['password'],'d'); $cfgServer['password'] = $_SESSION['password']; print("Session Password is".$cfgServer['password']." for this page"); //Making a Connection $GLOBALS['link'] = mysql_connect( $cfgServer['host'] , $cfgServer['user'] , $cfgServer['password'] ); //$dbsup = $cfgServer['user']; $globvars['dblog'] = $dbsup ; //Check whether connection has been established //mysql_select_db($GLOBALS['db']) or die("Error: Failed to open database" ); */ //} if(! $GLOBALS['link']) { // use normal login $cfgServer['user'] = $dbusr ; $cfgServer['password'] = $dbpss ; $GLOBALS['link'] = mysql_connect( $cfgServer['host'] , $cfgServer['user'] , $cfgServer['password'] ); if (! $GLOBALS['link']) die("Error: Failed to open MySQL"); $globvars['dblog'] = $dbusr ; } mysql_select_db($GLOBALS['db']) or die("Error: Failed to open database" ); } Best Regards, Divya Link to comment https://forums.phpfreaks.com/topic/133344-mysql-connection-help/ Share on other sites More sharing options...
revraz Posted November 19, 2008 Share Posted November 19, 2008 Where is your session_start for the first page? Link to comment https://forums.phpfreaks.com/topic/133344-mysql-connection-help/#findComment-693521 Share on other sites More sharing options...
darkfreaks Posted November 19, 2008 Share Posted November 19, 2008 Missing session_start, missing several closing brackets , added mysql_error() </SCRIPT> <% } /* %> </HEAD> <BODY> <?php globvars('username','password','do'); global $globvars; extract($globvars) ; //*********Test whether $_SESSION variable exists /*if(!isset($_SESSION['password'])){ //redirect out header("Location: index.php"); die(); // need this, or the rest of the script executes }*/ // SESSION DECLARATION session_start(); $_SESSION['username'] = $username; $_SESSION['password'] = $password; $globvars['session_id'] = session_id(); /*PRINTING SESSION USERNAME AND PASSWORD print("Username ".$_SESSION['username']); print("Password ".$_SESSION['password']);*/ $globvars['session_id'] = session_id(); // get host if (isset($HTTP_HOST)){ $globvars['host']=$HTTP_HOST; } else { $globvars['host']=$_SERVER['HTTP_HOST']; } // get page name if (isset($PHP_SELF)){ $page=$PHP_SELF; } else { $page=$_SERVER['PHP_SELF']; } if(strrpos($page,'/')) { $page = substr( $page , strrpos($page,'/')+1 ); } $globvars['page']=$page; opendb(); function verify($filename) { global $globvars; extract($globvars) ; $stamp = date("Y-m-d H:i:s"); $globvars['login']['user'] = null ; $globvars['login']['email'] = null ; $globvars['login']['message'] = null ; $globvars['login']['types'] = null ; $globvars['login']['pages'] = null ; $globvars['login']['name'] = null ; if($filename) { $string = "SELECT * FROM `pages` WHERE `filename` = '$filename'"; $query= mysql_query("$string"); if(mysql_num_rows($query)) { $t_row = mysql_fetch_array($query); $utallow = $t_row['user_types']; if( ( $do == 'logout' ) && $session_id ) { $string = "UPDATE `users` SET `session` = '' WHERE `session` = '$session_id' LIMIT 1"; mysql_query($string) or die(mysql_error()); $globvars['login']['message'] = 'You are logged out'; } elseif($do=='login') { if($username && $password) { // check user $string = "SELECT * FROM `users` WHERE `username` = '$username' AND `password` = '$password'"; $check = mysql_query($string) or die(mysql_error());; if(mysql_num_rows($check)) { // user found $u_row = mysql_fetch_array($check); $string = "UPDATE `users` SET `session` = '$session_id', `lastlogin` = '$stamp' WHERE `username` = '$username' LIMIT 1"; mysql_query($string)or die(mysql_error());; setcookie("username", $username, time()+60*60*24*365); $globvars['login']['message'] = 'Welcome ' . $u_row['forename'] . ' ' . $u_row['surname'] . ', you are logged in.' ; $globvars['login']['user'] = $u_row['username'] ; $globvars['login']['name'] = $u_row['forename'] . ' ' . $u_row['surname'] ; $globvars['login']['email'] = $u_row['email'] ; // run cleanup cleanup(); // check user types getutype($utallow,$u_row['utypes']) ; } else { $globvars['login']['message'] = 'Error: Invalid username or password' ; } } else { $globvars['login']['message'] = 'Error: Enter username and password' ; } } elseif($session_id) { // check if logged in $string = "SELECT * FROM `users` WHERE `session` = '$session_id'"; $check = mysql_query($string) or die(mysql_error());; if(mysql_num_rows($check)==1) { // found already logged in $u_row = mysql_fetch_array($check); $globvars['login']['user'] = $u_row['username'] ; $globvars['login']['name'] = $u_row['forename'] . ' ' . $u_row['surname'] ; $globvars['login']['email'] = $u_row['email'] ; // check user types getutype($utallow,$u_row['utypes']) ; } } } } } ?> Link to comment https://forums.phpfreaks.com/topic/133344-mysql-connection-help/#findComment-693551 Share on other sites More sharing options...
divadiva Posted November 19, 2008 Author Share Posted November 19, 2008 Thanks for replying. I added the check" die(no connection) for session. But session works and the one thing that still doesnt work is mysql connect. Any comments/sugestion will be appreciated. Link to comment https://forums.phpfreaks.com/topic/133344-mysql-connection-help/#findComment-693607 Share on other sites More sharing options...
darkfreaks Posted November 19, 2008 Share Posted November 19, 2008 dont use session variables in the connect instead define them like <?php define(DB_USER, $cfgServer['user']); define(DB_HOST, $cfgServer['host']); define(DB_PASS, $cfgServer['pass']); $GLOBALS['link']= mysql_connect(DB_HOST,DB_USER,DB_PASS); ?> Link to comment https://forums.phpfreaks.com/topic/133344-mysql-connection-help/#findComment-693619 Share on other sites More sharing options...
darkfreaks Posted November 19, 2008 Share Posted November 19, 2008 <?php if (! $GLOBALS['link']) die('Error:'. mysql_error());?> Link to comment https://forums.phpfreaks.com/topic/133344-mysql-connection-help/#findComment-693646 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.