简体   繁体   English

PHP / MySQL登录系统-

[英]PHP/MySQL log in system -

I'm pretty new to both PHP and MySQL and I'm struggling to get my login system to function properly. 我对PHP和MySQL还是很陌生,我正努力使我的登录系统正常运行。 The registration works fine, but when I run the login it doesn't recognise there is anything within the table matching the entered data. 注册工作正常,但是当我运行登录名时,它无法识别表中与输入数据匹配的任何内容。 Below is the code I believe to be the problem area. 以下是我认为是问题区域的代码。

Thanks in advance. 提前致谢。

<?php

function load($page = 'login.php')
{
$url = 'http://'.$_SERVER['HTTP_HOST'].
dirname($_SERVER['PHP_SELF']);
$url = rtrim($url,'/\/');
$url.= '/'.$page;

header("location:$url");
exit();
} 
function validate($dbc,$email ='',$pwd='')
{
$errors = array();
if (empty($email))
    { $errors[] = 'Enter your email address.'; }
else
    {  $e = mysqli_real_escape_string($dbc,trim($email));}
if (empty($pwd))
    { $errors[] = 'Enter your password.';}
else
    { $p = mysqli_real_escape_string($dbc, trim($pwd)); }
if (empty($errors))
{
    $q = "SELECT adultID, FirstName, Surname "
            . "FROM adult_information "
            . "WHERE Email = '$e' AND Password = SHA1('$p')";
    $r = mysqli_query($dbc, $q);

    if (mysqli_num_rows($r) == 1)
    { $row = mysqli_fetch_array($r, MYSQLI_ASSOC);
        return array( true, $row);}
    else
    {$errors[]='Email address and password not found.';}
}
return array(false,$errors);
}

I believe that you'll get what you're looking for if you change 我相信,如果您进行更改,您将得到想要的东西

$q = "SELECT adultID, FirstName, Surname "
        . "FROM adult_information "
        . "WHERE Email = '$e' AND Password = SHA1('$p')";

to

$p = SHA1($p);

$q = "SELECT adultID, FirstName, Surname "
        . "FROM adult_information "
        . "WHERE Email = '$e' AND Password = '$p'";

Whenever a PHP-to-MySQL query isn't performing as expected, my first step is to get a look at the SQL I'm actually passing to the database. 每当从PHP到MySQL的查询未按预期执行时,我的第一步就是查看我实际传递给数据库的SQL。 In this case, it would be by inserting a line like echo '<p>$q</p>'; 在这种情况下,可以通过插入类似echo '<p>$q</p>'; immediately after assigning the value of $q. 分配$ q的值后立即。

Sometimes it immediately becomes obvious that I've got a malformed query just by looking at it. 有时候,仅通过查看就可以立即发现我有格式错误的查询。 If it doesn't, I copy the SQL code that appears and run it as a query within the database manager, to see what errors it throws and/or examine the resulting data. 如果没有,我复制出现的SQL代码并在数据库管理器中作为查询运行它,以查看它引发了什么错误和/或检查结果数据。

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

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