How to exit the application on 3 wrong attempts - php

I have made a login page where the No. of attempts is checked. The problem right now is it locks the user but keeps the counter for No. of Attempts increasing. I want to exit stop the counter to be increment. Please Help!
<html>
<head><title>Meeting Room Application</title>
<script type="text/javascript">
function validation()
{
var uname=document.forms["Login"]["txtuid"].value;
var pwd=document.forms["Login"]["txtpwd"].value;
if((uname==null || uname=="") && (pwd==null || pwd==""))
{
alert("Please fill all the details");
return false;
}
if(uname==null || uname=="")
{
alert("Please enter username");
return false;
}
if(pwd==null || pwd=="")
{
alert("Please enter password");
return false;
}
if(noofattempt>=3)
return false;
}
</script>
</head>
<body background="a6.jpg">
<?php
session_start();
if(isset($_SESSION['loginattempt']))
$NoOfAttempt=(int) $_SESSION['loginattempt'];
else
$NoOfAttempt=0;
?>
<table align="center" cellpadding="5" cellspacing="5">
<tr><td colspan="2" align="center"><img src="LOGO.jpg" width="275px;"> </td></tr>
<tr><th align="center" colspan="2">Kindly provide Login Details</th</tr>
<form method="post" name="Login" action="checkdb.php" onsubmit="return validation()" >
<tr><td align="right"><b>UserId : </td><td><input type="text" name="txtuid" placeholder="user"></td></tr>
<tr><td align="right"><b>Password : </td><td><input type="password" name="txtpwd" placeholder="*****"></td></tr>
<tr><td></td>
<td><font color="red">
<?php
if(isset($_SESSION['error']))
{
echo $_SESSION['error']."<br>";
echo "No Of Attempt ". $NoOfAttempt;
}
if($NoOfAttempt>=3)
echo "<br>Your Account Has been Locked..Contact Admin";
?> </font></td></tr>
<tr><td colspan="2" align="center"><input type="submit" value="Sign in">
<input type="reset" value="Reset"><input type="hidden" name='NoOfAttempt' value= <?php echo $NoOfAttempt; ?> /></td>
</tr>
</form>
</div>
</div>
</table>
</body>
</html>

You are not retrieving the hidden form value NoOfAttempt in your validation() function.
var noofattempt=document.forms["Login"]["NoOfAttempt"].value;
Note that this will stop the user from accidentally submitting the form additional times after the limit has been reached, however as a previous commenter said, this can be easily overridden by someone who is trying to do so, or even possibly just by having Javascript disabled. So the limit should be enforced somewhere else in your PHP code.

Related

showing div near save button if condition is true

I have simple code in php to enter name and address to the database.If name is already present in the database printing the message saying that it is already present ,if not entering data into database.But i want message has to be displayed near save button
<form method="POST" action="" >
<table>
<tr>
<td><br>Name t</td><td><br><input type="text" name="Name" id="wgtmsr" required/></td>
</tr>
<tr>
<td><br>Address</td>
<td><br/><textarea rows="3" cols="33" name="Address" id="inc_address" required></textarea></td>
</tr>
<tr>
<td><br></td><td><input type="submit" class="sav" name="rep" value="Save" id="btnsize" /></td>
</tr>
</table>
<?php
if (isset($_POST['rep']))
{
$Name=$_POST['Name'];
$Address=$_POST['Address'];
try{
$test=/* query check if name already present in database*/
if(!$test)
{
/*then insert into database*/
}else{
?> <div id="msg">
<?php echo "alreday present";?>
</div>
<?php
}
}catch(Exception $e) {
//echo "Error ".$e->getMessage();
}
}
But div is coming above the table.Please give suggestions so how can i style it near the save button
style:
#msg
{
color:#EA6262;
font-size:14pt;
margin-left:27% ;
margin-bttom:5% ;
}
Insert div after save button like
<input type="submit" class="sav" name="rep" value="Save" id="btnsize" /><span class="err" id="add_err"></span>
You can print the error using javascript.
document.getElementById("add_err").innerHTML = "Already having name!";
I tried similar thing its working
<?php
/**
* Created by PhpStorm.
* User: root
* Date: 3/9/15
* Time: 4:03 PM
*/
?>
<html>
<head>
<title></title>
</head>
<body>
<form>
<input type="submit" name="submit" /><span class="adderr" name="adderr" id="adderr"></span>
</form>
<?php
$var=0;
if($var==0)
{?>
<script>
document.getElementById("adderr").innerHTML = "Already having name!";
</script>
<?php
}
?>
</body>
</html>
It works fine

my code is not working on action method

This code use for display report on front end and in csv file also displa this code running properly and this code is redirect on same page document.frmmonthly.action ="test2.php"; but second code is not working properly the code is not redirect to other page what is reason or what's the problem in my code......
document.frmmonthly.action ="csv.php";
action method is not working on csv.php.....my code is not working plz help me...
<head>
<script language="text/javascript">
function OnSubmitForm(value) {
if(value == 'Generate') {
document.frmmonthly.action ="test2.php";
} else if(value == 'Download') {
document.frmmonthly.action ="csv.php";
}
return true;
}
</script>
</head>
<form method="POST" onSubmit="return OnSubmitForm();">
</tr>
<tr>
<td><input type="submit" name="submit"
onSubmit="OnSubmitForm(this.value)"
value="Generate"></tr>
<tr>
<td><input type="submit" name="submit1"
onSubmit="OnSubmitForm(this.value)"
value="Download" action="csv.php">
</tr>
</form>
Try this:
<form method="POST" name="frmmonthly" onSubmit="return OnSubmitForm();">
[Edit:] Also you have to rename onSubmit e.g. to onClick
Try this
function OnSubmitForm(value)
{
if(value == 'Generate')
{
document.frmmonthly.action ="test2.php";
}
else if(value == 'Download')
{
document.frmmonthly.action ="csv.php";
}
return true;
}
</script>
</head>
<form method="POST" name="frmmonthly" onSubmit="return OnSubmitForm();">
</tr>
<tr>
<td><input type="submit" name="submit" onSubmit="OnSubmitForm(this.value)" value="Generate"></tr>
<tr>
<td><input type="submit" name="submit1" onSubmit="OnSubmitForm(this.value)" value="Download">
</tr>
</form>
I remove action=".." from input type
Ypu should name your form:
<form method="POST" onSubmit="return OnSubmitForm();" name="frmmonthly">
Since the form doesn't have a name, your method can't set it's action.

Submit button is not working after using javascript

I created a login page that has a login form which submits username and password to loginproc.php in order to check their validity and everything was working perfectly. Anyway, on the login page I wrote a JavaScript function that alert the user in case the username and password fields are empty. After I've done that, the JavaScript works fine but when i click the submit button nothing happens absolutely!
----------------------------------------This is the JavaScript----------------------
<script type='text/javascript'>
function formValidator(){
// Make quick references to our fields
var username = document.getElementById('username');
var password = document.getElementById('password');
// Check each input in the order that it appears in the form!
if(notEmpty(username, "The username field is empty!")){
if(notEmpty(password, "The password is empty!")){
}
}
return false;
}
function notEmpty(elem, helperMsg){
if(elem.value.length == 0){
alert(helperMsg);
elem.focus(); // set the focus to this input
return false;
}
return true;
}
</script>
----------------------------------------This is the form----------------------
<form method="post" action="loginproc.php" onsubmit="return formValidator()" >
<tr><td colspan="3"><img src="icons/login.jpg" alt="Edugate" width="366" height="123"></td></tr>
<tr><td colspan="3" nowrap bgcolor="#990033" class="infoTitle"><div align="center"></div></td></tr>
<tr><td colspan="3" nowrap class="infoTitle"><div align="left">user login </div></td></tr>
<tr><td colspan="3" nowrap bgcolor="#990033" class="infoTitle"><div align="center"></div></td></tr>
<tr><td width="58">Username</td>
<td width="4">:</td>
<td width="297"><input type="text" id="username" name="username" size="20"></td></tr>
<tr><td>Password</td><td>:</td><td><input type="password" id="password" name="password" size="20"></td></tr>
<tr><td> </td><td> </td><td><input type="submit" class="BtnInTable" value="Login"></td></tr>
<?php if ($error == 1)
print "<tr><td colspan='3' class='ppos'><img src='icons/error.png' alt='error'> Icorrect usename or password...</td></tr>";?>
<tr><td colspan="3" nowrap bgcolor="#990033" class="infoTitle"><div align="center"></div></td></tr>
</form>
Kindly, can anybody guide me. Thanks in advace
You forgot return true;, it will go to return false; otherwise.
// Check each input in the order that it appears in the form!
if(notEmpty(username, "The username field is empty!")){
if(notEmpty(password, "The password is empty!")){
return true;
}
}
You formValidator() always returns false, and if you return false to the onsubmit, it will not submit.
Be sure to return true if everything is valid.
heres the problem..
this function only returns FALSE.
no return true statement..
for form to submit, the "onsubmit" should return true :)

Can't get php mailform submit button to refresh current DIV layer only

I can get the PHP mailform to work correctly. The problem is I would like the submit button upon validation to not refresh the entire page but just the content div layer. I have researched various solutions however I am fairly new to PHP so I may be entering the code incorrectly. The working page can be found at www.kaimeramedia.com/derek/Website and then by clicking on the contact link. The actual contact page code is:
<form method="post" id="captcha_form" name="form1" action="mailform.php">
<br />
<table width="100%" border="0">
<tr><td rowspan="4" width="125">
</td>
<td>
<div id="Imprint3">Name:</div></td>
<td width="15">
</td><td><div id="Imprint3">All fields are required.</div></td>
<tr>
<td width="150">
<input type="text" id="name" name="name" maxlength="30" value="name" onfocus="if
(this.value==this.defaultValue) this.value='';"/></td>
<td rowspan="5" colspan="2">
<table>
<tr><td width="10"></td><td>
<div id="Imprint3">Message:</div></td></tr>
<tr><td width="10">
</td>
<td width="200"><textarea name="message" id="message"
rows="6" cols="25" value="Enter your message" onfocus="if
(this.value==this.defaultValue) this.value='';"/><?php echo "</tex" . "tarea>"; ?>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<input name="submit" type="submit" value="Submit" />
<input type="button" name="reset_form" value="Reset" onclick="this.form.reset();">
</td></tr></table>
</td>
</tr>
<tr>
<td valign="top"><div id="Imprint3">Email:</div></td> </tr>
<td valign="top">
<input type="text" id="email" name="email" maxlength="100" value="you#email.com"
onfocus="if(this.value==this.defaultValue) this.value='';"/></td>
</tr> <tr>
<td width="15"></td>
<td align="center"> <div style="padding-bottom: 1em;">
<img src="captcha.php" /> <br />
<input type="text" name="userpass" value="input the above text here" onfocus="if
(this.value==this.defaultValue) this.value='';"></div></td></tr></table></form>
and the mailform.php file is as follows:
<?PHP
session_start();
try{
$check = new check();
if(!isset($_REQUEST['email']))
throw new exception('You did not enter an email address.');
if(!isset($_REQUEST['message']))
throw new exception('You did not enter a message.');
if(!isset($_REQUEST['name']))
throw new exception('You did not enter a name');
$sender = $_REQUEST['email'];
$message = $_REQUEST['message'];
$name = $_REQUEST['name'];
$recipient = 'text#text.com';
$subject = 'Regarding Your Portfolio';
if($check->captcha('userpass') == FALSE)
throw new exception('Your captcha is incorrect.');
if($check->spam($sender) == FALSE)
throw new exception('Your email field contains spam.');
if($check->spam($name) == FALSE)
throw new exception('Your name field contains spam.');
if($check->length($sender, 10) == FALSE)
throw new exception('Your email field does not satisfy the minimum character
count.');
if($check->length($message, 8) == FALSE)
throw new exception('Your message field does not satisfy the minimum character
count.');
if($check->length($name, 3) == FALSE)
throw new exception('Your name field does not satisfy the minimum character count.');
mail($recipient, $subject, $message, "From: $name <$sender>" );
include'thankyou.php';
}catch (Exception $E){
die($E->getMessage());
}
class check{
function captcha($field){
if(isset($_REQUEST[$field])==FALSE){ return false; }
if($_SESSION['pass'] != $_REQUEST[$field]){ return false; }
return true;
}
function email($email){
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ return false;}
return true;
}
function spam($field){
if(eregi("to:",$field) || eregi("cc:",$field) || eregi("\r",$field) ||
eregi("\n",$field) || eregi("%0A",$field)){ return false; }
return true;
}
function length($field, $min){
if(strlen($field) < $min){ return false; }
return true;
}
}
?>
I have replaced my receiving email to text#text.com on this page, but on my site it is my actual email. Another user helped me a lot with getting this script functioning correctly, but I was wondering if it is possible to get the thankyou.php to refresh into the current div layer and not refresh the entire page. the line that I want to edit I believe is the: include 'thankyou.php'; Any help would be appreciated.
This is not possible with php.
You will have to use JavaScript to realize this.
You would be able to send the email without refreshing the page and to load a file in a div.
With e.g. jQuery and the $.ajax function it would look something like this:
var msg; // defines variable "msg"
$('#button_ID').click(function(e){
e.preventDefault(); // this prevents the page from reloading (or default behavior, so the form submit)
msg = $('#input_field_ID').val(); // gets the value of the input field and saves it in variable "msg"
$.ajax({
type: 'POST',
url: '/sendmail.php',
data: {message : msg}, // posts variable "msg" as "message"
cache: false,
success: function(data) {
$("#yourdivID").load('/thankyou.php'); // loads a file in a div
},
error: function(xhr, ajaxOptions, thrownError){
alert('there was an error');
}
});
});
In this example you would be able to get the message in sendmail.php with $_POST["message"]
Or use $.post and serialize your entire form:
$.post("/sendmail.php", $("#formID").serialize());
Just that you can see how to handle something like this.

Problems With Dynamic Form Using JavaScript and PHP

I'm having trouble with these files, see if you guys can help me out :)
I get an object required error..
index.php
<?php
//Start session
session_start();
//Require functions.php
require("functions.php");
//Require posts.php
require("posts.php");
?>
<html>
<body>
<title>Log Into Booking System</title>
<?php
//Call function to display login
displayLogin();
?>
</body>
</html>
posts.php
<?php
if (isset($_POST['sublogin']))
{
//Check that all fields were typed in and are not null
if(!$_POST['user'] || !$_POST['pass'])
{
//Call function to display error
printText("Please make sure all required fields are filled in.");
}
else
{
//Here is some database checking
}
}
?>
functions.php
<?php
function displayLogin()
{
//Break php, then return to PHP
?>
<h1>Login</h1>
<form action="" method="post">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr><td>Username:</td><td><input type="text" name="user" maxlength="30"></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass" maxlength="30"></td></tr>
<tr><td><div id='error'></div></td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="sublogin" value="Login"></td></tr>
</table>
</form>
<?
}
function printText($txtmsg)
{
?>
<!-- Set up the displayError javascript function -->
<script type="text/javascript">
function displayError()
{
document.getElementById('error').innerHTML = <?echo $txtmsg;?>;
}
</script>
<!-- Call function to Display the error -->
<script type="text/javascript">
displayError();
</script>
<?
}
?>
I'm pretty sure
document.getElementById('error').innerHTML
Is causing me problems, i think it might have to do with how im calling it, like it dosent know what its looking for..
Thanks guys much appreciated,
Jenny
EDIT: Here is the paste of what jQuerybeast tried to do, http://pastebin.com/jj0RVMAd
SOLUTION: Who knew it was this easy? You MUST change the order of where posts.php is required as well as make sure you don't call a javascript function within functions.php because that will execute the javascript above the form and therefore giving a null object error.
Index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Log Into Booking System</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php
//Start session
session_start();
//Require functions.php
require("functions.php");
?>
</head>
<body>
<?php
//Call function to display login
displayLogin();
//Require posts.php
require("posts.php");
?>
</body>
</html>
posts.php
<?php
//POST: Main login page
if (isset($_POST['sublogin']))
{
//Display error if feilds are not filled in
if(!$_POST['user'] || !$_POST['pass'])
{
echo '<script type="text/javascript">document.getElementById("error").innerHTML = "Please make sure all required fields are filled in.";</script>';
return false;
}
else
{
//Some database minipulation here....
//...
if ($_POST['user'] != $databaseUsername)
{
echo '<script type="text/javascript">document.getElementById("error").innerHTML = "Please make sure your username is correct.";</script>';
return false;
}
elseif($_POST['pass'] != $databasePassword)
{
echo '<script type="text/javascript">document.getElementById("error").innerHTML = "Please make sure your password is correct.";</script>';
return false;
}
}
return true;
}
?>
functions.php
<?php
//Function: Display main.php login
function displayLogin()
{
//Break php, then return to PHP
?>
<div style="float: left; padding: 10px; width:20%;">
<fieldset>
<legend>
<h1>Login</h1>
</legend>
<form name = "mainlogin" action="" method="post">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr><td>Username:</td><td><input type="text" name="user" maxlength="30"></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass" maxlength="30"></td></tr>
<tr><td colspan="2" align="left"><input type="checkbox" name="remember">
<font size="2">Remember username and password</td></tr>
<tr><td><div id="error"></div></tr><td>
<tr><td colspan="2" align="right"><input type="submit" name="sublogin" value="Login"></td></tr>
</table>
</form>
</fieldset>
</div>
<?php
}
?>
The first thing I notice is that the PHP-echoed string inside the Javascript function doesn't have quotation marks around it.
Change:
function displayError()
{
document.getElementById('error').innerHTML = <?echo $txtmsg;?>;
}
To:
function displayError()
{
document.getElementById('error').innerHTML = "<?echo $txtmsg;?>";
}
I think the problem is caused from functions.php is because you open and close PHP the opposite way.
Instead of ?> <?
Try: <? ?>
Which is this:
function displayLogin()
{
//Break php, then return to PHP
<?
<h1>Login</h1>
<form action="" method="post">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr><td>Username:</td><td><input type="text" name="user" maxlength="30"></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass" maxlength="30"></td></tr>
<tr><td><div id='error'></div></td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="sublogin" value="Login"></td></tr>
</table>
</form>
?>
}
function printText($txtmsg)
{
<?
<!-- Set up the displayError javascript function -->
<script type="text/javascript">
function displayError()
{
document.getElementById('error').innerHTML = <?echo $txtmsg;?>;
}
</script>
<!-- Call function to Display the error -->
<script type="text/javascript">
displayError();
</script>
?>
}
change
document.getElementById('error').innerHTML = <?echo $txtmsg;?>;
to
document.getElementById('error').innerHTML = '<?= $txtmsg; ?>';
or
document.getElementById('error').innerHTML = '<?php echo $txtmsg; ?>';
Use this
document.getElementById('error').innerHTML = '<?php echo $txtmsg; ?>';
and
It seems short tag is not enabled on your server. Please make sure.
Or always use <?php ?> instead of <? ?>

Categories