Jump to content

*Solved* Selecting a database at login


swatisonee

Recommended Posts

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'>  &nbsp;Username  &nbsp; &nbsp;
</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'>  &nbsp;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

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.
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_blah

Then you can still keep the same header file you already have, and make the drop down box select the table name instead?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.