douyun4524 2016-06-16 17:10
浏览 35

在login.php中输入成功的用户名和密码之前,错误消息仍然存在

These are the codes of the website that I'm working on:

1) login.php

<?php
session_start();
include("connection.php");

// username and password sent from form 
$myusername = (isset($_POST['myusername']) ? $_POST['myusername'] : null);
$mypassword = (isset($_POST['mypassword']) ? $_POST['mypassword'] : null); 


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

$qry = "SELECT * FROM user WHERE user_name ='$myusername' AND user_password = '$mypassword'";
$result = mysql_query($qry);
$count = mysql_num_rows($result);

$output = mysql_fetch_assoc($result);

// If result matched $myusername and $mypassword, table row must be 1 row
      if($count == 1) {
         $_SESSION['login_user'] = $myusername;
         $_SESSION['mypassword'] = $mypassword;
         header("location: foodline.php");
         exit();
      }
      else {
         echo "Invalid Username or Password";
      }
?>


<html>
   
   <head>
      <title>Login Page</title>
      
      <style type = "text/css">
         body {
            font-family:Arial, Helvetica, sans-serif;
            font-size:14px;
         }
         
         label {
            font-weight:bold;
            width:100px;
            font-size:14px;
         }
         
         .box {
            border:#666666 solid 1px;
         }
      </style>
      
   </head>
   
   <body bgcolor = "#FFFFFF">
    
      <div align = "center">
         <div style = "width:300px; border: solid 1px #333333; " align = "left">
            <div style = "background-color:#333333; color:#FFFFFF; padding:3px;"><b>Login</b></div>
                
            <div style = "margin:30px">
               
               <form action = "" method = "post">
                  <label>UserName  :</label><input type = "text" name = "username" class = "box"/><br /><br />
                  <label>Password  :</label><input type = "password" name = "password" class = "box" /><br/><br />
                  <input type = "submit" value = " Submit "/><br />
               </form>                    
            </div>
                
         </div>
            
      </div>

   </body>
</html>

2) connection.php

<?php
$mysql_hostname ="localhost";
$mysql_user = "root";
$mysql_password ="";
$mysql_database = "foodline";

mysql_connect("$mysql_hostname", "$mysql_user", "$mysql_password") or die("Could not connect database");
mysql_select_db("$mysql_database") or die("Could not connect the database".mysql_error());
?>

3) session.php

<?php
   include('connection.php');
   session_start();
   
   $user_check = $_SESSION['login_user'];
   
   $ses_sql = mysql_query($user_check);
   
   $row = mysql_fetch_array($ses_sql);
   
   $login_session = $row['username'];
   
   if(!isset($_SESSION['login_user']))
   {
      header("location:foodline.php");
   }
?>

4) This is the output screen that I'm getting: Output Screen

The error message 'Invalid Username or Password' won't remove from above codes. Anything that I'm missing or anything that is done extra on the code?

Please help me!

</div>
  • 写回答

1条回答 默认 最新

  • duannao3402 2016-06-16 17:16
    关注

    You'd only want the PHP part to execute if your form has been submitted. Make use of isset.

    Update your php with this.

    <?php
    session_start();
    include("connection.php");
    
    if(isset($_POST['submit']))
    {
        // username and password sent from form 
        $myusername = (isset($_POST['myusername']) ? $_POST['myusername'] : null);
        $mypassword = (isset($_POST['mypassword']) ? $_POST['mypassword'] : null); 
    
    
        // To protect MySQL injection
        $myusername = stripslashes($myusername); 
        $mypassword = stripslashes($mypassword); 
        $myusername = mysql_real_escape_string($myusername); 
        $mypassword = mysql_real_escape_string($mypassword); 
    
        $qry = "SELECT * FROM user WHERE user_name ='$myusername' AND user_password = '$mypassword'";
        $result = mysql_query($qry);
        $count = mysql_num_rows($result);
    
        $output = mysql_fetch_assoc($result);
    
        // If result matched $myusername and $mypassword, table row must be 1 row
              if($count == 1) {
                 $_SESSION['login_user'] = $myusername;
                 $_SESSION['mypassword'] = $mypassword;
                 header("location: foodline.php");
                 exit();
              }
              else {
                 echo "Invalid Username or Password";
              }
    }
    ?>
    

    Now, your php will only execute if the form has been submitted.

    Update

    "SELECT * FROM user WHERE user_name ='" . $myusername . "' AND user_password = '" . $mypassword . "'";
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100