如果用户输入错误的ID和密码,如何在索引页上重定向错误消息

时间:2014-04-24 13:16:14

标签: php html web

我是php新手我希望如果用户输入错误的ID和密码以及索引页面上的错误消息重定向但我不知道该怎么做。我做了几件事,但没有任何作用。  我有2个文件叫做index.php和authenticate.php

  

的index.php

<form action="authenticate.php" method="post">
            <div class="get_info">
            <p>Username</p>
            <input type="text" name= "username" id="username" />
            <p>Password</p>
            <input type="password" name= "password" />
        </div> 

        <input type="submit" name="submit" value="Submit" onkeypress="submitOnEnter(this, event);" />
        </form>
  

Authenticate.php

 <?php
       include_once('config.php');

    // username and password sent from form
    $myusername=$_POST['username'];
    $mypassword=$_POST['password'];

    // To protect MySQL injection
    $myusername = stripslashes($myusername);
    $mypassword = stripslashes($mypassword);
    $myusername = mysql_real_escape_string($myusername);
    $mypassword = mysql_real_escape_string($mypassword);

    $sql="SELECT * FROM logins WHERE username='$myusername' and password='$mypassword'";
    $result=mysql_query($sql);

    // Mysql_num_row is counting table row
    $count=mysql_num_rows($result);
    // If result matched $myusername and $mypassword, table row must be 1 row
    $sqll="SELECT * FROM logins where username='$myusername'";
    $resultt=mysql_query($sqll);
    while($row = mysql_fetch_array($resultt))
      {
      echo $row['username'] . " " . $row['lastname'];
     //$req=$_request['username'];
      //echo"$req";
      }

    if($count==1){


    if(!isset($_SESSION)) 
        { 
            session_start(); 
        }

        $_SESSION['username'] = $myusername;

        header("location: courses/courses.php");
    }
    else {

        header("location: index.php");
        //echo"<script>alert('Username already exist!')</script>";
    //echo "Wrong Username or Password";
    }

    ?>

5 个答案:

答案 0 :(得分:0)

试试这个。

else{
    // You Can use php headers() instead of a javascript redirect if you want
    print "<script type='text/javascript'>window.location='index.php';</script>";
    //echo mysql_error();
}

答案 1 :(得分:0)

只需使用会话

即可

Authenticate.php

else {
        $_SESSION['error_message']="Wrong Username or Password";
        header("location: index.php");
    }

的index.php

if(isset($_SESSION['error_message']){
   echo $_SESSION['error_message'];
   unset($_SESSION['error_message']);
}

答案 2 :(得分:0)

  

尚未测试过,但这应该有效..

 <?php
    if(isset($_SESSION['msg']) && $_SESSION['msg'] != ''){
        echo $_SESSION['msg'];
        unset($_SESSION['msg']);
    }
    include_once('config.php');
    //You can start the session so long, so you can carry over your error message
    session_start();
    // username and password sent from form
    $myusername=$_POST['username'];
    $mypassword=$_POST['password'];

    // To protect MySQL injection
    $myusername = stripslashes($myusername);
    $mypassword = stripslashes($mypassword);
    $myusername = mysql_real_escape_string($myusername);
    $mypassword = mysql_real_escape_string($mypassword);

    $sql="SELECT * FROM logins WHERE username='$myusername' and password='$mypassword' LIMIT 1";

    $result=mysql_query($sql);
    //Get the data
    $data = mysql_fetch_array($result);

    if(!empty($data)){      //Alternatively you could just go "if($result){}"
        //Set the session username (Should use the user id rather or login id | Just tad bit of advice :) )

        $_SESSION['username'] = $data[0]['username'];
        //Set a success message
        $_SESSION['msg'] = "You've successfully logged in";
        //Redirect them to their page
        header("location: courses/courses.php");
    }else{
        //Else set message that credentials is bad
        $_SESSION['msg'] = "Login credentials bad"
        //Redirect them to the index page
        header("location: index.php");
    }

答案 3 :(得分:0)

如果您想显示提醒

...
?>
<script type='text/javascript'>
    <?php if(isset($_SESSION['error_message']){ ?>
       alert("<?php echo $_SESSION['error_message']; ?>");
       <?php unset($_SESSION['error_message']); ?>
    <?php } ?>
</script>

答案 4 :(得分:0)

一些简单的代码:

<?php
header("HTTP/1.0 401 Unauthorized"); // Set 401 Error
header("Location: index.php?error=Invalid+Username+or+Password"); // Redirect
exit(); // Don't want php to still keep executing
/* basic script by anonman. in public domain */
?>