Why value doesn't want to increment when I submit a post - php

I'm making a login attempt checker, so if user inputs wrong key (witch was send via email) then It add +1 on the attempt meter. I stored it in a session did quite a lot of research but it just doesn't work. here is my PHP code.
session_start();
$_SESSION['poskusi'] = 0;
$kljuc = $_SESSION['rand_kljuc'];
if(isset($_POST['vpis_kljuc'])){
$vpis_kljuc = $_POST['vpis_kljuc'];
if($vpis_kljuc == $kljuc){
echo "You are in";
}
else {
echo "Wrong key";
$_SESSION['poskusi']+1;
if($_SESSION['poskusi'] == 3){
echo "locked";
}
}
}

You are setting the counter to 0 every time the page loads.
Try this:
if(!isset($_SESSION['poskusi'])) {
$_SESSION['poskusi'] = 0;
}
You are also incrementing it wrong. It should be
$_SESSION['poskusi']++; or
$_SESSION['poskusi']+=1; if you prefer.

Related

vTiger Event Handler to check if record exists

I am working on vTiger 6.5 and I am trying to figure a way to see if a record exists in a custom module of mine. I want to check whether the 'policynumber' is new before saving, here is my code so far. For some reason it seems to act randomly depending on my module number chosen.
class isaHandler extends VTEventHandler {
function handleEvent($eventName, $entityData) {
global $adb;
$moduleName = $entityData->getModuleName();
if($moduleName=='isa'){
if($eventName == 'vtiger.entity.beforesave.modifiable') {
$isNew = $entityData->isNew('policynumber');
if ($isNew == false) {
echo "Duplicate policy number";
exit;
}
}
if($eventName == 'vtiger.entity.beforesave') {}}
if($eventName == 'vtiger.entity.beforesave.final') {
$price = $entityData->get('currentamount');
if($price > 20000){
echo "Please go back and enter less than 20000";
exit;
}
if($eventName == 'vtiger.entity.aftersave') {}
}
}
At the moment I am currently using an echo just to see the result. But later on I will perform more than this.
isNew()
Returns true if new record is being created, false otherwise.
More info is here
you should write a custom query to check policynumber already exist or not in your function:
if($eventName == 'vtiger.entity.beforesave.modifiable') {
global $adb;
$result = $adb->pquery("SELECT your-field-name FROM table_name WHERE policynumber=?", array($policynumbervalue));
if($result && $adb->num_rows($result)) {
echo "This policy number exist";
die();
}else{
// write your overwrite code
}
} //end if($eventName == 'vtiger.entity.beforesave.modifiable')
Update:
I am assuming there is field i.e. policynumber in your form, you enter some value in this field and submit the form. so you will get entered policy number value from this:
$policynumbervalue = $entityData->get('policynumber'); //this is vtiger standard way
if this does not work, you can simply use php global variable $_REQUEST['policynumber'] but I is not a good practice.
Hope this will help.
This is the update to my answer, I simply done an if statement on the number of rows displayed.
if($eventName == 'vtiger.entity.beforesave.modifiable') {
$policynumbervalue = $entityData->get('policynumber');
$sql = $adb->pquery("SELECT policynumber FROM vtiger_isa WHERE policynumber=?",array($policynumbervalue));
$nrows = $adb->num_rows($sql);
if($nrows > 0){
echo "<script type=\"text/javascript\">window.alert('ISA policy number already exists, you will be redirected to the updata module.');
window.location.href = '/vtigercrm/index.php?module=isa&view=List';</script>";
exit;
}

I need help fixing this code

So, I made a PHP page/link checker, which should not allow an user to visit/redirect to a page if isn't passed certain minutes from last visit/redirect.
The problem is, the user is being redirected to the page ALWAYS even if he already did it 1 min ago and the timer is 7 min (example). The timer is setted into MySQL as minutes.
can't figure out what is wrong in the code
this is the first page:
<?php
session_start();
$sql = "SELECT * FROM table_records";
$result = mysql_query($sql);
$records = array();
while ($row = mysql_fetch_assoc($result)) {
$records[] = $row;
}
foreach ($records as $record) {
$now = new DateTime();
if (!array_key_exists($record, $_SESSION['records']) || ($now->getTimestamp()-$_SESSION['records'][$record]) <= 600) {
echo "<td><center>".$record['id']."</center></td>";
echo "<td><center>".$record['name']."</center></td>";
echo "<td><center>".$record['link']."</center></td>";
echo "<td><center>".$record['delay']."</center></td>";`
} else {
// link disabled
}
}
?>
and this is the page the users are redirected to, to check the timer, and in case redirect them to the link.
$waiting_time = $delay * 60; //calculate delay time in seconds
if (!array_key_exists($id, $_SESSION['records'])) {
$_SESSION['records'][$id] = $now->getTimestamp();
header("Location: $link");
exit();
} elseif (array_key_exists($id, $_SESSION['records']) && ($now->getTimestamp()-$_SESSION['records'][$id]) >= $waiting_time) {
echo "Looks like you already visited this page";
} elseif (array_key_exists($id, $_SESSION['records']) && ($now->getTimestamp()-$_SESSION['records'][$id]) < $waiting_time) {
$_SESSION['records'][$id] = $now->getTimestamp();
header("Location: $link");
exit();
}
The problem is, the user is being redirected to the $link ALWAYS, even if he already visited, and the time of delay isn't passed.
What is wrong with the code?
DRY, you can write your if/elseif statements much easier:
if (array_key_exists($id, $_SESSION['records']) && ($now->getTimestamp()-$_SESSION['records'][$id]) < $waiting_time) {
echo "Looks like you already visited this page";
} else {
$_SESSION['records'][$id] = $now->getTimestamp();
header("Location: $link");
exit();
}
Now, if you look at it you'll see there are two things to check at first:
Is $_SESSION['records'] not empty (maybe session wasn't intialized on second page?) - var_dump ($_SESSION['records']) - what's in there?
what's the result of ($now->getTimestamp()-$_SESSION['records'][$id]) and what's in $waiting_time variable - var_dump it
Don't forget to call exit() after dumping the code and before redirection or simply comment location () lines, otherwise you'll see nothing
Third possibility (you'll know this is the case if you don't see var_dump printout) is that your browser remembers 301 redirection and when you go second time to same address it redirects automatically without calling your script - restart your browser or try different one.

PHP - counter error using $_SESSION

The $count++; does not working while i am using if(!isset($_SESSION["rand"]))...
I am building a number guessing game.
$numOfGess=4;
$min=1;
$max=10;
if(!isset($_SESSION["rand"])){
$_SESSION["rand"] = rand($min, $max);
$count=0;
}
if(isset($_POST["numGuess"])){
$numGuess = $_REQUEST["numGuess"];
if($numGuess != $_SESSION["rand"]) {
//Validation
if($numGuess < $min || $numGuess > $max) {
echo"Your number must be between 1 to 10 !";
}
//Number is Small
if($numGuess < $_SESSION["rand"]) {
echo "too small...";
$count++;
}
//Number is Big
if($numGuess > $_SESSION["rand"]) {
echo "too big...";
$count++;
}
if($count==$numOfGess) {
echo"Game Over!";
unset($_SESSION["rand"]);
}
}
else {
echo"You got it! (in your last chance)";
unset($_SESSION["rand"]);
}
}
You should store the count variable as a session variable as well, otherwise it gets reset with every request. So, just replace $count with $_SESSION['count'] and it should work.
Store count in your session variable. What happens if $_SESSION['rand'] is set? How does it know what the previous count was?
Your forgetting
Session_start()
to start/resume the session

PHP If offer is rejected 3 times, remove div

I have a "offer" button that lets the user enter a price offer. If they enter 3 offers and all are rejected, I want the div to be removed.
if(isset($_POST['offer_form']) && !empty($_POST['offer_form'])){
if($offers === "1"){
if($_POST['offer_form'] >= $price_offer){
echo 'Accepted';
}else{
echo '<div class="offer_errors">Offer Rejected</div>';
}
}else{
echo '<div class="offer_errors">Sorry, no offers can be made</div>';
}
}
You can try to use Sessions
session_start();
$_SESSION['offer']=1;
Something like that.
to increment is do something like this:
session_start();
if(isset($_SESSION['offer']) && $_SESSION['offer'] != ''){
$offer = $_SESSION['offer'];
$offer += 1;
$_SESSION['offer'] = $offer;
}else{
$_SESSION['offer'] = 1;
}
Just get some ideas from this. I dont think this is ready for what you need. Just modify it suit your needs.

Redirect to page only once?

I have this piece of php code that looks up the account prompt function on my website. basically if a user has violated a term and condition on the site, at login they are redirected to a prompt page that says you are very naughty and here's a warning.
My code is this:
<?php
$account_prompt = account_prompt();
while ($prompt = mysql_fetch_array($account_prompt))
if ($prompt['account_prompt'] == '1') {
redirect_to("prompt.php");
}
?>
My question is how can i get it to only redirect once?
Thanks
He just redirects 1ce unless you are stucked in an endless loop...
Try this
if (isset($prompt['account_prompt']) && $prompt['account_prompt'] == '1') {
header("Location: prompt.php");
exit;
}
Use a flag which is set to 1 according to your condition, and take the redirect out of the loop.
$flag = 0;
while ($prompt = mysql_fetch_array($account_prompt)) {
if ($prompt['account_prompt'] == '1') $flag = 1;
}
if ($flag == 1)
redirect_to("prompt.php");

Categories