我有以下PHP代码...(用+++切换敏感数据)
<?php
$host="triplestrata.com"; // Host name
$username="+++"; // Mysql username
$password="+++"; // Mysql password
$db_name="+++"; // Database name
$tbl_name="++_+++"; // Table name
// Connect to server and select databse.
mysql_connect("triplestrata.com", "+++", "+++")or die("cannot connect");
mysql_select_db("+++")or die("cannot select DB");
// username and password sent from form
$user_login=$_POST['user_login'];
$user_pass=$_POST['user_pass'];
// To protect MySQL injection (more detail about MySQL injection)
$user_login= stripslashes($user_login);
$user_pass= stripslashes($user_pass);
$user_login= mysql_real_escape_string($user_login);
$user_pass= mysql_real_escape_string($user_pass);
$sql="SELECT * FROM $tbl_name WHERE username='$user_login' and password='$user_pass'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $user_login and $user_pass, table row must be 1 row
if($count==1){
// Register $user_login, $user_pass and redirect to file "login_success.php"
session_register("user_login ");
session_register("user_pass");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
我知道这听起来像一个非常基本的问题,但我在哪里输入我的Db数据?我试过把它放在开头,但它不起作用。我收到连接错误。
那么我应该如何,在放置用户名和Db名称时,我应该包含还是忽略前缀? IE是一个名为&#39; mybase&#39;和用户名&#39; user1&#39;:
会不会是
$host="localhost"; // Host name
$username="user1"; // Mysql username
$password="password"; // Mysql password
$db_name="mybase"; // Database name
$tbl_name="members"; // Table name
我收到此错误:
警告:mysql_connect()[function.mysql-connect]:拒绝访问用户&#39;用户&#39; cpanel.123.45.67.89.webhost.com&#39; (使用密码:YES)在第11行的/home/user/public_html/checklogin.php中 无法连接
但是我改变了一切以匹配Db表...想法?感谢
答案 0 :(得分:0)
您已将数据库连接信息分配给变量(例如$ host,$ username等)。连接时,不应该在这些变量周围加上引号。而不是传递&#34; localhost&#34;作为你传递的主人&#34; $ host&#34;。试试这个:
mysql_connect($host, $username, $password)or die("cannot connect");
mysql_select_db($db_name)or die("cannot select DB");
如果这不起作用,请包含您正在接收的连接错误。
我不确定这是否与您的错误有关,但可能会有所帮助。 (确保用户名和密码格式正确)
答案 1 :(得分:0)
删除这些行上变量的引号:
mysql_connect("$host", "$username", "$password");
mysql_select_db("$db_name")or die("cannot select DB");
制作这些:
mysql_connect($host, $username, $password);
mysql_select_db($db_name)or die("cannot select DB");
请注意:
使用预准备语句和参数化查询。这些是由数据库服务器与任何参数分开发送和解析的SQL语句。这样攻击者就无法注入恶意SQL。