PHP,重新加载页面并显示无效的凭据消息?

时间:2014-11-27 11:15:39

标签: php mysql

我的index.php文件中有这个片段,它会要求用户登录。如果细节不在数据库系统中,我想重新加载index.php页面并显示一条消息,如"无效的凭据,再试一次"或类似的东西。

我怎样才能做到这一点?这是我的代码:

    <?php
session_start();

//Check if user is already logged in
if(!isset($_POST['id']))
{
    $message = 'User is logged in already';
}

//Check the username and password have been submitted
if(!isset( $_POST['Username'], $_POST['Password']))
{
    $message = 'Please enter a valid username and password';
}

//Check the username is the correct length
else if (strlen($_POST['Username']) > 20 || strlen($_POST['Username']) < 4)
{
    $message = 'Incorrect Username length, please try again';
}

//Check the password is the correct length
else if (strlen($_POST['Password']) > 20 || strlen($_POST['Password']) < 4)
{
    $message = 'Incorrect Password length, please try again';
}

//Check the username only has alpha numeric characters
else if (ctype_alnum($_POST['Username']) != true)
{
    $message = "Username must be alpha numeric";
}

else if (ctype_alnum($_POST['Password']) != true)
{
    $message = "Password must be alpha numeric";
}
else
{
    $admin = "****";
    $adminPassword = "****";

    //Enter the valid data into the database
    $username = filter_var($_POST['Username'], FILTER_SANITIZE_STRING);
    $password = filter_var($_POST['Password'], FILTER_SANITIZE_STRING);

    //Encrypt the password
    $password = sha1($password);

    //Connect to the database
    $SQLusername = "";
    $SQLpassword = "";
    $SQLhostname = ""; 
    $databaseName = "";

    try
    {
        //connection to the database
        $dbhandle = mysql_connect($SQLhostname, $SQLusername, $SQLpassword) 
          or die("Unable to connect to MySQL");
        echo "Connected to MySQL<br>";

        //select a database to work with
        $selected = mysql_select_db($databaseName, $dbhandle)
                or die("Could not select database");

        $selectCustomers = "SELECT * FROM
                customers WHERE name = 
                '$username' AND password = '$password'";

        $selectEmployees = "SELECT * FROM
                employees WHERE name = 
                '$username' AND password = '$password'";

        $selectAdmin = "SELECT * FROM
                employees WHERE name = 
                '$admin' AND password = '$adminPassword'";

        $customerResult = mysql_query($selectCustomers) or die(mysql_error());
        $employeeResult = mysql_query($selectEmployees) or die(mysql_error());
        $adminResult = mysql_query($selectAdmin) or die(mysql_error());

        if(mysql_num_rows($customerResult) >= 1)
        {
            header("Location: memberPage.php");
            $_SESSION["customer"] = "customer";
        }

        else if(mysql_num_rows($employeeResult) >= 1)
        {
           header("Location: adminPage.php");
           $_SESSION["admin"] = "admin";
        }

        else if(mysql_num_rows($adminResult) >= 1)
        {
           header("Location: adminPage.php");
           $_SESSION["admin"] = "admin";
        }

        else
        {
             header("Location: index.php");
        }

        //close the connection
        mysql_close($dbhandle);
    } 
    catch(Exception $e)
    {

        $message = 'We are unable to process your request. Please try again later"';
    }


}
?>
<html>
<head>
<title>Login</title>
</head>
<body>
</body>
</html>

3 个答案:

答案 0 :(得分:2)

在上一个else

else
    {
         header("Location: index.php");
         //I would like to return a message here and then reload this page
    }

尝试使用以下命令进行更改:

else
    {
die(header('refresh: 3; url=index.php').'Invalid Credentials, wait 3 seconds or just click <a href="index.php">HERE</a> to check again.');
    }

&#34; 3&#34; 是等待刷新的秒数(重新加载页面)。

希望这可以帮到你!

答案 1 :(得分:1)

你可以试试这个:

echo '<script type="text/javascript">alert("invalid credentials");</script>' ;
echo '<script type="text/javascript">location.reload();</script>' ;

答案 2 :(得分:1)

您可以尝试提供以下参数:

&#13;
&#13;
header("Location: index.php?err=1"); 
&#13;
&#13;
&#13;

然后通过

将错误输入到页面中

&#13;
&#13;
if($_GET['err']==1)
{

echo "your credentials are wrong";

}
&#13;
&#13;
&#13;

希望这会有所帮助......