Php简单登录脚本白页

时间:2012-05-26 00:01:41

标签: php mysql sql

你好我试图将PDO植入我的登录脚本,使其成为sql注入的安装程序。但我得到一个白页我认为它是因为我试图计算行数,看看用户是否真实.....

// Here we inculde the function page
include 'functions/functions.php';
// Here we connect to the db
$db = mysqlconnect();

$password = md5($_POST['mypassword']);
$statement = $db->prepare("SELECT * FROM users WHERE username = ? AND password = ?");
$statement->execute(array($_POST['myusername'], $password));


// Replace counting function based on database you are using.
 $count = $statement->rowCount();
// 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['username'] = $myusername ;


//Test if it is a shared client
if (!empty($_SERVER['HTTP_CLIENT_IP'])){
  $ip=$_SERVER['HTTP_CLIENT_IP'];
//Is it a proxy address
}elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
  $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
  $ip=$_SERVER['REMOTE_ADDR'];
}


$updateinfo=mysql_query("UPDATE `users` SET lastip ='$ip' WHERE `username` = '".$_SESSION['username']."'");
mysql_query("INSERT INTO user_log 
(username, ip) VALUES('".$_SESSION['username']."', '$ip' ) ") 
or die(mysql_error());  





header("Location: home.php");
} else {
  echo "Wrong Username or Password";
}


echo"<p> </p>";

我没有得到任何错误只是一个白页。

此处还有我的功能页面,其中包括

    function mysqlconnect(){
     global $db;
    $host = 'localhost';
    $port = 3306; // This is the default port for MySQL
    $database = '';
    $username = '';
    $password = '';

    // Construct the DSN, or "Data Source Name".  Really, it's just a fancy name
    // for a string that says what type of server we're connecting to, and how
    // to connect to it.  As long as the above is filled out, this line is all
    // you need :)
    $dsn = "mysql:host=$host;port=$port;dbname=$database";

    // Connect!
    $db = new PDO($dsn, $username, $password);

}

1 个答案:

答案 0 :(得分:1)

你的代码中有几件事情会引起人们的注意。

如果您在此处粘贴了整个脚本,则会遗漏session_start()。我不知道你的home.php中有什么,但如果它的内容生成依赖于$ _SESSION ['username']中的值,那么它永远不会发生,因为它在标题重定向后会为空。

查看有关session_start()的手册。

另外,如上所述:

  

对于大多数数据库,PDOStatement :: rowCount()不返回受SELECT语句影响的行数。

以防万一。我过去花了很多时间自己想知道这件事。

您可能希望看一下关于rowCount的手册中的示例#2。

当然,正如@Paul已指出的那样,如果迁移到PDO,则不应再使用mysql_query()

相关问题