duansha8764
duansha8764
2016-04-05 19:19

用php和html登录

已采纳

I am making a login page using html and php, i did a simple one which was working fine using these code :

HTML Login Form

    <link rel="stylesheet" href="css/style.css">




 </head>

 <body>

  <div class="login">
<div class="login-triangle"></div>

  <h2 class="login-header">Login</h2>

 <form class="login-container" method="post" action="Login.php">
   <p><input type="text" id="username" name="username"        placeholder="Username"></p>

    <p><input type="password" id="password" name="password"    placeholder="Password"></p>
     <p><input type="submit" value="Login"></p>
  </form>
</div>
  <script    src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>


  </body>
 </html>

PHP

<?php
include ("dbconfig.php");
session_start();
$name = mysqli_real_escape_string($dbconfig, $_POST['username']); //to clean up, to avoid sql injection
//$name = md5($name);


$pw = mysqli_real_escape_string($dbconfig, $_POST['password']);
 //  $pw = md5($pw);


$sql_query="SELECT userid FROM user WHERE username='$name' AND password='$pw'";
$result = mysqli_query($dbconfig, $sql_query);
$row = mysqli_Fetch_array ($result, MYSQLI_ASSOC);
$count = mysqli_num_rows ($result);
 if ($count >0){
    $_SESSION['Login'] = $name;
    header ("location:Welcome.php");
 }
 if($count == 1)
  {
    echo "wrong login details";
  }

?>

But when i try to do the login with a new html file using the same php file it wont work at all, it keep saying "wrong login details" even though i am putting the right login in.

Here is the new html, i am thinking maybe it has to do with the additional classes which was added.

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<title>Kate's World Sign In</title>

    <!-- Google Fonts -->
    <link href='https://fonts.googleapis.com/css?     family=Roboto+Slab:400,100,300,700|Lato:400,100,300,700,900'    rel='stylesheet' type='text/css'>

    <link rel="stylesheet" href="css/animate.css">
    <!-- Custom Stylesheet -->
    <link rel="stylesheet" href="css/style.css">

     <script   src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js">  </script>
</head>

<body>
    <Form  method="post" action="Login.php">
    <div class="container">
        <div class="top">
            <h1 id="title" class="hidden"><span id="logo">Log <span>In</span></span></h1>
        </div>
        <div class="login-box animated fadeInUp">
            <div class="box-header">
                <h2>Log In</h2>
            </div>
            <label for="username">Username</label>
            <br/>
            <input type="text" id="username" name="username" >
            <br/>
            <label for="password">Password</label>
            <br/>
            <input type="password" id="password" name="password">
            <br/>
            <button type="submit">Sign In</button>
            <br/>
        </div>
        </div>
    </Form>
</body>

<script>
    $(document).ready(function () {
        $('#logo').addClass('animated fadeInDown');
        $("input:text:visible:first").focus();
    });
    $('#username').focus(function() {
        $('label[for="username"]').addClass('selected');
    });
    $('#username').blur(function() {
        $('label[for="username"]').removeClass('selected');
    });
    $('#password').focus(function() {
        $('label[for="password"]').addClass('selected');
    });
    $('#password').blur(function() {
        $('label[for="password"]').removeClass('selected');
    });
</script>

</html> 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • doubleyou1001 doubleyou1001 5年前

    No, the additional classes should not effect your PHP code.

    To solve the problem, you need to see what you are receiving on the PHP side. Stick in a few tests - echo out some data. First, at the very beginning. Then, when you know for sure what is comign through, move your tests down the file a bit. Work out all the bugs, then remove all the tests.

    For example, start by modifying your PHP like this:

        <?php
            session_start();
            include ("dbconfig.php");
            $name = mysqli_real_escape_string($dbconfig, $_POST['username']); //to clean up, to avoid sql injection
        echo 'Name: ' . $name. '<br>';
    
            $pw = mysqli_real_escape_string($dbconfig, $_POST['password']);
        echo 'Password: ' . $pw. '<br>';
    
        die();
    

    Then, move down the file a bit and do this:

        $sql_query="SELECT userid FROM user WHERE username='$name' AND password='$pw'";
        $result = mysqli_query($dbconfig, $sql_query);
        $row = mysqli_Fetch_array ($result, MYSQLI_ASSOC);
        $count = mysqli_num_rows ($result);
    echo 'Rows found: ' .$count. '<br>';
    
        if ($count >0){
    echo 'Inside count > 0<br>';
            $_SESSION['Login'] = $name;
            header ("location:Welcome.php");
        }else{
    echo 'Inside count ELSE<br>';
            echo "wrong login details";
        }
    

    Notes:

    1. PHP header() method will not work if other header messages have been sent. Alternative: echo '<meta http-equiv="refresh" content="0; url=http://new.example.com/address" />';

    2. Note McKenzma's observations about your if ($count >0){ code: both IF statements will be true if $count==1.

    3. Note that session_start() should be the very first instruction in your PHP file. See my example code above

    点赞 评论 复制链接分享
  • dsf8897 dsf8897 5年前

    You should have used if and else, not if and if.

    <?php
     $count = mysqli_num_rows ($result);
    if ($count >0){
    $_SESSION['Login'] = $name;
    header ("location:Welcome.php");
    } else {
    echo "wrong login details";
    }
    ?>
    
    点赞 评论 复制链接分享
  • doumiyi7063 doumiyi7063 5年前

    Your 2nd conditional should be "$count != 1". You want to return exactly one row for a successful login.

    点赞 评论 复制链接分享

相关推荐