duanqian8867 2016-11-04 14:49
浏览 21

在php和编辑登录页面

i want to create a login page, display the data from database of particular user in table form upto 4 columns and 1 row and also should be able to update the table data's and then logout. can anyone help me in this with a script. i need it to be done using php, mysql. i have tried till this, but i don't know what to do beyond this.

<html>
<form action="login.php" method="post">
    Username: <input type="text" name="username"><p>
    Password: <input type="password" name="password"><p>
              <input type="submit" value="Log in!" >
</form>           

<?php

session_start();

$username = $_POST['username'];
$password = $_POST['password'];

if ($username&&$password) 
{
    $connect = mysql_connect("localhost", "root", "") or die("Couldn't connect to the database!");
    mysql_select_db("login") or die("Couldn't find database");

    $query = mysql_query("SELECT * FROM users WHERE username='$username'");

    $numrows = mysql_num_rows($query);

    if ($numrows!==0)
    {
        while($row = mysql_fetch_assoc($query)) 
        {
            $dbusername = $row['username'];
            $dbpassword = $row['password']; # code...
        }

        if ($username==$dbusername&&$password==$dbpassword)
        {
            echo "You are logged in!";# 
            @$_SESSION['usernme'] = $username;
        }
        else
            echo "Your password is incorrect!";
    # code...
    }
    else
        die("That user doesn't exists!");

}
else
    die("Please enter a username and password!")
?>
  • 写回答

1条回答 默认 最新

  • duangu1878 2016-11-04 14:58
    关注

    Why not directly let MySQL check if the combination of username and password is correct?

    $query = mysqli_query("SELECT * FROM users WHERE username='" . $username . "' AND password = '" . $password . "'");

    Only if mysqli_num_rows($query) == 1, there is a valid login. More than 1 row should not be possible.

    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助