I'm using a Contract form from HTML Contract Form Guide in my website. In testing, I am running my website from my computer using apache and PHP v5.5. Everything seems to work well on my testing server and I can fill out the form, click submit, and land on the "success page". When I upload my site to my hosting service and try to use the contact form, the captcha does not accept my input and returns the message, "failed the anti-spam check". My hosting service is running PHP v5.3. I'm not sure what's changing and why my form no longer works when I upload it. Oh, I am also learning PHP on the fly so I might not fully comprehend what I am being asked so it might take me a little longer to give an accurate reply.
* This issue was solved. I ended up contacting fatcow which is my hosting company. This is their response "I have set the session.save_path in your 'PHP Scripting' and I have increased the memory limit from 32 MB to 128 MB for your PHP scripts." I guess it was a server side issue. I guess I've narrowed it down and when I run to a similar issue I can also assume it is a server side issue. Hope anyone with a similar issue can use this post and decide what to do accordingly.
I ran var_dump() on $_SESSION['FGCF_Captcha_Answer'] and $user_answer and both values were equal. This is what I got:
string 'c85757e710d687d24c0b044812d5ee05' (length=32)
string 'c85757e710d687d24c0b044812d5ee05' (length=32)
I am also hosting from a fatcow server and have contacted them to see if they know anything about this issue with reCAPTCHA on their server. Just awaiting their reply. Is it possible that the server I'm running on could experience these things, maybe not a programming question but like I stated above, on my own computer running with apache the form works fine and the only problem I'm having on fatcow is not the entire form, just reCAPTCHA
Here is the captcha code, if anything else is needed please let me know:
<?PHP
class FGSimpleCaptcha extends FG_CaptchaHandler
{
var $error_str;
var $captcha_varname;
var $uniquekey;
function FGSimpleCaptcha($captcha_var_name)
{
$this->captcha_varname=$captcha_var_name;
$this->uniquekey='KHJhsjsy65HGbsmnd';
}
/*Add more simple questions here.*/
function GetSimpleCaptcha()
{
$arrQuestions = array(
"What color is the sky? "=>"blue",
"What is 1+1=" => "2",
"What is the color of grass?"=>"green",
"Are you a robot? "=>"no",
"Are you human?"=>"yes");
$question = array_rand($arrQuestions);
$answer = $arrQuestions[$question];
$_SESSION['FGCF_Captcha_Answer'] = $this->Md5CaptchaAnswer($answer);
return $question;
}
function SetFormKey($key)
{
$this->uniquekey = $key;
}
function GetKey()
{
return $this->uniquekey;
}
function Validate()
{
$ret=false;
if(empty($_POST[$this->captcha_varname]))
{
$this->error_str = "Please answer the anti-spam question";
$ret = false;
}
else
{
$scaptcha = trim($_POST[$this->captcha_varname]);
$scaptcha = strtolower($scaptcha);
$user_answer = $this->Md5CaptchaAnswer($scaptcha);
if($user_answer != $_SESSION['FGCF_Captcha_Answer'])
{
$this->error_str = "Failed the anti-spam check!";
$ret = false;
}
else
{
$ret = true;
}
}//else
return $ret;
}
function Md5CaptchaAnswer($answer)
{
return md5($this->GetKey().$answer);
}
function GetError()
{
return $this->error_str;
}
}
?>
Here is partial code for the form:
class FGContactForm
{
var $receipients;
var $errors;
var $error_message;
var $name;
var $email;
var $message;
var $from_address;
var $form_random_key;
var $conditional_field;
var $arr_conditional_receipients;
var $fileupload_fields;
var $captcha_handler;
var $mailer;
function FGContactForm()
{
$this->receipients = array();
$this->errors = array();
$this->form_random_key = 'HTgsjhartag';
$this->conditional_field='';
$this->arr_conditional_receipients=array();
$this->fileupload_fields=array();
$this->mailer = new PHPMailer();
$this->mailer->CharSet = 'utf-8';
}
function EnableCaptcha($captcha_handler)
{
$this->captcha_handler = $captcha_handler;
session_start();
}
function AddRecipient($email,$name="")
{
$this->mailer->AddAddress($email,$name);
}
function SetFromAddress($from)
{
$this->from_address = $from;
}
function SetFormRandomKey($key)
{
$this->form_random_key = $key;
}
function GetSpamTrapInputName()
{
return 'sp'.md5('KHGdnbvsgst'.$this->GetKey());
}
function SafeDisplay($value_name)
{
if(empty($_POST[$value_name]))
{
return'';
}
return htmlentities($_POST[$value_name]);
}
function GetFormIDInputName()
{
$rand = md5('TygshRt'.$this->GetKey());
$rand = substr($rand,0,20);
return 'id'.$rand;
}
function GetFormIDInputValue()
{
return md5('jhgahTsajhg'.$this->GetKey());
}
function SetConditionalField($field)
{
$this->conditional_field = $field;
}
function AddConditionalReceipent($value,$email)
{
$this->arr_conditional_receipients[$value] = $email;
}
function AddFileUploadField($file_field_name,$accepted_types,$max_size)
{
$this->fileupload_fields[] =
array("name"=>$file_field_name,
"file_types"=>$accepted_types,
"maxsize"=>$max_size);
}
function ProcessForm()
{
if(!isset($_POST['submitted']))
{
return false;
}
if(!$this->Validate())
{
$this->error_message = implode('<br/>',$this->errors);
return false;
}
$this->CollectData();
$ret = $this->SendFormSubmission();
return $ret;
}
function RedirectToURL($url)
{
header("Location: $url");/* Redirect browser */
/* Make sure that code below does not get executed when we redirect. */
exit;
}
function GetErrorMessage()
{
return $this->error_message;
}
function GetSelfScript()
{
return htmlentities($_SERVER['PHP_SELF']);
}
function GetName()
{
return $this->name;
}
function GetEmail()
{
return $this->email;
}
function GetMessage()
{
return htmlentities($this->message,ENT_QUOTES,"UTF-8");
}
This issue was solved. I ended up contacting fatcow which is my hosting company. This is their response "I have set the session.save_path in your 'PHP Scripting' and I have increased the memory limit from 32 MB to 128 MB for your PHP scripts." I guess it was a server side issue. With some help I was able to narrow it down and when I run to a similar issue I can also assume it is a server side issue. Hope anyone with a similar issue can use this post and decide what to do accordingly.
Related
I am developing a Register/Login system with validation. Registering system is working well. For example, when I register the same email twice, the following message appears:
Email already registered!
However, when I log-in with the same e-mail and password, an error occurs. The following message appears as a validation error:
Email not registered!
Even if the email is registered in DB.
Code for e-mail validation:
<?php
public function validateEmail($par)
{
if (filter_var($par, FILTER_VALIDATE_EMAIL)) {
return true;
} else {
$this->setErro("Invalid Email!");
return false;
}
}
public function validateIssetEmail($email, $action = null)
{
$b = $this->cadastro->getIssetEmail($email);
if ($action == null) {
if ($b > 0) {
$this->setErro("Email already registered!");
return false;
} else {
return true;
}
} else {
if ($b > 0) {
return true;
} else {
$this->setErro("Email not registered!");
return false;
}
}
}
Code for login controller:
<?php
$validate = new Classes\ClassValidate();
$validate->validateFields($_POST);
$validate->validateEmail($email);
$validate->validateIssetEmail($email,"login");
$validate->validateStrongSenha($senha);
$validate->validateSenha($email,$senha);
var_dump($validate->getErro());
Code for class login:
<?php
namespace Models;
class ClassLogin extends ClassCrud
{
# Returns user data
public function getDataUser($email)
{
$b = $this->selectDB(
"*",
"users",
"where email=?",
array(
$email
)
);
$f = $b->fetch(\PDO::FETCH_ASSOC);
$r = $b->rowCount();
return $arrData = [
"data" => $f,
"rows" => $r
];
}
}
My getIssetEmail method exists on Register code only.
# Check directly at the bank if the email is registered
public function getIssetEmail($email)
{
$b = $this->selectDB(
"*",
"users",
"where email=?",
[
$email
]
);
return $r = $b->rowCount(); // returns the amount of rows in the search
}
And ClassPassword
<?php
namespace Classes;
use Models\ClassLogin;
class ClassPassword
{
private $db;
public function __construct()
{
$this->db = new ClassLogin();
}
# Create password's hash to save in DB
public function passwordHash($senha)
{
return password_hash($senha, PASSWORD_DEFAULT);
}
# Verify if password's hash is correct
public function verifyHash($email, $senha)
{
$hashDb = $this->db->getDataUser($email);
return password_verify($senha, $hashDb["data"]["senha"]);
}
}
This is not an answer but hopefully it will help in debugging.
First, I'm going to change your code. This is 100% a style choice but I personally think it is easier to follow. If you have an if statement that always returns, you don't technically need an else. Once again, this is a style choice and you don't have to follow it.
Second, if you can, try adding logging into your workflow, it will save you so much time debugging. It isn't always an option, especially for legacy code bases, but it is awesome when you can inspect complex code. In this example, I"m just making a couple of helper methods that dump stuff but normally I'd use something like Monolog to write to a stream that I can tail, and I can easily turn it off in production. When logging, sometimes it helps to avoid identical messages so that you can easily find the exact line number you are on, too.
So with those changes, try running this code inside of your class:
private function logMessage($message)
{
echo $message . PHP_EOL;
}
private function logVariable($variable)
{
var_dump($variable);
}
public function validateIssetEmail($email, $action = null)
{
$this->logVariable($email);
$this->logVariable($action);
$b = $this->cadastro->getIssetEmail($email);
$this->logVariable($b);
if ($action === null) {
$this->logMessage('Action was null');
if ($b > 0) {
$this->logMessage('B is greater than zero');
$this->setErro("Email already registered!");
return false;
}
$this->logMessage('B was not greater than zero');
return true;
}
$this->logMessage('Action was not null');
if ($b > 0) {
$this->logMessage('B is greater than zero');
return true;
}
$this->logMessage('B was not greater than zero');
$this->setErro("Email not registered!");
return false;
}
This should log in human-readable form every step. You should be able to walk through this and identify where your bug is. For instance, in the comments above you said that a variable was 0 in a block that was guarded by a check that guarantees that that shouldn't happen.
This is the wrong part i guess you assigned login as action so you can call cadastro class inside of the function
$cadastro = new Cadastro();
$b = $cadastro->getIssetEmail($email);
if ($action == null) {
if ($b > 0) {
$this->setErro("Email already registered!");
return false;
} else {
return true;
}
} else {
if ($b > 0) {
return true;
} else {
$this->setErro("Email not registered!");
return false;
}
}
BotMan Version: 2.1
PHP Version:7.3
Messaging Service(s):
Cache Driver: SymfonyCache
Description:
I trying to have conversation. In every next method I lost data from conversation properties, that was saved in properties before!
class GetAlertDataConversation extends AppConversation
{
public $bot;
public $alertTitle;
public $alertDescription;
public $alertLatitude;
public $alertLongitude;
public $alertAuthorId;
public $alertAuthorName;
public function __construct($bot)
{
$this->bot = $bot;
}
private function askTitle()
{
$this->ask('Что случилось? (кратко)', function (Answer $answer) {
$this->alertTitle = $this->askSomethingWithLettersCounting($answer->getText(), 'Слишком коротко!', 'askDescription');
\Yii::warning($this->alertTitle);
});
}
public function askDescription()
{
$this->ask('Расскажи подробней!', function (Answer $answer) {
$this->alertDescription = $this->askSomethingWithLettersCounting($answer->getText(), 'Слишком коротко!', 'askLocation');
\Yii::warning($this->alertTitle);
});
}
private function askLocation()
{
\Yii::warning($this->alertTitle);
$this->askForLocation('Локация?', function (Location $location) {
// $location is a Location object with the latitude / longitude.
$this->alertLatitude = $location->getLatitude();
$this->alertLongitude = $location->getLongitude();
$this->endConversation();
return true;
});
}
private function endConversation()
{
\Yii::warning($this->alertTitle);
$alertId = $this->saveAlertData();
if ($alertId)
$this->say("Событие номер {$alertId} зарегистрировано!");
else
$this->say("Ошибка при сохранении события, обратитесь к администратору!");
}
private function saveAlertData()
{
$user = $this->bot->getUser();
$this->alertAuthorId = $user->getId();
$this->alertAuthorName = $user->getFirstName() . ' ' . $user->getLastName();
$alert = new Alert();
\Yii::warning($this->alertTitle);
$alert->name = $this->alertTitle;
$alert->description = $this->alertDescription;
$alert->latitude = $this->alertLatitude;
$alert->longitude = $this->alertLongitude;
$alert->author_id = $this->alertAuthorId;
$alert->author_name = $this->alertAuthorName;
$alert->chat_link = '';
$alert->additional = '';
if ($alert->validate()) {
$alert->save();
return $alert->id;
} else {
\Yii::warning($alert->errors);
\Yii::warning($alert);
return false;
}
}
}
There is user's text answer in the first \Yii::warning($this->alertTitle); in the askTitle() function.
But all other \Yii::warning($this->alertTitle); returns NULL!!!!
As the result, saving of Alert object not working!
Please, help me. Some ideas?
I think, that it can be by some caching + serialise problem.
I was trying to change cache method to Redis. Same result.
Problem was in this function call and caching:
$this->askSomethingWithLettersCounting($answer->getText(), 'Слишком коротко!', 'askDescription');
If you will read other conversation botman issues in github, you wil see, that most of issues in conversation cache and serialise.
Not any PHP code of conversation can be cached right.
In this case, not direct call of functions askDescription() and askLocation() broken conversation caching.
I fixed that by removing askSomethingWithLettersCounting() function.
<?php
error_reporting(E_ALL ^ E_NOTICE);ini_set('error_reporting', E_ALL ^ E_NOTICE);
define('IS_ADMIN_FLAG', false);
include_once(dirname(__FILE__).'/../../config/config.inc.php');
include_once(dirname(__FILE__).'/../../config/setting.inc.php');
include_once('includes/model/smsAdapter.php');
include_once('includes/model/sms.php');
include_once('includes/model/variables.php');
class ControllerSmsApi
{
public function __construct()
{
$this->index();
}
public function index()
{
die("DISABLED");
$to = $this->getVar("to");
$text = $this->getVar("text");
$unicode = $this->getVar("unicode");
$type = $this->getVar("type");
$transaction = $this->getVar("transaction");
if(isset($to) && strlen($to) > 4 && strlen($text) > 0)
{
$sms = new SmsModel(true, SmsModel::TYPE_SIMPLE, $type, ($transaction ? SmsModel::SMS_TRANSACTION : SmsModel::SMS_BULK));
$sms->number($to)->text($text)->unicode($unicode)->send();
if(!$sms->isError())
{
echo "SMSSTATUS:OK";
}
else
{
echo "SMSSTATUS:ERROR";
}
}
else
{
echo "SMSSTATUS:ERROR";
}
}
private function getVar($var)
{
if(filter_input(INPUT_POST, $var))
{
return filter_input(INPUT_POST, $var);
}
elseif(filter_input(INPUT_GET, $var))
{
return filter_input(INPUT_GET, $var);
}
else
{
return null;
}
}
}
new ControllerSmsApi();
?>
I have an ecommers website in which customer placed order and get all updation via email service ,but now i want to make it for sms service also for which i have sms api in msg91 for php. But unfortunately i am unable to integrate it with prestashop via prestasms or any other free module.
Actually making a Module should do the job and adding
various hooks might do the job, you can generate one with nearly all you will need here : https://validator.prestashop.com/
Based on your answer you will certainly need two hooks : actionOrderStatusUpdate and actionValidateOrder. You can also get an updated list here http://www.prestarocket.com/blog/prestashop-1-7-hook-list-liste-des-hooks/.
If you need example of a module well working, you can take a look at modules/dashactivity/ which one of the most compliant to Prestashops guidelines.
Your code might look like this in the end :
<?php
class Msg91SMS extends Module
{
public function __construct()
{
$this->name = 'msg91sms';
$this->tab = 'front_office';
$this->version = '1.0.1';
$this->author = 'YourName';
$this->displayName = $this->l('MSG91SMS');
$this->description = $this->l('Description');
// Hooks you need, setup on install so you might do it again
$this->hooks = array(
'actionValidateOrder',
'actionOrderStatusUpdate',
);
}
public function install() {
if (!parent::install()) {
return false;
} else {
if (isset($this->hooks) && !empty($this->hooks)) {
foreach ($this->hooks as $v) {
if (!$this->registerHook($v)) {
return false;
}
}
}
}
}
public function hookActionValidateOrder($params) {
$order = $params['order'];
// Do your magic here
}
public function hookActionOrderStatusUpdate($params) {
// Same as above, remember to check order state to see if it interests you some ways with $order->id_state and a switch / case
}
}
I have my own MVC and in my BaseController i create simple method flashMessage.
public function flashMessage($name, $value)
{
if(!isset($_SESSION['message'][$name])) {
$_SESSION['message'][$name] = $value;
}
}
This work good but i dont know when to destroy this session. Is good idea to put in __destructor session_unset($_SESSION['message']); ?
This work good but my message has no lifetime
public function authenticate()
{
if(isset($_POST['submit']))
{
$username = $this->inputFilter($_POST['username']);
$password = $this->inputFilter($_POST['password']);
// check if user exist
if(!$this->auth->autheticate($username, $password)) {
$this->flashMessage('error', 'Error: Invalid username or password!');
return $this->redirect('login');
}else {
$this->flashMessage('success', 'Success: Uspešno ste se prijavili na sistem!');
return $this->redirect('home');
}
}
}
You remove it on read.
A simple example, by making the flash message a class.
class FlashMessage
{
static function create($name, $value)
{
if(!isset($_SESSION['message'][$name])) {
$_SESSION['message'][$name] = $value;
}
}
static function read($name)
{
if(isset($_SESSION['message'][$name])) {
$message = $_SESSION['message'][$name];
unset($_SESSION['message'][$name]);
return $message;
}
//return null, false or throw exception
}
}
I suggest looking in already implemented flash messages algorithm.
E.g. in Yii you can set to remove flash message when it was shown:
$this->setFlash('type', 'message');
$this->showFlash('type');
function showFlash($type) {
$msg = isset($_SESSION['message'][$type]) ? $_SESSION['message'][$type] : null;
if (!is_null($msg)) {
unset($_SESSION['message'][$type]);
}
return $msg;
}
I was going through a tutorial, on PHPUNIT i got the tutorial quit well...and do some logins
on my own..
But i went into the web to load a page and submit a value on a real-time page
it works fine... but the issue i have is that after entering the user key ..it submit but it wont wait for the next page
I see similar questions here but none is working-out for me
Pls Can anyone help me.
This is my code
<?php
class TestLogin extends PHPUnit_Extensions_Selenium2TestCase {
public function setUp()
{
$this->setHost('localhost');
$this->setPort(4444);
$this->setBrowser('firefox');
$this->setBrowserUrl('http://localhost/tutor');
}
public function setSpeed($timeInMilliSec)
{
$this->setSpeed('120');
}
/*Function to locate website*/
public function testHasLoginForm()
{
$this->url('http://www.jamb.org.ng/DirectEntry/');/*This is the site*/
$username = $this->byId('ctl00_ContentPlaceHolder1_RegNumber');/*Search for a name*/
##ctl00_ContentPlaceHolder1_txtRegNumber
$action = $this->byId('ctl00_ContentPlaceHolder1_Reprint')->attribute('action');
$this->byId('ctl00_ContentPlaceHolder1_RegNumber')->value('49130518ED');/*value of the textbox*/
$jump = $this->byId('ctl00_ContentPlaceHolder1_Reprint');
$jump->submit();
}
}
?>
You could use
$this->timeouts()->implicitWait(10000);//10 seconds
to set timeout of searching elements on page.
https://github.com/sebastianbergmann/phpunit-selenium/blob/master/PHPUnit/Extensions/Selenium2TestCase/Session/Timeouts.php
Resolved.i just changed
$jump->submit();
to
$this->byName('ctl00$ContentPlaceHolder1$Reprint')->click();
If you want to wait for diffrent elements or even capabilities i suggest use this pattern
This is example wait for element. I recommend using byCssSelector here to get more flexibility.
You can easily use this pattern for example to wait for element to be clickable:
protected function waitForClickable($element, $wait=30) {
for ($i=0; $i <= $wait; $i++) {
try{
$element->click();
return true;
}
catch (Exception $e) {
sleep(1);
}
}
return false;
}
You can even use it in any context passing anonymous function as parameter:
protected function waitForAny($function, $args, $wait=30) {
for ($i=0; $i <= $wait; $i++) {
try{
call_user_func_array($function, $args);
return true;
}
catch (Exception $e) {
sleep(1);
}
}
return false;
}
Usage:
$f = function($e){
$e->click();
};
$this->waitForAny($f, array($element));