2012-05-26 89 views
0

你好我试图植入PDO到我的登录脚本,使其从SQL注入安全。但即时得到一个白色的页面,我认为它是因为我尝试计算行数,看看用户是否真正.....Php简单登录脚本白页

// 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

停止使用'global'关键字。你用'md5()'做了什么?你确定你已经启用了错误报告。 – PeeHaa

+0

是错误报告已打开。并返回$ db;显示没有数据库选择(当int他功能文件有)而我使用MD5,因为用户密码是MD5 ... – user1405062

+1

你仍然在使用'mysql_query'。你想达到什么目的?我看到你正在使用PDO连接到数据库,但是然后你使用'mysql_query'之外的 –

回答

1

有没有在你的代码夫妇的事情,在眼球枝。

如果你在这里粘贴了整个脚本,那么你错过了session_start()。我不知道你的home.php里有什么,但是如果它的内容生成依赖于$ _SESSION ['username']中的一个值,它永远不会发生,因为在头重定向之后它将是空的。

查看关于session_start()的手册。

此外,作为中指出:

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

以防万一想到这一点。过去我花了很多时间在自己想着这件事上。

您可能想看一下关于rowCount的手册上的示例#2。

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

+0

服务器有session_start所有准备好我把它放到页面上,然后得到100的错误的说法标题都准备好发送... – user1405062

+0

如果你对已经发送的标题感到唠叨,它发生在这一行: 'header(“Location:home.php”);'你必须在该行之前的某处输出到浏览器。 – budwiser