php登录验证失败

时间:2013-02-27 19:06:11

标签: php mysql login

我是php的新手,我正在尝试编写一个非常简单的登录脚本。我已经掌握了基本功能,但我无法登录系统。我的登录脚本如下,我的注册脚本也在下面。

checklogin.php

include_once 'inc/db.inc.php';

$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string(md5($_POST['password']));


try {
$sql="SELECT id, username, password FROM users WHERE username='$username' and password='$password'";
$result = $pdo->query($sql);
$count=mysql_num_rows($result);
// If result matched $username and $password, table row must be 1 row
if($count == 1){

    // Register $username, $password and redirect to file "index.php"
     session_register("username");
     session_register("password"); 
     header("Location: index.php");
 }
 else {
 header("Location: login.php?invalid=1");
 }
}

catch (PDOException $e) {
    echo $e;
}

ob_end_flush();

?>

checkreg.php

include_once 'inc/db.inc.php';

 //This makes sure they did not leave any fields blank
 if (!$_POST['username'] | !$_POST['password'] | !$_POST['passwordconf'] ) {
    die('You did not complete all of the required fields');
}

if ($_POST['password'] != $_POST['passwordconf']) {
    die('Your passwords did not match. ');
}

$_POST['password'] = md5($_POST['password']);
if (!get_magic_quotes_gpc()) {
    $_POST['password'] = addslashes($_POST['password']);
    $_POST['username'] = addslashes($_POST['username']);
        }
$username = $_POST['username'];
$password = $_POST['password'];

 try {
 // now we insert it into the database
$sql = "INSERT INTO users(username,password) VALUES ('$username','$password')";
$result = $pdo->exec($sql);
header("Location: index.php");

} catch (PDOException $e){
 echo $e;
}
?>

我知道注册正在写入数据库,但每次尝试有效登录时,我都会收到无效的凭据标志。你能做些什么来帮助我会很棒。谢谢。

4 个答案:

答案 0 :(得分:0)

首先你应该清楚一些事情:

1

if (!$_POST['username'] | !$_POST['password'] | !$_POST['passwordconf'] ) {
die('You did not complete all of the required fields');}

它应该是&&而不是|。

  1. 在你的代码中无处可查看用户名是否存在,所以我猜你有多个用户使用相同的用户名。在插入checkreg.php之前,您应该先检查用户名是否存在。

  2. 在checklogin.php中
  3. 将if($ count == 1)更改为if($ count> 0)

  4. 如果这没有帮助,您可以向我提供更多信息,如数据库数据(现在数据库中的数据)

答案 1 :(得分:0)

<?php
// Use of session_register() is deprecated
$barney = "A big purple dinosaur.";
session_register("barney");

// Use of $_SESSION is preferred, as of PHP 4.1.0
$_SESSION["zim"] = "An invader from another planet.";

// The old way was to use $HTTP_SESSION_VARS
$HTTP_SESSION_VARS["spongebob"] = "He's got square pants.";
?>

警告:

自PHP 5.3.0开始,此函数已被弃用,自PHP 5.4.0起已被删除。

答案 2 :(得分:0)

要尝试的事情:

1)不推荐使用session_register函数调用。 http://php.net/manual/en/function.session-register.php。如果可能的话,你真的应该使用$ _SESSION。

这样的事情:

 $_SESSION["username"] = $username;
 $_SESSION["password"] = "validated"; // md5 is variable so you can't easily check for it on next page render

2)在测试值时,你不想要$ _POST,你想在下一页渲染上再次使用$ _SESSION。像这样:

if ("validated" == $_SESSION["password"]) {
    // You're logged in...
}

3)注意,要填充$ _SESSION数组,必须调用session_start();使用前一次且仅一次。 (http://www.php.net/manual/en/function.session-start.php了解更多。)

4)我知道这是示例代码,但SQL注入攻击是可能的。需要逃避这些变量。

$ sql =“SELECT id,username,password FROM users WHERE username ='$ username'和password ='$ password'”;

答案 3 :(得分:0)

您的问题可能是 session_register(),具体取决于您使用的PHP版本。试试

    session_start();

checklogin.php 的顶部,然后使用

    ...
    $_SESSION['username'] = $username;
    $_SESSION['password'] = $password;
    ...

而不是 session_register()