Php会话变量,阻止用户

时间:2013-02-05 02:56:43

标签: php

嘿,任何人都可以查看我的代码并帮助我......我正在尝试使用如果用户输入密码和用户名错误3次,它只会将它们转发到404 html页。不幸的是,我在浏览器中显示了一些代码,我不知道为什么。我知道php,所以如果代码是草率或不正确的,请不要跟我讨论它。

的login.php:

<?php 
session_start(); 
$_SESSION['wrong']=0;
$user = $_POST['user'];
$pass = $_POST['pass'];
if($user == "school"
&& $pass == "homework")
{
echo"Password system work!";
}else{
$_SESSION['wrong']=$_SESSION['wrong']+1;
header( 'Location: http://localhost' ) ;
}
?>

的index.html:

<? session_start(); 
include 'login.php';
global $_SESSION['wrong'];
if($_SESSION['wrong']>=3){
header( 'Location: 404_File_Not_Found.html' ) ; //This is what the browser is showing

}
?>
<html>
<head>
<style type="text/css">

#form{
width:  340px;
height: 400px;
box-shadow: 0px 2px 5px rgba(0,0,0,0.25);
position: relative;
-webkit-box-shadow: 0px 2px 5px;
border: solid 1px #ddd;
padding: 30px 30px 60px 30px;
background: #fff;
border-radius: 10px;
}
p{
font-family: Verdana;

}
div{
margin-left:auto;
margin-right:auto;
margin-top:5%;

}
img{
width: 120;
height: 133;
left: 100px;
position: relative;
}
input{
margin-left:10%;
margin-right:5%;
font-size: 14px;
width: 264px;
padding: 9px 7px 7px;
background: none !important;
position: relative;
z-index: 10;
margin-bottom: 20px;
border-radius: 5px;

}
.button:hover{
color: red;
}
</style>


</head>
<body>
<div id="form">     
<img src="http://traceybaptiste.files.wordpress.com/2011/01/homework2.jpg" />
    <form method="POST" action="login.php">
    <p>User:</p> <input type="text" name="user"></input>
    <p>Pass:</p> <input type="password" name="pass"></input>
    <input type="submit" name="submit" class="button"></input>
    </form>
</div>
</body>
</html>

404.html:

<html>
<head>
<style>
html{
background-color:grey;
}
body{
background-image:url("404.jpg");
background-repeat:no-repeat;
margin-left: 25%;
margin-right: auto;
margin-top: 10%;

}
</style>
</head>
<body>
</body>
</html>

3 个答案:

答案 0 :(得分:2)

我不太确定,但我认为你不应该包含login.php(index.html的第二行)。我也是php的新手,但似乎你的表单在index.html上,然后你将数据发布到login.php并从那里重定向用户。在表单开头包含login.php对我来说真的没有意义。我可能会弄错,但只是想帮忙。

我也认为William N编写的代码也是错误的,这就是为什么你被卡在login.php中,如果密码错误,它不会将你重定向到localhost。

<?php 
session_start(); 
$_SESSION['wrong']=0;
$user = $_POST['user'];
$pass = $_POST['pass'];
if($user == "school"
&& $pass == "homework")
{
echo"Password system work!";
}else{  
$_SESSION['wrong']=$_SESSION['wrong']+1;
header( 'Location: http://localhost' ) ;}

if($_SESSION['wrong'] == 3)

header( 'Location: diepage.html' ) ;

?>

试试这个。

编辑:哦,似乎你每次加载login.php($ _SESSION ['wrong'] = 0;)时都会通过为它分配0来重置你的$ _SESSION ['错误'],所以它永远不会要去3.可能你可以这样做:

if (!isset ($_SESSION['wrong'])){
$_SESSION['wrong']=0;}

因此,只有在之前没有设置的情况下,您才会启动并将其设为零。但我再也不确定了。

答案 1 :(得分:0)

<?php 
session_start(); 
$_SESSION['wrong']=0;
$user = $_POST['user'];
$pass = $_POST['pass'];
if($user == "school"
&& $pass == "homework")
{
echo"Password system work!";
}else{  $_SESSION['wrong']=$_SESSION['wrong']+1; if($_SESSION['wrong'] == 3)

header( 'Location: diepage.html' ) ;
}
?>

答案 2 :(得分:0)

如果您的文件名为index.html,则不会被PHP解析。

另外,使用<?php代替<?,它更可靠,因为它不依赖于特定的配置设置。