繁体   English   中英

php登录身份验证失败

[英]php login authentication failing

我是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;
}
?>

我知道注册正在写入数据库,但是每次我尝试进行有效登录时,都会收到无效的凭据标志。 您能为我做的任何事情都会很棒。 谢谢。

首先,您应该清除一些事项:

1。

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

它应该是&&而不是|。

  1. 在您的代码中,没有地方可以检查用户名是否存在,因此我猜测您有多个具有相同用户名的用户。 在插入checkreg.php之前,您应该首先检查用户名是否存在。

  2. 在您的checklogin.php中将if($ count == 1)更改为if($ count> 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起已取消此功能。

可以尝试的几件事:

1)不建议使用session_register函数调用。 http://php.net/manual/zh/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/zh/function.session-start.php 。)

4)我知道这是示例代码,但是可能会发生SQL注入攻击。 需要转义那些变量。

$ sql =“ SELECT id,用户名,密码,来自用户的用户名='$ username',密码='$ password'”;

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

    session_start();

checklogin.php的顶部,然后使用

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

代替session_register()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM