php容易的登录页面代码说明

利用PHP实现登录与注册功能以及使用PHP读取mysql数据库——以表格形式显示数据

登录界面

<body>

<form action=”login1.php” method=”post”>

<div>用户名:<input type=”text” name=”uid” /></div><br />

<div>密码:<input type=”password” name=”pwd” /></div><br />

<div><input type=”submit” value=”登录”/></div>

</form>

</body>


后台登录处理

<?php

$uid = $_POST[“uid”];

$pwd = $_POST[“pwd”];

$db = new MySQLi(“localhost”,”root”,””,”0710_info”);

if(mysqli_connect_error()){

die(“连接失败”);

}

$sql = “select pwd from users where uid='{$uid}'”;

//echo $sql;

$result = $db->query($sql);

$arr = $result->fetch_row();

//防止sql注入攻击

if($arr[0]==$pwd && !empty($pwd)){

//跳转到主界面(php方式)

header(“location:register_system.php”);

//JS跳转页面方式

/*echo “<script>

window.location.href=’register_system.php’

</script>”;*/

}else{

header(“location:login_system.php”);

}

?>


注册界面

<body>

<form action=”register1.php” method=”post” >

<div style=”font-size:20px; font-weight:1000; font-family:微软雅黑; margin-left:50px; margin-bottom:20px”>用户注册</div>

<div>用户名:<input type=”text” name=”uid” /></div><br />

<div>登录密码:<input type=”password” name=”pwd” /></div><br />

<div>姓名:<input type=”text” name=”name” /></div><br />

<div>性别:

<input type=”radio” name=”sex” value=”1″ />男

<input type=”radio” name=”sex” value=”0″ />女

</div><br />

<div><input type=”submit” value=”注册” onclick=”return check()”/></div>

</form>

</body>


JS表单验证

<script>

function check(){

var uid = document.getElementsByTagName(“input”)[0].value;

if(uid == “”){

alert(“请输入用户名!”);

return false;

}

var pwd = document.getElementsByTagName(“input”)[1].value;

if(pwd == “”){

alert(“请输入密码!”);

return false;

}

var name = document.getElementsByTagName(“input”)[2].value;

if(name == “”){

alert(“请输入姓名!”);

return false;

}

}

</script>


后台注册处理

<?php

$uid = $_POST[“uid”];

$pwd = $_POST[“pwd”];

$name = $_POST[“name”];

$sex = $_POST[“sex”];

$db = new MySQLi(“localhost”,”root”,””,”0710_info”);

if(mysqli_connect_error()){

die(“连接失败”);

}

$sql = “insert into users values(‘{$uid}’,'{$pwd}’,'{$name}’,{$sex})”;

$r = $db->query($sql);

if($r){

echo “注册成功!”;

}else{

echo “注册失败!”;

}

?>


PHP读取mysql数据库——以表格形式显示数据

<body>

<table width=”50%” border=”1″>

<tr>

<td>代号</td>

<td>姓名</td>

<td>性别</td>

<td>生日</td>

<td>民族</td>

</tr>

<?php

$db = new MySQLi(“localhost”,”root”,””,”0710_info”);

$sql = “select info.code,info.name,sex,birthday,nation.name from info join nation on info.nation=nation.code”;//联合查询

$result = $db->query($sql);

$arr = $result->fetch_all();

//var_dump($arr);

//利用foreach循环遍历所有数据

foreach($arr as $v){

$sex = $v[2]?’男’:’女’;//利用三元运算符判断性别

echo “<tr>

<td>{$v[0]}</td>

<td>{$v[1]}</td>

<td>{$sex}</td>

<td>{$v[3]}</td>

<td>{$v[4]}</td>

</tr>”;

}

?>

</table>

</body>