Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 172
  • Last Modified:

Question to locate the problem

Hi,
How to check/validate where it is abnormally behaving below?
function login($username, $password)
// check username and password with db
// if yes, return true
// else return false
{
  // connect to db
  $conn = df_conn();
  if (!$conn)
    return 0;

$sql="select top 10000 * from user_acct where username='$username' and passwd = '$password'";
$rst = odbc_exec($conn,$sql);
$num_rows = odbc_num_rows($rst); 

 if (!$rst)
     return 0;
  
  if ($num_rows>0)
     return 1;
  else 
     return 0; 
}

Open in new window

I do not know why sometimes it is fine to access but sometimes not, using the same correct user name and password?
0
HuaMinChen
Asked:
HuaMinChen
  • 8
  • 5
  • 5
4 Solutions
 
Terry WoodsIT GuruCommented:
Is it a live site or a dev site? If live, then if the database is too busy then it might be failing to run the query.

Can you replicate the problem yourself, even if it doesn't occur every time?

Are you getting an error message? If not, then you need to be adding code so that errors are detected and either displayed (if a dev site) or logged.
0
 
HuaMinChenBusiness AnalystAuthor Commented:
Within function in above, how to show any relevant meaningful error (that can be from DATABASE)?
0
 
Julian HansenCommented:
Firstly, I am fascinated by this
select top 10000 * from 

Open in new window

Why top 10000 - surely your user / pass forms a unique key?

Secondly, when you say it does not work - can you be a bit more specific.
0
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
HuaMinChenBusiness AnalystAuthor Commented:
Hi,
Julian,
It does go to 'else' part below, even if I've put correct user name and password. (I also already adjusted function below)
if ($username && $passwd)
// they have just tried logging in
{
    if (login($username, $passwd))
    {
      // if they are in the database register the user id
      $valid_user = $username;
      session_register("valid_user");
	  $conn = df_conn();
	  $sql="select top 1 principal from user_acct where username='$username'";
	  $rst = odbc_exec($conn,$sql);
	  $qry = odbc_fetch_array($rst);
	  $pri=$qry[principal];
      session_register("pri");
	  $sql="select top 1 territory from user_acct where username='$username'";
	  $rst = odbc_exec($conn,$sql);
	  $qry = odbc_fetch_array($rst);
	  $g_area=$qry[territory];
	  session_register("g_area");
	  $g_multiarea = str_replace(",", chr(13), $g_area);
	  session_register("g_multiarea");
    }  
    else
    {
      // unsuccessful login
      do_html_header("Problem:");
      echo "You could not be logged in. 
            You must be logged in to view this page.";
      do_html_url("login.php", "Login");
      do_html_footer();
      exit;
    }      
}

function login($username, $password)
// check username and password with db
// if yes, return true
// else return false
{
  // connect to db
  $conn = df_conn();
  if (!$conn)
    return 0;

//$sql="select top 10000 * from user_acct where username='$username' and passwd = '$password'";
$sql="select top 1 * from user_acct where username='$username' and passwd = '$password'";
$rst = odbc_exec($conn,$sql);
$num_rows = odbc_num_rows($rst); 

 if (!$rst)
     return 0;
  
  if ($num_rows>0)
     return 1;
  else 
     return 0; 
}

Open in new window

Within function, how to detect any DATABASE problem if exists?
0
 
Terry WoodsIT GuruCommented:
@Julian, note that I've already mentioned in a previous question the need for sanitising the database inputs... :-)
0
 
Terry WoodsIT GuruCommented:
Try this change to your code:
$rst = odbc_exec($conn,$sql);
if (!$rst) { die(odbc_errormsg()); }

Open in new window


If you'd rather it didn't die, you can output it to a log file, with something like this. You may want to change the file/folder name of course, especially if you're running it on Windows:
$rst = odbc_exec($conn,$sql);
if (!$rst) { file_put_contents('/tmp/phplogfile.txt', odbc_errormsg(), FILE_APPEND); }

Open in new window

0
 
Julian HansenCommented:
@Terry - I was not referring to sanitizing - just why he was limiting his results to only the first 10000

@HuaMinChen - can you guarantee that you are putting in correct name and password. Have you tried logging all login requests so you can see what happens on a fail.
0
 
HuaMinChenBusiness AnalystAuthor Commented:
Terry,
Here is the error message I've got

[Microsoft][ODBC SQL Server Driver]Communication link failure

Yes, Julian. User name and password are definitely fine.
0
 
Terry WoodsIT GuruCommented:
@Julian, I know... but I figured that would probably be the next logical thing for you to comment on, so I thought I'd save you the trouble!
0
 
Terry WoodsIT GuruCommented:
That error fits with the sporadic behaviour. A failure to connect to the database can be sporadic. Do you have any idea why that might happen? Is the database server very busy, or remote?
0
 
Julian HansenCommented:
No, I don't think it is sanitising - my bet is on a database connection issue. If it were sanitizing it should fail all the time - as he says it happens on the same name / password combination.
0
 
Terry WoodsIT GuruCommented:
Assuming that the database is running SQL Server (on a Windows server), I think this question would be best reposted in specific Windows Topic areas. If you look at some of the solutions suggested for resolving the error here, you can see it might require someone with specific WIndows server experience: https://support.microsoft.com/en-nz/kb/942861
0
 
HuaMinChenBusiness AnalystAuthor Commented:
A failure to connect to the database can be sporadic. Do you have any idea why that might happen? Is the database server very busy, or remote?

It is using System DSN to connect to SQL server database, from the current Web server.
0
 
Julian HansenCommented:
[Microsoft][ODBC SQL Server Driver]Communication link failure
Well there you have it - db link is unstable. So either your network is faulty or your database server has an issue. Where is the database server located - is it on the same machine or are you accessing it over the network?
0
 
Terry WoodsIT GuruCommented:
I think you can close this question (and open a new one as suggested above); the request was to locate the problem, and it has been located as being elsewhere! :-)
0
 
HuaMinChenBusiness AnalystAuthor Commented:
Where is the database server located - is it on the same machine or are you accessing it over the network?

It connects to DATABASE through System DSN, while Web server and Database are on different servers.
0
 
Julian HansenCommented:
Then I second Terry's comment above - this is an infrastructure problem which falls outside the scope of this thread.
You have a problem with either your network connection to your DB Server or the Server itself.  You will need to get in touch with Experts who specialise on those areas.
0
 
Terry WoodsIT GuruCommented:
Note though, as a final comment on this question, that you can get an error code in addition to the message... this should hopefully worK:
if (!$rst) { die(odbc_error().": ".odbc_errormsg()); }

Open in new window

The code may help in tracking down additional information on the problem, eg with a Google search.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 8
  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now