dsrnwngq411594 2013-06-28 11:17
浏览 30
已采纳

php登录脚本无法加载[关闭]

I'm new to php and i need help with this code, because i get "HTTP-fout 500 (Internal Server Error):" when i try to load it. Could someone post the whole good code. thanks.

  <?php
session_start();


if(empty($_POST['username']) || empty($_POST['password'])) {
$notifications[] = 'Login failed! Please provide a username and password.';
   }

   if(count($notifications) == 0) {
try {
    $dbh = new PDO('mysql:dbname=####;host=####',   
'####', '####');

    $sql = "SELECT username, verified FROM users WHERE username = :username AND    
password = :password";
    $sth = $dbh->prepare($sql);
    $sth->execute(array(
        ':username'    => $_POST['username'],
        ':password' => md5($_POST['password'])
    ));

    $result = $sth->fetch(PDO::FETCH_ASSOC);

    if($result) {
        // Set session details and redirect user to members page
        session_regenerate_id();
    $_SESSION['username']=':username';
    $_SESSION['password']=':password';

        header('Location: index.php');
    } else {
        $notifications[] = "Username or Password incorrect.";
    }
    ?>
  • 写回答

2条回答 默认 最新

  • doqw89029 2013-06-28 11:47
    关注

    You are missing a few curly braces. Not sure if this is the cause of your issue but try this out. The third and forth lines added set the error reporting to display on the current page instead of logging it to file / syslog / etc.

    <?php
    session_start();
    ini_set('display_errors', true);
    error_reporting(E_ALL);
    
    if(empty($_POST['username']) || empty($_POST['password'])) {
        $notifications[] = 'Login failed! Please provide a username and password.';
    }
    
    if(count($notifications) == 0) {
        try {
            $dbh = new PDO('mysql:dbname=####;host=####','####', '####');
            $sql = "SELECT username, verified FROM users WHERE username = :username AND password = :password";
            $sth = $dbh->prepare($sql);
            $sth->execute(array(
                ':username'    => $_POST['username'],
                ':password' => md5($_POST['password'])
            ));
    
            $result = $sth->fetch(PDO::FETCH_ASSOC);
    
            if($result) {
                session_regenerate_id();
                $_SESSION['username']=':username';
                $_SESSION['password']=':password';
    
                header('Location: index.php');
            } else {
                $notifications[] = "Username or Password incorrect.";
            }
        }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?