swatisonee Posted December 6, 2006 Share Posted December 6, 2006 Hi,I'm not sure if this is possible but heres my query anyhow.I have 3 databases hosted on the same domain,server. I have common users for all 3 but since they relate to three different lines, i need to keep their data separate. Is there a way I could modify the following login script so that when the user clicks on the Login button on the homepage, he is first asked to select a database ( A, B, C) and then he logs in to that db and the rest of the login script works as usual.[code]<?include "include/session.php";include "include/z_db.php"; // this contains db name, pw, user details?><!doctype html public "-//w3c//dtd html 3.2//en"><html><head><title>:Login :</title><meta name="GENERATOR" content="Arachnophilia 4.0"><meta name="FORMATTER" content="Arachnophilia 4.0"></head><body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000"><form action='loginck.php' method=post><table border='0' cellspacing='0' cellpadding='0' align=center> <tr id='cat'> <tr> <td bgcolor='#f1f1f1' ><font face='verdana, arial, helvetica' size='2' align='center'> Username </font></td> <td bgcolor='#f1f1f1' align='center'><font face='verdana, arial, helvetica' size='2' ><input type ='text' class='bginput' name='username' ></font></td></tr><tr> <td bgcolor='#ffffff' ><font face='verdana, arial, helvetica' size='2' align='center'> Password </font></td> <td bgcolor='#ffffff' align='center'><font face='verdana, arial, helvetica' size='2' ><input type ='password' class='bginput' name='password' ></font></td></tr><tr> <td bgcolor='#f1f1f1' colspan='2' align='center'><font face='verdana, arial, helvetica' size='2' align='center'> <input type='submit' value='Submit'> <input type='reset' value='Reset'></font></td> </tr></table></center></form></body></html>[/code]LOGINCK.PHP==========[code]<?include "include/session.php";include "include/z_db.php";$username=$_POST['username'];$password=$_POST['password'];$ip=$_SERVER['REMOTE_ADDR'];$tm = date("Y-m-d H:i:s");?><!doctype html public "-//w3c//dtd html 3.2//en"><html><head><title>:Login :</title><meta name="GENERATOR" content="Arachnophilia 4.0"><meta name="FORMATTER" content="Arachnophilia 4.0"></head><body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000"><?$username=mysql_real_escape_string($username);$password=mysql_real_escape_string($password);$sql = "SELECT * FROM Users WHERE Username='$username' AND Password = '$password'";$result = mysql_query($sql);if ($myrow = mysql_fetch_array($result)){ include "include/newsession.php";$userid = $myrow["Userid"];$result2=mysql_query("insert into Log (Sessionid,Userid,IP,Date) values('$_SESSION[id]','$userid','$ip','$tm')");echo mysql_error(); echo "<p class=data> <center><font face=Tahoma size=2>Thank you for logging in "; echo $username ; } else { session_unset();echo "<font face='Verdana' size='2' color=red>Wrong Login. Use your correct Username and Password and Retry <br><center><input type='button' value='Retry' onClick='history.go(-1)'></center>"; } echo "<center> "; if (mysql_result($result,0,"Type") == '2') { $Username=mysql_result($result,0,"Username"); printf("<p><font face=\"Tahoma\" size=\"2\"><a href=adminhead.php?Userid=$userid>Click here to proceed to your options</a></font>"); }?></body></html>[/code] Link to comment https://forums.phpfreaks.com/topic/29628-solved-selecting-a-database-at-login/ Share on other sites More sharing options...
artacus Posted December 6, 2006 Share Posted December 6, 2006 Sure add a select box on your login page where the user can select the database. Then in the page it is submitted to select the appropriate db using mysql_select_db(); Link to comment https://forums.phpfreaks.com/topic/29628-solved-selecting-a-database-at-login/#findComment-136010 Share on other sites More sharing options...
swatisonee Posted December 6, 2006 Author Share Posted December 6, 2006 ok i got the first part. i put in a select box and then i amended loginck.php as under :[code]<?include "include/session.php";include "include/z_db.php";$username=$_POST['username'];$password=$_POST['password'];$ip=$_SERVER['REMOTE_ADDR'];$tm = date("Y-m-d H:i:s");$dbtype = $_POST['dbtype'];?>[/code]But the header file include "include/z_db.php"; contains db info so it will mean i cannot have the header appearing on the sheet. Also, how do i have this selection of dbname reflected in the include file that is as under :[code]<?error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR);$dbservertype='mysql';$servername='localhost';$dbusername='abcd;$dbpassword='abcd';$dbname='abcd';connecttodb($servername,$dbname,$dbusername,$dbpassword);function connecttodb($servername,$dbname,$dbuser,$dbpassword){global $link;$link=mysql_connect ("$servername","$dbuser","$dbpassword");if(!$link){die("Could not connect to MySQL");}mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());}?>[/code]Thanks. Link to comment https://forums.phpfreaks.com/topic/29628-solved-selecting-a-database-at-login/#findComment-136027 Share on other sites More sharing options...
Switch0r Posted December 6, 2006 Share Posted December 6, 2006 Would it not be simpler to have all the tables you need in the same db, with a prefix on the table name?eg. info1_blah, info2_blah, info3_blahThen you can still keep the same header file you already have, and make the drop down box select the table name instead? Link to comment https://forums.phpfreaks.com/topic/29628-solved-selecting-a-database-at-login/#findComment-136338 Share on other sites More sharing options...
swatisonee Posted December 14, 2006 Author Share Posted December 14, 2006 No i need to have them separate right from day 1 so if 1 of the dbs move then i wont get grief trying to amend/copy/modify all the php files for them. anyway got it working with a large dollop of help ! Link to comment https://forums.phpfreaks.com/topic/29628-solved-selecting-a-database-at-login/#findComment-141225 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.