我有一个登录div/page。此时,登录表单有一个外部文件的操作,即config.php。每当用户单击提交时,它当然会转到config.php文件,如果用户登录失败,则在config.php文件中,用户会重定向回login.html页面。
我现在的问题是,如果用户登录失败,如何在下面/表单底部回复它?
这是我的代码:
<?php
session_start();
// include connection file
include("configPDO.php");
$member_email=$_POST['member_email'];
$member_password=$_POST['member_password'];
$return = mysql_real_escape_string($_POST['return']);
$STM = $dbh->prepare("SELECT * FROM customer WHERE member_email = :member_email AND member_password = :member_password");
$STM->bindParam(':member_email', $member_email);
$STM->bindParam(':member_password', $member_password);
$STM->execute();
$count = $STM->rowCount();
$row = $STM -> fetch(PDO::FETCH_ASSOC);
if ( $count == 1 ) {
session_start();
$_SESSION['login_id'] = $row['member_id'];
$_SESSION['member_name'] = $row['member_name']; // added
$_SESSION['member_email'] = $row['member_email']; // added
//echo 'SESSION =' .$_SESSION['myusername'];
//echo 'ROW =' .$row['myusername'];
//var_dump($row);
if ( $_SESSION['login_id'] != '' || $_SESSION['login_id'] > 0 ) { // edited
header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
} else {
header("location: .$return");
}
}
else
{
header("location:.$return");
}
$dbh = null;
?>
这是我的表格:
<form method="post" enctype="multipart/form-data" action="config-login.php">
<table width="345" align="center" style="background:honeydew; vertical-align:top; margin:0px auto; border:solid medium yellowgreen;">
<td class="myimage">Email :</td>
<td width="196" style="padding-right:2px;"><input type="text" name="member_email" style="width:100%"/></td>
</tr>
<tr>
<td class="myimage">Password : </td>
<td style="padding-right:2px;"><input type="password" name="member_password" style="width:100%"/></td>
</tr>
<tr>
</table>
<p style="background:greenyellow; display:block; width:100%; overflow:hidden; padding:10px 0;">
<input class="myimage" style="margin-right:10px; padding:0 10px; float:right;" type="submit" value="Register"/></p>
</form>
我想在表格底部显示登录失败。有人能帮忙吗?非常感谢。
[更新]
我使用了@Mour解释的代码,代码的效果很好。但是,我忘了一件事,如果这个登录表单是通过javascript的点击功能显示的,这意味着如果点击login
的文本,那么这个登录表单就会显示出来。所以我现在想要的是,如果登录失败,我想保持这个登录表单的显示,而不必再点击登录文本。
<li>
<a href = "javascript:void(0)"
onclick ="document.getElementById('light').style.display='block';
document.getElementById('fade').style.display='block'">Login</a></li>
</ul>
<!--Black Overlay-->
<div id="fade" class="black_overlay" onLoad="initDynamicOptionLists()"></div>
<!--Pop Up Div-->
<div id="light" class="white_content">
<div id="loginbox">
<span id="login-form">Login</span>
<form method="post" action="config-login.php">
<table width="345" align="center" style="background:honeydew; vertical-align:top; margin:0px auto; border:solid medium yellowgreen;">
<tr>
<td class="myimage">Email :</td>
<td width="196" style="padding-right:2px;"><input type="text" name="member_email" style="width:100%"/></td></tr>
<tr>
<td class="myimage">Password : </td>
<td style="padding-right:2px;"><input type="password" name="member_password" style="width:100%"/></td></tr>
</table>
<div style="background:greenyellow; display:block; width:100%; overflow:hidden; margin:10px 0; padding:10px 0;">
<input type="hidden" name="return" value="<?php echo $_SERVER['REQUEST_URI']; ?>" />
<p><?php
if($_GET['result']=='failed'){
echo 'invalid login';
}?>
</p>
<input class="myimage" style="margin-right:10px; padding:0 10px; float:right;" type="submit" value="Login"/>
<span style="position: absolute; top: 11px; right:1px; color:white;" id="closeBlocked">
<a style="color:green; text-decoration:none; background:white; padding:10px;"
href = "javascript:void(0)"
onclick ="document.getElementById('light').style.display='none';
document.getElementById('fade').style.display='none'"><b>X</b></a></span>
</div>
</form>
</div>
</div>
<!-- End of Pop Up Div-->
将config-login.php代码更改为
else
{
header("location:.$return"."?result=failed");
}
并在顶部写下以下代码
<?php
if($_GET['result']=='failed'){
echo 'invalid login';
}
使用ajx u可以很容易地实现。否则,如果你不想使用ajax,那么当你重定向到登录页面或出现错误时通过发送一些带有url的状态类型的变量来执行如下操作header("location:login.php?status='fails'");在登录页面上,当用户来到登录页面时,检查这个状态变量,如果它的ststus失败,你可以提醒一些错误,否则你可以继续。
您也可以生成array
或object
并传入验证错误,然后生成JSON
并传入header
。
我写过并使用过这个,虽然它可能不是最好的,但它很干净。
这是与代码相关部分的链接。
switch($count){
case 0:
$array[$i++]='invalid or too short name. use alphanumerics,_';
break;
case 1:
$array[$i++]='invalid or too short password,use alphanumerics,_,@ ,$';
break;
case 2:
$array[$i++]="invalid email address";
break;
case 3:
$array[$i++]="invalid or too short address, use alphanumerics,-,_,:";
break;
default:
$array[$i++]="undefined error";
break;
}
// ...
if($array!=NULL){
$_SESSION['error']=json_encode($array);
header('Location:./signup.php');
}
// Of course check for validation of JSON data. You will find it in the manual
if(isset($_SESSION["error"])){
print_r("<ul>");
$array=json_decode($_SESSION['error']);
foreach($array as $elem){
print_r("<li>".$elem."</li>");
}
print_r("</ul>");
unset($_SESSION['error']);
$_SESSION['error']=NULL;
}