登录页面在登录时不断清除我的文本框。

时间:2013-02-21 08:15:18

标签: php html mysql login

我有这个问题。当我想登录我的php页面时,它只是清除文本框而没有别的..我一直试图解决这个问题,但我找不到解决方案。这是代码:

<?php
include_once ("db.php");
include_once("session.php");

$username=$_POST['uporabnisko']; 
$pass=$_POST['geslo'];

    if ((!empty($username)) && (!empty($pass)))
    {
        $pass=sha1($pass); // zakodiramo geslo

        /*$query=mysql_query("SELECT username, password FROM users WHERE username='$username' AND password='$pass'");
        $username=mysql_real_escape_string($username);
        $pass=mysql_real_escape_string($pass);*/

        $query = sprintf("SELECT username, password FROM users WHERE username = '%s' AND password = '%s'",                   
                    mysql_real_escape_string($username), mysql_real_escape_string($pass));

        $result=mysql_query($query);

            if(mysql_num_rows($result) == 1) // preveri, če je uporabnik v bazi 
            {
                $user = mysql_fetch_array($result);

                $_SESSION['prijavljen'] = 1;
                $_SESSION['id_user'] = $user['id'];
                $_SESSION['username'] = $user['username'];

                echo   // preusmeri na index.php
                ("<SCRIPT LANGUAGE='JavaScript'>
                window.alert('Uspešna prijava!')
                window.location.href='index.php'
                </SCRIPT>");
            }
            else
            {
                echo 
                ("<SCRIPT LANGUAGE='JavaScript'>
                window.alert('Napačno uporabniško ime /geslo..')
                window.location.href='prijava.php'
                </SCRIPT>");
            }
    }
    else
    {
        echo 
        ("<SCRIPT LANGUAGE='JavaScript'>
        window.alert('Niste izpolnili vseh polj!')
        window.location.href='prijava.php'
        </SCRIPT>");
    }
?>

我不知道会出现什么问题......

2 个答案:

答案 0 :(得分:0)

您在每个Javascript行后都缺少; 。你需要

echo ("<SCRIPT LANGUAGE='JavaScript'>
                window.alert('Napačno uporabniško ime /geslo..');
                window.location.href='prijava.php';
                </SCRIPT>");

答案 1 :(得分:0)

您应该将include_once标记更改为require_once标记,以确保实际包含您的脚本。

您还应该至少使用mysqli或预备语句来登录,以保护自己免受SQL注入攻击。

要重定向页面,可以使用php中的标题功能

$result=mysql_query($query);

        if(mysql_num_rows($result) == 1) // preveri, če je uporabnik v bazi 
        {
            $user = mysql_fetch_array($result);

            $_SESSION['prijavljen'] = 1;
            $_SESSION['id_user'] = $user['id'];
            $_SESSION['username'] = $user['username'];

            header("location:index.php");
         } else {
         echo("Napačno uporabniško ime /geslo..");
         header("location:prijava.php");
         }

尝试这些更改,可能会有所帮助