Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP SQL Database Connect 2

Status
Not open for further replies.

Enkrypted

Technical User
Sep 18, 2002
663
US
I have a website setup and I am having issues with it trying to make the connection to the database. I wanted to doublecheck to make sure my PHP coding is correct. Here is the underlying PHP code for the login page:

Code:
<?php
ob_start();
$host="mywebhost.com"; // Host name 
$username="myusername"; // Mysql username 
$password="mypassword"; // Mysql password 
$db_name="mydatabasename"; // Database name 
$tbl_name="mytablename"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// Define $myusername and $mypassword 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword"); 
header("location:/dptest/dpremote.html");
}
else {
echo "Wrong Username or Password";
}

ob_end_flush();
?>

I've doublechecked the login material at the top of that and it is all correct, however upon logging into the page i get the following error:


Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'krypt'@'gravity.dnsprotect.com' (using password: YES) in /home/krypticv/public_html/dptest/checklogin.php on line 10
cannot connect


Is there something I am not doing properly? Any help on this is appreciated. TIA!

Enkrypted
A+
 
This looks like a case where the mysql user 'krypt' is only allwed to connect from localhost. Check your mysql database and make sure that user 'krypt' has an '%' host entry or a host entry specifically stating 'gravity.dnsprotect.com'

--== Anything can go wrong. It's just a matter of how far wrong it will go till people think its right. ==--
 
well ... this is mysql telling you that your username is not permitted to access that database from the host gravity.dnsprotect.com.

remember that mysql permissions are granular. control is given to the global admin over which user can access which table/view and perform which functions from which host.

if this is a misconfiguration and you are the admin then just change the grant table. if you are not the admin then most probably you will have to contact the person that is. In either case this is not a php issue and follow ups would be better dealt with in the mysql forum.
 
I was able to correct the login issue however another issue has developed. When I put the wrong login information in, it successfully echoes the error it should. When I put the correct login information in, it goes right back to the login screen again. I've checked the config file and it should take it to the page I have set for success. The code is the same as above (I only reset up the database and everything worked fine again). The only change that was made to the code is the following:

header("location:/dptest/dpremote.html");

was changed to

header("location:/dptest/dpremote.php");

Enkrypted
A+
 
jpadie,

Thank you for helping assist me with this issue. I have page protection setup (the page i want to be viewed requires them to be logged in and if they are not, keeps taking them to the login screen) however, my only issue is that of after logging in going to the requested page. I have looked at that thread you posted in and have also tried using the code you have in there. I wasn't able to successfully make it work. I will give it another try, but also want to look at why it's not redirecting to the page it should.

Enkrypted
A+
 
there is no reason why the code you have posted should not create the redirects you intend. If you are seeing alternative behaviour then there is most probably something in another script or .htaccess file that causes this.

as an aside, you are using deprecated function calls which will quite probably break in a future version of php.
 
There are only 2 other pages that are involved. The following is the login page code:

Code:
<title>Dogg Pound Remote Server Management Login</title><body bgcolor="#000000">
<p align="center">&nbsp;</p>
<p align="center"><img src="logo-dp.gif" width="800" height="200"></p>
<p align="center"><font color="#FF0000" size="4" face="Verdana, Arial, Helvetica, sans-serif">Remote 
  Server Management Login Portal</font></p>
  <p>&nbsp;</p>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#000000">
  <tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="0" bgcolor="#FFFFFF">
          <tr bgcolor="#000000"> 
            <td colspan="3"><strong><font color="#FFFFFF">Member Login </font></strong></td>
</tr>
<tr>
            <td width="78" bgcolor="#000000"><font color="#FFFFFF">Username</font></td>
            <td width="6" bgcolor="#000000">:</td>
            <td width="294" bgcolor="#000000">
<input name="myusername" type="text" id="myusername">
            </td>
</tr>
<tr>
            <td bgcolor="#000000"><font color="#FFFFFF">Password</font></td>
            <td bgcolor="#000000">:</td>
            <td bgcolor="#000000"> 
              <input name="mypassword" type="password" id="mypassword">
            </td>
</tr>
<tr>
            <td bgcolor="#000000">&nbsp;</td>
            <td bgcolor="#000000">&nbsp;</td>
            <td bgcolor="#000000">
<input type="submit" name="Submit" value="Login">
            </td>
</tr>
</table>
</td>
</form>
</tr>
</table>

The other page is the landing page. Here is the code for that page:

Code:
<?php
  session_start();

  if (!isset($_SESSION['is_logged_in'])) {
    header("Location:dpremotelogin.php");
    die();
    }
?>
<html>
<head>
<title>Dogg Pound Remote Server Management</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#000000">
<div align="center">
  <p>&nbsp;</p>
  <p><img src="logo-dp.gif" width="800" height="200"></p>
  <p><font color="#FF0000" size="4" face="Verdana, Arial, Helvetica, sans-serif">Remote 
    Server Management</font></p>
  <p>&nbsp;</p>
  <form name="form1" method="post" action="construction.php">
    <p><font color="#FFFFFF" face="Verdana, Arial, Helvetica, sans-serif">Click here to stop the server</font> </p>
    <p>
      <input type="submit" name="Submit" value="Stop Server">
    </p>
  </form>
  <form name="form2" method="post" action="construction.php">
    <p><font color="#FFFFFF" face="Verdana, Arial, Helvetica, sans-serif">Click here to start the server</font></p>
    <p> 
      <input type="submit" name="Submit" value="Start Server">
    </p>
  </form>
  <form name="form3" method="post" action="construction.php">
    <p><font color="#FFFFFF" face="Verdana, Arial, Helvetica, sans-serif">Click here to restart the server</font> </p>
    <p>
      <input type="submit" name="Submit" value="Restart Server">
    </p>
    <p>&nbsp; </p>
  </form>
  <form name="form4" enctype="multipart/form-data" method="post" action="construction.php">
    <p><font color="#FFFFFF" face="Verdana, Arial, Helvetica, sans-serif">Click Browse to upload maps to the server</font></p>
    <p>
      <input type="file" name="file">
    </p>
    <p>
      <input type="submit" name="Submit2" value="Upload Map">
    </p>
  </form>
  <p>&nbsp;</p>
</div>
</body>
</html>

Enkrypted
A+
 
In your landing page you check to see whether a session variable named "is_logged_in" is not set, however at no time in any of your other pages do I see you actually setting the variable. Meaning the second the user arrives to the page it gets sent back to to the dpremotelogin.php page because that particular variable is not set.

Perhaps you intended to set that variable when you determine the login is correct.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks for pointing that out vacunita. I am still learning how to do this stuff so any help....well...helps...lol. That took care of the problem. Thanks guys!

Enkrypted
A+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top