我使用OOP概念创建了一个Web应用程序,无需连接到数据库。我为每个活动都包含了单独的功能,但由于我对oops和php缺乏了解,因此出现了一些逻辑错误。当我在xampp中运行我的应用程序时我没有得到任何输出只是空白屏幕加载帮我解决它,我在下面分享我的代码
不显示任何错误消息。
<?php session_start();
class loginAction {
var $username;
var $password;
function checkLogin($username,$password) {
if($username == 'admin' && password == '123'){
$_SESSION['auth']['username'] = $username;
$_SESSION['auth']['password'] = $password;
return true;
}else {
return false;
}
}
function logout() {
session_start();
session_destroy(); /* Destroy started session */
header("location:index.php");
exit;
}
}
?>
<?php
include_once "function.php";
$loginAction = new loginAction();
if(isset($_POST['Submit'])) {
$loginAction->username ='admin';
$loginAction->password = '123';
header("location:home.php");
exit;
} else {
return "<span style = 'color:red'>Invalid Login Credentials</span>";
}
return 0;
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title> Login</title>
</head>
<body>
<br>
<form action="" method="post" name="Login_Form">
<table width="400" border="0" align="center" cellpadding="5" cellspacing="1" class="Table">
<?php if(isset($msg)){?>
<tr>
<td colspan="2" align="center" valign="top"><?php echo $msg;?></td>
</tr>
<?php } ?>
<tr>
<td colspan="2" align="left" valign="top"><h3>Login</h3></td>
</tr>
<tr>
<td align="right" valign="top">Username</td>
<td><input name="username" type="text" class="Input"></td>
</tr>
<tr>
<td align="right">Password</td>
<td><input name="password" type="password" class="Input"></td>
</tr>
<tr>
<td> </td>
<td><input name="Submit" type="submit" value="Login" class="Button3"></td>
</tr>
</table>
</form>
</body>
</html>
<?php
include_once "function.php";
$loginAction = new loginAction();
$loginAction->logout();
?>
<?php session_start(); /* Starts the session */
if(isset($_SESSION['auth'])){
if($_SESSION['auth']['Username']=='admin' && $_SESSION['auth']['Password']=='123'){
}else{
header("location:login.php");
exit;
}
}
?>
Congratulation! You have logged in<br><a href="logout.php">Click here</a> to Logout.
答案 0 :(得分:1)
1)你需要像这个
一样用post值调用方法2)检查返回布尔值并重定向到home,否则显示错误消息。
3)并删除不需要的回报。
<强>的index.php 强>
include_once "function.php";
$loginAction = new loginAction();
if(isset($_POST['Submit'])) {
$loginAction->username ='admin';
$loginAction->password = '123';
$result = $loginAction->checkLogin($_POST['username'],$_POST['password']);
if($result){
header("location:home.php");
exit;
}else {
echo "<span style = 'color:red'>Invalid Login Credentials</span>";
}
}
?>
<强> Home.php 强>
<?php session_start(); /* Starts the session */
if(isset($_SESSION['auth'])){
if($_SESSION['auth']['Username']!='admin' && $_SESSION['auth']['Password']!='123'){
header("location:login.php");
exit;
}
}
?>
Congratulation! You have logged in<br><a href="logout.php">Click here</a> to Logout.
答案 1 :(得分:0)
如果你看一下你的index.php
,你会发现它永远不会超过第一个php代码块:在那个块中你可以exit;
或return
一个值({{1或者字符串)。
根据manual:
如果从全局范围调用,则执行当前脚本 文件已结束。
因此,您需要从0
中删除return
语句。
另请注意,如果发出实际的帖子请求,您应该只显示错误;注意设置index.php
不一定是错误。