登录验证时,重定向到另一个页面失败

时间:2014-08-10 12:32:40

标签: php html mysql

以下代码重定向到空白页而不是.php文件。

我尝试了什么?

我尝试使用header()函数将用户重定向到另一个.php文件,如果用户存在于MYSQL数据库中,并且密码等于MYSQL数据库。

我目前的代码是什么?

<?php

session_start();
$verbindung = mysql_connect( "localhost", "******" , "*****" ); mysql_select_db( "m7_studios_de" );

$username = $_POST["username"];
$passwort = $_POST["password"];

$ergebnis = mysql_query("SELECT * FROM accounts WHERE username = '$username' LIMIT 1");
while($row = mysqli_fetch_object($ergebnis))
    if($row->password == $passwort){
        $_SESSION["username"] = $username;
        header("Location: http://www.m7-studios.de/overview.php");
        exit;
    }
    else{
        header( "Location: http://www.m7-studios.de/");
        exit;
    }

?> 

3 个答案:

答案 0 :(得分:2)

1)在header()调用之前不要输出任何空格,否则它将不起作用。您正在输出php的第一个和第二个块之间的空格

2)不要混用mysql和mysqli函数 - 您正在使用mysqli_fetch_object。实际上根本不使用mysql函数,它们已被弃用

3)使用参数化查询来保护自己免受SQL注入攻击。

答案 1 :(得分:1)

您好像在混合mysql_(在您的连接中)与mysqli_(提取行)。你需要在整个过程中使用其中一个。我建议mysqli_,因为mysql_已被弃用。

答案 2 :(得分:0)

也许这应该有用

  • 用户名必须唯一且区分大小写
  • 密码应为哈希值
  • 代码重定向到登录页面,如果登录失败,则GET error = loginfailed

<?php
    $verbindung = mysql_connect( "localhost", "******" , "*****" ); 
    mysql_select_db( "m7_studios_de" );
    $username = $_POST["username"];
    $passwort = $_POST["password"];
    $abfrage = "SELECT username FROM accounts WHERE username='$username' AND password='$password' LIMIT 1";
    $result = mysql_query($abfrage);
    if(mysql_num_rows($result)==1) {
        $_SESSION["username"] = $username;
        header("Location: http://foo.com/logged_in.php");
        die();
    } else {
        header("Location: http://foo.com/index?error=loginfailed");
        die();
    }
?>