php网页登录,为什么验证不通过,登陆不进去,输入正确的账号密码还是提示先登录?帮忙看一下
pass.php<?phpheader("Content-type:text/html;charset=utf-8");session_start();require("...
pass.php
<?php
header("Content-type:text/html;charset=utf-8");
session_start() ;
require("conn.php");
//echo 'test';exit;
$pass=$_POST["password"];
//$username = $_POST['name'];
$sql="select * from admin where name='".$_POST["name"]."'and password='".$pass."'";
//$sql = "select password,tel from admin where name = '$username'";
$result=mysql_query($sql) or die(mysql_error());
if(!$result){
header("Location:index.php?msg=用户名不存在");
exit;
}
$rs=mysql_fetch_assoc($result);
if($pass != $rs['password']){
header("Location:index.php?msg=密码有误");
exit;
}
//var_dump($rs);echo '--'.$pass;exit;
$_SESSION['username'] = $username;
$_SESSION['tel'] = $rs['tel'];
header("Location:home.php");
?>
登陆页面
index.php
<!doctype html>
<html lang="en" class="no-js">
<head>
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
<meta content="yes" name="apple-mobile-web-app-capable">
<meta content="black" name="apple-mobile-web-app-status-bar-style">
<meta name="format-detection" content="telephone=no">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/style.css">
<title>车票预订</title>
<script language="javascript" type="text/javascript" >
function check(){
if($('username').value == ''){
alert('用户名不能为空!');
}
if($('password').value == ''){
alert('手机号不能为空!');
}
}
function $(id){
return document.getElementById(id);
}
</script>
</head>
<body>
<div id="index" style="background-image:url(images/bg.jpg);">
<div class="page-container">
<h1>登陆</h1>
<form name="LoginForm" action="pass.php" method="post">
<input type="text" name="name" id="username" placeholder="输入用户名" autofocus required>
<input type="password" name="password" id="password" placeholder="输入密码!" required>
<input type="checkbox" checked="checked" class="Captcha">
<span style="font-size: 18px;float:left;margin-top:20px;" >自动登录</span>
<button type="submit" class="submit_button" onClick="check()">确定</button>
</form>
<p><font color='red'><?php echo @$_GET['msg']?></font></p>
</div>
</div>
</body>
</html>
数据库
id name password
1 admin admin
2 lily 12345
conn.php
<?php
$conn =@mysql_connect("localhost","root","12345");
if (!$conn){
die("数据库连接失败: " . mysql_error());
}
mysql_select_db("hr",$conn);
mysql_query("set character set 'utf8'");
mysql_query("set names 'utf8'");
?> 展开
<?php
header("Content-type:text/html;charset=utf-8");
session_start() ;
require("conn.php");
//echo 'test';exit;
$pass=$_POST["password"];
//$username = $_POST['name'];
$sql="select * from admin where name='".$_POST["name"]."'and password='".$pass."'";
//$sql = "select password,tel from admin where name = '$username'";
$result=mysql_query($sql) or die(mysql_error());
if(!$result){
header("Location:index.php?msg=用户名不存在");
exit;
}
$rs=mysql_fetch_assoc($result);
if($pass != $rs['password']){
header("Location:index.php?msg=密码有误");
exit;
}
//var_dump($rs);echo '--'.$pass;exit;
$_SESSION['username'] = $username;
$_SESSION['tel'] = $rs['tel'];
header("Location:home.php");
?>
登陆页面
index.php
<!doctype html>
<html lang="en" class="no-js">
<head>
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
<meta content="yes" name="apple-mobile-web-app-capable">
<meta content="black" name="apple-mobile-web-app-status-bar-style">
<meta name="format-detection" content="telephone=no">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/style.css">
<title>车票预订</title>
<script language="javascript" type="text/javascript" >
function check(){
if($('username').value == ''){
alert('用户名不能为空!');
}
if($('password').value == ''){
alert('手机号不能为空!');
}
}
function $(id){
return document.getElementById(id);
}
</script>
</head>
<body>
<div id="index" style="background-image:url(images/bg.jpg);">
<div class="page-container">
<h1>登陆</h1>
<form name="LoginForm" action="pass.php" method="post">
<input type="text" name="name" id="username" placeholder="输入用户名" autofocus required>
<input type="password" name="password" id="password" placeholder="输入密码!" required>
<input type="checkbox" checked="checked" class="Captcha">
<span style="font-size: 18px;float:left;margin-top:20px;" >自动登录</span>
<button type="submit" class="submit_button" onClick="check()">确定</button>
</form>
<p><font color='red'><?php echo @$_GET['msg']?></font></p>
</div>
</div>
</body>
</html>
数据库
id name password
1 admin admin
2 lily 12345
conn.php
<?php
$conn =@mysql_connect("localhost","root","12345");
if (!$conn){
die("数据库连接失败: " . mysql_error());
}
mysql_select_db("hr",$conn);
mysql_query("set character set 'utf8'");
mysql_query("set names 'utf8'");
?> 展开
2个回答
2016-05-19
展开全部
判断session前,需要先使用session_start()启用会话
session_start();
if(isset($_SESSION['username'])){
//用户已经登陆
}else{
//用户未登录或登陆超时
}
session_start();
if(isset($_SESSION['username'])){
//用户已经登陆
}else{
//用户未登录或登陆超时
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询