1

我正在尝试验证我的 php/sql 网页的登录名。

第一个代码显示了 Login.php 的一部分,我在其中获取了两个文本字段、电子邮件和密码,并将它们传递给 authenticate.php

第二个代码显示了我在哪里获取这两个值并尝试处理它们。

我遇到的问题是我每次都被定向到 index.php,即使我在该字段中输入了正确的数据。

任何帮助,将不胜感激。

Login.php 的一部分

        <td width="70">Email</td>
        <td width="6" align="center">:</td>
<form action="authenticate.php" method="post" name="authenticate_form">
        <td><input name="email" type="text" id="email"></td>
      </tr>
      <tr>
        <td width="70">Password</td>
        <td width="6" align="center">:</td>
        <td><input name="password" type="text" id="password"></td>
      </tr>
      <tr>
        <td width="70">Login</td>
        <td width="6" align="center">:</td>
        <td>
            <input type="submit" name="submit" value="Login" />
</form>
        </td>

验证.php

// ----------------------
// Retrieve login information

include("db_info.php");

// ----------------------

$conn = oci_connect($db_user, $db_pwd, '(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(Host=asuka.cs.ndsu.nodak.edu)(Port=1521)))(CONNECT_DATA=(SID=asuka)))');

if (!$conn) {
    $e = oci_error();
    print_r($e);
    exit();
}

// ----------------------
// Get POST values

if(isset($_POST['email']) && $_POST['email'] && isset($_POST['password']) && $_POST['password']) {

     // Get posted form information and strip out unsafe characters
     $email = htmlspecialchars(stripslashes($_POST['email']));
 $password = htmlspecialchars(stripslashes($_POST['password']));
} else {
    // Illegal access.
    // Redirect back to index.php
    header("location: index3.php");
    exit();
}

// ----------------------
// Authenticate User

// Create query
$sql = "SELECT PASSWORD FROM CUSTOMER WHERE EMAIL = '$email'";

 // Create database query statement
$statement_id = oci_parse($conn, $sql);

// Execute query statement
$result = oci_execute($statement_id, OCI_COMMIT_ON_SUCCESS);
$queryResult = oci_fetch_row($statement_id);

//var_dump($queryResult);
// Check for successful authentication
if($password == $queryResult[0]) {

if ($email=="admin@hotmail.com") {
    $db_login_status = 2;
    header("location: admin.php");
} else {
    $db_login_status = 1;
    header("location: normal.php");
}
} else {
    header("location: fail.php");
}

// ----------------------
// Close connections

oci_free_statement($statement_id);
oci_close($conn);
4

3 回答 3

3

$result永远不会分配,因此它永远不会等于1. 解决方法是:

$result = oci_execute($statement_id, OCI_COMMIT_ON_SUCCESS);
// Check for successful authentication
if ($result) {

$result将是布尔值,因此您无需将其与1.

$result返回时有false一个函数可以查看出了什么问题。这个函数是oci_error()。您应该打印(或记录)错误,而不是重定向。这将帮助您进行调试。

问题可能是您的 SQL 查询中的参数没有引号。

于 2012-04-29T22:42:39.070 回答
2
if ($email=="admin@hotmail.com") {
    $db_login_status = 2;
    header("location: index1.php");
} else {
    $db_login_status = 1;
    header("location: index.php");
}

如果您被发送到 index.php,那一定意味着您没有使用“admin@hotmail.com”登录。尝试使用该电子邮件地址。否则,请尝试删除上面的代码并留下这两行:

    $db_login_status = 1;
    header("location: index.php");

问题是即使您输入了正确的登录名,您也不​​会被重定向,因为您这样做了,您会被重定向。如果您不想在登录时被重定向,则必须更改脚本以执行您想要的任何操作。

编辑:根据您的评论,它似乎$email是空的。那是因为<input name="email">您的表单中的 是一个隐藏的输入,当您输入密码时没有填充任何内容。我假设您有一个 javascript,它从其他表格单元格中的可见文本输入中导入值。你?否则就是你的问题。您的<form>标签实际上需要包含登录数据输入的输入。您可以通过将表单包裹在整个登录表周围来解决问题。

除此之外,您$result始终为 1,因为查询成功,而不是因为它包含结果。之后,您还需要执行$row = oci_fetch_row($statement_id);. 然后检查if($row)而不是if($statement_id). 或者干脆if(oci_fetch_row($statement_id))

附带说明一下,结果是另一个问题:不要忘记在外部编辑器上提交您对数据库所做的编辑。如果它们没有提交,其他查询将看不到它们。在这种情况下,'admin@hotmail.com' 的记录被添加到外部程序中并且没有提交 - 所以 PHP 拒绝确认登录。

于 2012-04-29T22:43:10.090 回答
2

if ($result==1) {}

$result 从未在代码中分配!

于 2012-04-29T22:49:16.283 回答