0

你好,我是 php 的新手,所以不要抨击我,但我就是不知道这段代码有什么问题。所以基本上我有几种形式的输出,但是一旦我用 mysql 做任何事情(甚至只是连接和断开连接!),它就不允许我做任何类型的输出。它也不允许我重定向。

我尝试在 mysql 连接和断开代码之间取出所有代码,但它无助于解决任何问题,但是,一旦我注释掉 mysql 连接代码,我所有的输出和重定向都会工作!我正在尝试构建一个简单的登录脚本,该脚本可以从其他地方的表单中获取电子邮件和密码。我很想解决这个问题,这样我就可以弄清楚其余部分是否有效。而且我知道“标题”之后将不起作用echo;只要我能确保它正常工作,回声和文件写入就不会出现。任何帮助,将不胜感激!谢谢!

<?php
/*
    Login.php searches for the email address given by loginPage.php in the data base.
    If the email is found and the password given by loginPage.php matches that stored 
    in the data base, a session will begin and the client will be redirected to the 
    main page.
    *** INCOMPLETE ***  
*/
echo "HELLO!";
$email = $_POST["email"];
$password = $_POST["password"];

$errorLog = fopen("login.txt", "w");    
    fwrite($errorLog, "***Sesion started***");
$mysql_id = mysql_connect("localhost", "root", "12131");
if (!$mysql_id)
    {
        die('Could not connect: ' . mysql_error());
    }
mysql_select_db('informationStation', $mysql_id);

$results = mysql_query("SELECT password FROM Personals WHERE email = '" . $email . "';", $mysql_id);

if($results != null && $password == mysql_fetch_array($result)) 
    {
         $redirect  = 'Location: http://127.0.1.1/main.php';
    }
else 
    {
         $redirect  = 'Location: http://127.0.1.1/loginPage.php';
    {   
mysql_close($mysql_id); 
fwrite($errorLog, "result: " . $results);
fwrite($errorLog, "redirect: " . $redirect); 
fclose($errorLog);
header($redirect);  
?>
4

2 回答 2

0

试试这个让你开始..

$results = mysql_query("SELECT password FROM Personals WHERE email = '" . $email . "'", $mysql_id) or die(mysql_error());

但是您还需要阅读有关 sql 注入的信息。

并且您不应该将密码存储在数据库中而不对其进行加盐和散列处理。

而且您还需要使用数组语法来访问结果中的数据......

于 2012-08-02T15:48:06.727 回答
0
$mysql = mysql_connect("localhost", "root", "12131") or die('Could not connect: ' . mysql_error());
mysql_select_db('informationStation', $mysql);


function loged($email, $password) {
    $result = mysql_query("SELECT id FROM Personals WHERE email = '" . $email . "' AND password='" . $password . "'");
    if(mysql_num_rows($result) != 1)
        return false;
    return true;
}


if(loged(mysql_real_escape_string($email), md5($password))) {
    header('Location: http://127.0.1.1/mainPage.php');
    exit;
}   

header('Location: http://127.0.1.1/loginPage.php');

在此示例中,您需要使用 md5 加密方法存储用户密码(搜索其他更安全的加密方法)。

我们还针对 sql 注入对电子邮件地址进行了转义。

我创建了一个函数,每次你想查看用户是否登录时都可以调用它。

请注意,这不是一个完整的登录脚本。您还需要创建一个登录功能,您必须在其中为每个用户启动一个新会话。

于 2012-08-02T15:58:02.710 回答