php+mysql 最简单的登录验证
最简单的登录验证。
之后可以不断完善,不断练习sql注入,xss等等。
<html> <body> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head> <form method = "post" action = "login.php"> 登录<br> name:<input type = "text" name = "name"><br> password:<input type = "password" name = "password"><br> <input type = "submit"> </form> </body> </html>
<?php
header("Content-type: text/html; charset=utf-8");
$con = mysql_connect("localhost:3306","root","root");
if (!$con)
{
die("Could not connect: " . mysql_error());
}
//连接数据库
mysql_query("SET NAMES utf8");
mysql_select_db("user",$con);
/*
创建表单
$sql = "CREATE TABLE user(
id int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
name varchar(15),
password varchar(15)
)";
*/
$name = $_POST["name"] ;
$password = $_POST["password"];
$sql1= "INSERT INTO user(name,password)
VALUES( "$name", "$password")";
$sql2 = "SELECT id FROM user WHERE name= "$name" and password = "$password"" ;
$result = mysql_query($sql2,$con) ;
$row = mysql_fetch_array($result) ;
if($row[0])
{
echo "success";
}
else
{
echo "failed".mysql_error();
}
mysql_close($con);
?>
注意:
php中的单引号和双引号的区别很重要。
最简单的区别就是:
单引号中的代码不执行,所以速度快。
双引号中的代码会执行。
sql语句要用双引号包起来。
参考链接:PHP 单引号与双引号的区别 说的不是特别好,但是可以参考一下。
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: TP5 数组的特定格定输出
- 下一篇: vue-router和vue-cli以及组件之间的传值
